layout-sidebar.vue 6.9 KB

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