layout-sidebar.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <script setup lang="ts">
  2. import type { CSSProperties } from 'vue';
  3. import { computed, shallowRef, useSlots, watchEffect } from 'vue';
  4. import { VbenScrollbar } from '@vben-core/shadcn-ui';
  5. import { useScrollLock } from '@vueuse/core';
  6. import { SidebarCollapseButton, SidebarFixedButton } from './widgets';
  7. interface Props {
  8. /**
  9. * 折叠区域高度
  10. * @default 42
  11. */
  12. collapseHeight?: number;
  13. /**
  14. * 折叠宽度
  15. * @default 48
  16. */
  17. collapseWidth?: number;
  18. /**
  19. * 隐藏的dom是否可见
  20. * @default true
  21. */
  22. domVisible?: boolean;
  23. /**
  24. * 扩展区域宽度
  25. */
  26. extraWidth: number;
  27. /**
  28. * 固定扩展区域
  29. * @default false
  30. */
  31. fixedExtra?: boolean;
  32. /**
  33. * 头部高度
  34. */
  35. headerHeight: number;
  36. /**
  37. * 是否侧边混合模式
  38. * @default false
  39. */
  40. isSidebarMixed?: boolean;
  41. /**
  42. * 顶部margin
  43. * @default 60
  44. */
  45. marginTop?: number;
  46. /**
  47. * 混合菜单宽度
  48. * @default 80
  49. */
  50. mixedWidth?: number;
  51. /**
  52. * 顶部padding
  53. * @default 60
  54. */
  55. paddingTop?: number;
  56. /**
  57. * 是否显示
  58. * @default true
  59. */
  60. show?: boolean;
  61. /**
  62. * 显示折叠按钮
  63. * @default false
  64. */
  65. showCollapseButton?: boolean;
  66. /**
  67. * 主题
  68. */
  69. theme: string;
  70. /**
  71. * 宽度
  72. */
  73. width: number;
  74. /**
  75. * zIndex
  76. * @default 0
  77. */
  78. zIndex?: number;
  79. }
  80. const props = withDefaults(defineProps<Props>(), {
  81. collapseHeight: 42,
  82. collapseWidth: 48,
  83. domVisible: true,
  84. fixedExtra: false,
  85. isSidebarMixed: false,
  86. marginTop: 0,
  87. mixedWidth: 70,
  88. paddingTop: 0,
  89. show: true,
  90. showCollapseButton: true,
  91. zIndex: 0,
  92. });
  93. const emit = defineEmits<{ leave: [] }>();
  94. const collapse = defineModel<boolean>('collapse');
  95. const extraCollapse = defineModel<boolean>('extraCollapse');
  96. const expandOnHovering = defineModel<boolean>('expandOnHovering');
  97. const expandOnHover = defineModel<boolean>('expandOnHover');
  98. const extraVisible = defineModel<boolean>('extraVisible');
  99. const isLocked = useScrollLock(document.body);
  100. const slots = useSlots();
  101. const asideRef = shallowRef<HTMLDivElement | null>();
  102. const hiddenSideStyle = computed((): CSSProperties => calcMenuWidthStyle(true));
  103. const style = computed((): CSSProperties => {
  104. const { isSidebarMixed, marginTop, paddingTop, zIndex } = props;
  105. return {
  106. '--scroll-shadow': 'var(--sidebar)',
  107. ...calcMenuWidthStyle(false),
  108. height: `calc(100% - ${marginTop}px)`,
  109. marginTop: `${marginTop}px`,
  110. paddingTop: `${paddingTop}px`,
  111. zIndex,
  112. ...(isSidebarMixed && extraVisible.value ? { transition: 'none' } : {}),
  113. };
  114. });
  115. const extraStyle = computed((): CSSProperties => {
  116. const { extraWidth, show, width, zIndex } = props;
  117. return {
  118. left: `${width}px`,
  119. width: extraVisible.value && show ? `${extraWidth}px` : 0,
  120. zIndex,
  121. };
  122. });
  123. const extraTitleStyle = computed((): CSSProperties => {
  124. const { headerHeight } = props;
  125. return {
  126. height: `${headerHeight - 1}px`,
  127. };
  128. });
  129. const contentWidthStyle = computed((): CSSProperties => {
  130. const { collapseWidth, fixedExtra, isSidebarMixed, mixedWidth } = props;
  131. if (isSidebarMixed && fixedExtra) {
  132. return { width: `${collapse.value ? collapseWidth : mixedWidth}px` };
  133. }
  134. return {};
  135. });
  136. const contentStyle = computed((): CSSProperties => {
  137. const { collapseHeight, headerHeight } = props;
  138. return {
  139. height: `calc(100% - ${headerHeight + collapseHeight}px)`,
  140. paddingTop: '8px',
  141. ...contentWidthStyle.value,
  142. };
  143. });
  144. const headerStyle = computed((): CSSProperties => {
  145. const { headerHeight, isSidebarMixed } = props;
  146. return {
  147. ...(isSidebarMixed ? { display: 'flex', justifyContent: 'center' } : {}),
  148. height: `${headerHeight}px`,
  149. ...contentWidthStyle.value,
  150. };
  151. });
  152. const extraContentStyle = computed((): CSSProperties => {
  153. const { collapseHeight, headerHeight } = props;
  154. return {
  155. height: `calc(100% - ${headerHeight + collapseHeight}px)`,
  156. };
  157. });
  158. const collapseStyle = computed((): CSSProperties => {
  159. return {
  160. height: `${props.collapseHeight}px`,
  161. };
  162. });
  163. watchEffect(() => {
  164. extraVisible.value = props.fixedExtra ? true : extraVisible.value;
  165. });
  166. function calcMenuWidthStyle(isHiddenDom: boolean): CSSProperties {
  167. const { extraWidth, fixedExtra, isSidebarMixed, show, width } = props;
  168. let widthValue =
  169. width === 0
  170. ? '0px'
  171. : `${width + (isSidebarMixed && fixedExtra && extraVisible.value ? extraWidth : 0)}px`;
  172. const { collapseWidth } = props;
  173. if (isHiddenDom && expandOnHovering.value && !expandOnHover.value) {
  174. widthValue = `${collapseWidth}px`;
  175. }
  176. return {
  177. ...(widthValue === '0px' ? { overflow: 'hidden' } : {}),
  178. flex: `0 0 ${widthValue}`,
  179. marginLeft: show ? 0 : `-${widthValue}`,
  180. maxWidth: widthValue,
  181. minWidth: widthValue,
  182. width: widthValue,
  183. };
  184. }
  185. function handleMouseenter(e: MouseEvent) {
  186. if (e?.offsetX < 10) {
  187. return;
  188. }
  189. // 未开启和未折叠状态不生效
  190. if (expandOnHover.value) {
  191. return;
  192. }
  193. if (!expandOnHovering.value) {
  194. collapse.value = false;
  195. }
  196. if (props.isSidebarMixed) {
  197. isLocked.value = true;
  198. }
  199. expandOnHovering.value = true;
  200. }
  201. function handleMouseleave() {
  202. emit('leave');
  203. if (props.isSidebarMixed) {
  204. isLocked.value = false;
  205. }
  206. if (expandOnHover.value) {
  207. return;
  208. }
  209. expandOnHovering.value = false;
  210. collapse.value = true;
  211. extraVisible.value = false;
  212. }
  213. </script>
  214. <template>
  215. <div
  216. v-if="domVisible"
  217. :class="theme"
  218. :style="hiddenSideStyle"
  219. class="h-full transition-all duration-150"
  220. ></div>
  221. <aside
  222. :class="[
  223. theme,
  224. {
  225. 'bg-sidebar-deep': isSidebarMixed,
  226. 'bg-sidebar border-border border-r': !isSidebarMixed,
  227. },
  228. ]"
  229. :style="style"
  230. class="fixed left-0 top-0 h-full transition-all duration-150"
  231. @mouseenter="handleMouseenter"
  232. @mouseleave="handleMouseleave"
  233. >
  234. <SidebarFixedButton
  235. v-if="!collapse && !isSidebarMixed"
  236. v-model:expand-on-hover="expandOnHover"
  237. />
  238. <div v-if="slots.logo" :style="headerStyle">
  239. <slot name="logo"></slot>
  240. </div>
  241. <VbenScrollbar :style="contentStyle" shadow shadow-border>
  242. <slot></slot>
  243. </VbenScrollbar>
  244. <div :style="collapseStyle"></div>
  245. <SidebarCollapseButton
  246. v-if="showCollapseButton && !isSidebarMixed"
  247. v-model:collapsed="collapse"
  248. />
  249. <div
  250. v-if="isSidebarMixed"
  251. ref="asideRef"
  252. :class="{
  253. 'border-l': extraVisible,
  254. }"
  255. :style="extraStyle"
  256. class="border-border bg-sidebar fixed top-0 h-full overflow-hidden border-r transition-all duration-200"
  257. >
  258. <SidebarCollapseButton
  259. v-if="isSidebarMixed && expandOnHover"
  260. v-model:collapsed="extraCollapse"
  261. />
  262. <SidebarFixedButton
  263. v-if="!extraCollapse"
  264. v-model:expand-on-hover="expandOnHover"
  265. />
  266. <div v-if="!extraCollapse" :style="extraTitleStyle" class="pl-2">
  267. <slot name="extra-title"></slot>
  268. </div>
  269. <VbenScrollbar
  270. :style="extraContentStyle"
  271. class="border-border py-2"
  272. shadow
  273. shadow-border
  274. >
  275. <slot name="extra"></slot>
  276. </VbenScrollbar>
  277. </div>
  278. </aside>
  279. </template>