layout-sidebar.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 true
  64. */
  65. showCollapseButton?: boolean;
  66. /**
  67. * 显示固定按钮
  68. * @default true
  69. */
  70. showFixedButton?: boolean;
  71. /**
  72. * 主题
  73. */
  74. theme: string;
  75. /**
  76. * 子主题
  77. */
  78. themeSub: string;
  79. /**
  80. * 宽度
  81. */
  82. width: number;
  83. /**
  84. * zIndex
  85. * @default 0
  86. */
  87. zIndex?: number;
  88. }
  89. const props = withDefaults(defineProps<Props>(), {
  90. collapseHeight: 42,
  91. collapseWidth: 48,
  92. domVisible: true,
  93. fixedExtra: false,
  94. isSidebarMixed: false,
  95. marginTop: 0,
  96. mixedWidth: 70,
  97. paddingTop: 0,
  98. show: true,
  99. showCollapseButton: true,
  100. showFixedButton: true,
  101. zIndex: 0,
  102. });
  103. const emit = defineEmits<{ leave: [] }>();
  104. const collapse = defineModel<boolean>('collapse');
  105. const extraCollapse = defineModel<boolean>('extraCollapse');
  106. const expandOnHovering = defineModel<boolean>('expandOnHovering');
  107. const expandOnHover = defineModel<boolean>('expandOnHover');
  108. const extraVisible = defineModel<boolean>('extraVisible');
  109. const isLocked = useScrollLock(document.body);
  110. const slots = useSlots();
  111. // @ts-expect-error unused
  112. const asideRef = shallowRef<HTMLDivElement | null>();
  113. const hiddenSideStyle = computed((): CSSProperties => calcMenuWidthStyle(true));
  114. const style = computed((): CSSProperties => {
  115. const { isSidebarMixed, marginTop, paddingTop, zIndex } = props;
  116. return {
  117. '--scroll-shadow': 'var(--sidebar)',
  118. ...calcMenuWidthStyle(false),
  119. height: `calc(100% - ${marginTop}px)`,
  120. marginTop: `${marginTop}px`,
  121. paddingTop: `${paddingTop}px`,
  122. zIndex,
  123. ...(isSidebarMixed && extraVisible.value ? { transition: 'none' } : {}),
  124. };
  125. });
  126. const extraStyle = computed((): CSSProperties => {
  127. const { extraWidth, show, width, zIndex } = props;
  128. return {
  129. left: `${width}px`,
  130. width: extraVisible.value && show ? `${extraWidth}px` : 0,
  131. zIndex,
  132. };
  133. });
  134. const extraTitleStyle = computed((): CSSProperties => {
  135. const { headerHeight } = props;
  136. return {
  137. height: `${headerHeight - 1}px`,
  138. };
  139. });
  140. const contentWidthStyle = computed((): CSSProperties => {
  141. const { collapseWidth, fixedExtra, isSidebarMixed, mixedWidth } = props;
  142. if (isSidebarMixed && fixedExtra) {
  143. return { width: `${collapse.value ? collapseWidth : mixedWidth}px` };
  144. }
  145. return {};
  146. });
  147. const contentStyle = computed((): CSSProperties => {
  148. const { collapseHeight, headerHeight } = props;
  149. return {
  150. height: `calc(100% - ${headerHeight + collapseHeight}px)`,
  151. paddingTop: '8px',
  152. ...contentWidthStyle.value,
  153. };
  154. });
  155. const headerStyle = computed((): CSSProperties => {
  156. const { headerHeight, isSidebarMixed } = props;
  157. return {
  158. ...(isSidebarMixed ? { display: 'flex', justifyContent: 'center' } : {}),
  159. height: `${headerHeight - 1}px`,
  160. ...contentWidthStyle.value,
  161. };
  162. });
  163. const extraContentStyle = computed((): CSSProperties => {
  164. const { collapseHeight, headerHeight } = props;
  165. return {
  166. height: `calc(100% - ${headerHeight + collapseHeight}px)`,
  167. };
  168. });
  169. const collapseStyle = computed((): CSSProperties => {
  170. return {
  171. height: `${props.collapseHeight}px`,
  172. };
  173. });
  174. watchEffect(() => {
  175. extraVisible.value = props.fixedExtra ? true : extraVisible.value;
  176. });
  177. function calcMenuWidthStyle(isHiddenDom: boolean): CSSProperties {
  178. const { extraWidth, fixedExtra, isSidebarMixed, show, width } = props;
  179. let widthValue =
  180. width === 0
  181. ? '0px'
  182. : `${width + (isSidebarMixed && fixedExtra && extraVisible.value ? extraWidth : 0)}px`;
  183. const { collapseWidth } = props;
  184. if (isHiddenDom && expandOnHovering.value && !expandOnHover.value) {
  185. widthValue = `${collapseWidth}px`;
  186. }
  187. return {
  188. ...(widthValue === '0px' ? { overflow: 'hidden' } : {}),
  189. flex: `0 0 ${widthValue}`,
  190. marginLeft: show ? 0 : `-${widthValue}`,
  191. maxWidth: widthValue,
  192. minWidth: widthValue,
  193. width: widthValue,
  194. };
  195. }
  196. function handleMouseenter(e: MouseEvent) {
  197. if (e?.offsetX < 10) {
  198. return;
  199. }
  200. // 未开启和未折叠状态不生效
  201. if (expandOnHover.value) {
  202. return;
  203. }
  204. if (!expandOnHovering.value) {
  205. collapse.value = false;
  206. }
  207. if (props.isSidebarMixed) {
  208. isLocked.value = true;
  209. }
  210. expandOnHovering.value = true;
  211. }
  212. function handleMouseleave() {
  213. emit('leave');
  214. if (props.isSidebarMixed) {
  215. isLocked.value = false;
  216. }
  217. if (expandOnHover.value) {
  218. return;
  219. }
  220. expandOnHovering.value = false;
  221. collapse.value = true;
  222. extraVisible.value = false;
  223. }
  224. </script>
  225. <template>
  226. <div
  227. v-if="domVisible"
  228. :class="theme"
  229. :style="hiddenSideStyle"
  230. class="h-full transition-all duration-150"
  231. ></div>
  232. <aside
  233. :style="style"
  234. class="fixed left-0 top-0 h-full transition-all duration-150"
  235. @mouseenter="handleMouseenter"
  236. @mouseleave="handleMouseleave"
  237. >
  238. <div
  239. class="h-full"
  240. :class="[
  241. theme,
  242. {
  243. 'bg-sidebar-deep': isSidebarMixed,
  244. 'border-r border-border bg-sidebar': !isSidebarMixed,
  245. },
  246. ]"
  247. :style="{ width: `${width}px` }"
  248. >
  249. <SidebarFixedButton
  250. v-if="!collapse && !isSidebarMixed && showFixedButton"
  251. v-model:expand-on-hover="expandOnHover"
  252. />
  253. <div v-if="slots.logo" :style="headerStyle">
  254. <slot name="logo"></slot>
  255. </div>
  256. <VbenScrollbar :style="contentStyle" shadow shadow-border>
  257. <slot></slot>
  258. </VbenScrollbar>
  259. <div :style="collapseStyle"></div>
  260. <SidebarCollapseButton
  261. v-if="showCollapseButton && !isSidebarMixed"
  262. v-model:collapsed="collapse"
  263. />
  264. </div>
  265. <div
  266. v-if="isSidebarMixed"
  267. ref="asideRef"
  268. :class="[
  269. themeSub,
  270. {
  271. 'border-l': extraVisible,
  272. },
  273. ]"
  274. :style="extraStyle"
  275. class="fixed top-0 h-full overflow-hidden border-r border-border bg-sidebar transition-all duration-200"
  276. >
  277. <SidebarCollapseButton
  278. v-if="isSidebarMixed && expandOnHover"
  279. v-model:collapsed="extraCollapse"
  280. />
  281. <SidebarFixedButton
  282. v-if="!extraCollapse"
  283. v-model:expand-on-hover="expandOnHover"
  284. />
  285. <div v-if="!extraCollapse" :style="extraTitleStyle" class="pl-2">
  286. <slot name="extra-title"></slot>
  287. </div>
  288. <VbenScrollbar
  289. :style="extraContentStyle"
  290. class="border-border py-2"
  291. shadow
  292. shadow-border
  293. >
  294. <slot name="extra"></slot>
  295. </VbenScrollbar>
  296. </div>
  297. </aside>
  298. </template>