layout-sidebar.vue 7.2 KB

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