layout-sidebar.vue 8.6 KB

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