vben-layout.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <script setup lang="ts">
  2. import type { VbenLayoutProps } from './vben-layout';
  3. import type { CSSProperties } from 'vue';
  4. import { computed, ref, watch } from 'vue';
  5. import { Menu } from '@vben-core/icons';
  6. import { VbenIconButton } from '@vben-core/shadcn-ui';
  7. import { useMouse, useScroll, useThrottleFn } from '@vueuse/core';
  8. import {
  9. LayoutContent,
  10. LayoutFooter,
  11. LayoutHeader,
  12. LayoutSidebar,
  13. LayoutTabbar,
  14. } from './components';
  15. import { useLayout } from './hooks/use-layout';
  16. interface Props extends VbenLayoutProps {}
  17. defineOptions({
  18. name: 'VbenLayout',
  19. });
  20. const props = withDefaults(defineProps<Props>(), {
  21. contentCompact: 'wide',
  22. contentCompactWidth: 1200,
  23. contentPadding: 0,
  24. contentPaddingBottom: 0,
  25. contentPaddingLeft: 0,
  26. contentPaddingRight: 0,
  27. contentPaddingTop: 0,
  28. footerEnable: false,
  29. footerFixed: true,
  30. footerHeight: 32,
  31. headerHeight: 50,
  32. headerHidden: false,
  33. headerMode: 'fixed',
  34. headerToggleSidebarButton: true,
  35. headerVisible: true,
  36. isMobile: false,
  37. layout: 'sidebar-nav',
  38. sidebarCollapseShowTitle: false,
  39. sidebarExtraCollapsedWidth: 60,
  40. sidebarHidden: false,
  41. sidebarMixedWidth: 80,
  42. sidebarTheme: 'dark',
  43. sidebarWidth: 180,
  44. sideCollapseWidth: 60,
  45. tabbarEnable: true,
  46. tabbarHeight: 40,
  47. zIndex: 200,
  48. });
  49. const emit = defineEmits<{ sideMouseLeave: []; toggleSidebar: [] }>();
  50. const sidebarCollapse = defineModel<boolean>('sidebarCollapse');
  51. const sidebarExtraVisible = defineModel<boolean>('sidebarExtraVisible');
  52. const sidebarExtraCollapse = defineModel<boolean>('sidebarExtraCollapse');
  53. const sidebarExpandOnHover = defineModel<boolean>('sidebarExpandOnHover');
  54. const sidebarEnable = defineModel<boolean>('sidebarEnable', { default: true });
  55. // side是否处于hover状态展开菜单中
  56. const sidebarExpandOnHovering = ref(false);
  57. const headerIsHidden = ref(false);
  58. const contentRef = ref();
  59. const {
  60. arrivedState,
  61. directions,
  62. isScrolling,
  63. y: scrollY,
  64. } = useScroll(document);
  65. const { y: mouseY } = useMouse({ target: contentRef, type: 'client' });
  66. const {
  67. currentLayout,
  68. isFullContent,
  69. isHeaderNav,
  70. isMixedNav,
  71. isSidebarMixedNav,
  72. } = useLayout(props);
  73. /**
  74. * 顶栏是否自动隐藏
  75. */
  76. const isHeaderAutoMode = computed(() => props.headerMode === 'auto');
  77. const headerWrapperHeight = computed(() => {
  78. let height = 0;
  79. if (props.headerVisible && !props.headerHidden) {
  80. height += props.headerHeight;
  81. }
  82. if (props.tabbarEnable) {
  83. height += props.tabbarHeight;
  84. }
  85. return height;
  86. });
  87. const getSideCollapseWidth = computed(() => {
  88. const { sidebarCollapseShowTitle, sidebarMixedWidth, sideCollapseWidth } =
  89. props;
  90. return sidebarCollapseShowTitle || isSidebarMixedNav.value
  91. ? sidebarMixedWidth
  92. : sideCollapseWidth;
  93. });
  94. /**
  95. * 动态获取侧边区域是否可见
  96. */
  97. const sidebarEnableState = computed(() => {
  98. return !isHeaderNav.value && sidebarEnable.value;
  99. });
  100. /**
  101. * 侧边区域离顶部高度
  102. */
  103. const sidebarMarginTop = computed(() => {
  104. const { headerHeight, isMobile } = props;
  105. return isMixedNav.value && !isMobile ? headerHeight : 0;
  106. });
  107. /**
  108. * 动态获取侧边宽度
  109. */
  110. const getSidebarWidth = computed(() => {
  111. const { isMobile, sidebarHidden, sidebarMixedWidth, sidebarWidth } = props;
  112. let width = 0;
  113. if (sidebarHidden) {
  114. return width;
  115. }
  116. if (
  117. !sidebarEnableState.value ||
  118. (sidebarHidden && !isSidebarMixedNav.value && !isMixedNav.value)
  119. ) {
  120. return width;
  121. }
  122. if (isSidebarMixedNav.value && !isMobile) {
  123. width = sidebarMixedWidth;
  124. } else if (sidebarCollapse.value) {
  125. width = isMobile ? 0 : getSideCollapseWidth.value;
  126. } else {
  127. width = sidebarWidth;
  128. }
  129. return width;
  130. });
  131. /**
  132. * 获取扩展区域宽度
  133. */
  134. const sidebarExtraWidth = computed(() => {
  135. const { sidebarExtraCollapsedWidth, sidebarWidth } = props;
  136. return sidebarExtraCollapse.value ? sidebarExtraCollapsedWidth : sidebarWidth;
  137. });
  138. /**
  139. * 是否侧边栏模式,包含混合侧边
  140. */
  141. const isSideMode = computed(
  142. () =>
  143. currentLayout.value === 'mixed-nav' ||
  144. currentLayout.value === 'sidebar-mixed-nav' ||
  145. currentLayout.value === 'sidebar-nav',
  146. );
  147. /**
  148. * header fixed值
  149. */
  150. const headerFixed = computed(() => {
  151. const { headerMode } = props;
  152. return (
  153. isMixedNav.value ||
  154. headerMode === 'fixed' ||
  155. headerMode === 'auto-scroll' ||
  156. headerMode === 'auto'
  157. );
  158. });
  159. const showSidebar = computed(() => {
  160. return isSideMode.value && sidebarEnable.value;
  161. });
  162. /**
  163. * 遮罩可见性
  164. */
  165. const maskVisible = computed(() => !sidebarCollapse.value && props.isMobile);
  166. const mainStyle = computed(() => {
  167. let width = '100%';
  168. let sidebarAndExtraWidth = 'unset';
  169. if (
  170. headerFixed.value &&
  171. currentLayout.value !== 'header-nav' &&
  172. currentLayout.value !== 'mixed-nav' &&
  173. showSidebar.value &&
  174. !props.isMobile
  175. ) {
  176. // fixed模式下生效
  177. const isSideNavEffective =
  178. isSidebarMixedNav.value &&
  179. sidebarExpandOnHover.value &&
  180. sidebarExtraVisible.value;
  181. if (isSideNavEffective) {
  182. const sideCollapseWidth = sidebarCollapse.value
  183. ? getSideCollapseWidth.value
  184. : props.sidebarMixedWidth;
  185. const sideWidth = sidebarExtraCollapse.value
  186. ? props.sidebarExtraCollapsedWidth
  187. : props.sidebarWidth;
  188. // 100% - 侧边菜单混合宽度 - 菜单宽度
  189. sidebarAndExtraWidth = `${sideCollapseWidth + sideWidth}px`;
  190. width = `calc(100% - ${sidebarAndExtraWidth})`;
  191. } else {
  192. sidebarAndExtraWidth =
  193. sidebarExpandOnHovering.value && !sidebarExpandOnHover.value
  194. ? `${getSideCollapseWidth.value}px`
  195. : `${getSidebarWidth.value}px`;
  196. width = `calc(100% - ${sidebarAndExtraWidth})`;
  197. }
  198. }
  199. return {
  200. sidebarAndExtraWidth,
  201. width,
  202. };
  203. });
  204. // 计算 tabbar 的样式
  205. const tabbarStyle = computed((): CSSProperties => {
  206. let width = '';
  207. let marginLeft = 0;
  208. // 如果不是混合导航,tabbar 的宽度为 100%
  209. if (!isMixedNav.value || props.sidebarHidden) {
  210. width = '100%';
  211. } else if (sidebarEnable.value) {
  212. // 鼠标在侧边栏上时,且侧边栏展开时的宽度
  213. const onHoveringWidth = sidebarExpandOnHover.value
  214. ? props.sidebarWidth
  215. : getSideCollapseWidth.value;
  216. // 设置 marginLeft,根据侧边栏是否折叠来决定
  217. marginLeft = sidebarCollapse.value
  218. ? getSideCollapseWidth.value
  219. : onHoveringWidth;
  220. // 设置 tabbar 的宽度,计算方式为 100% 减去侧边栏的宽度
  221. width = `calc(100% - ${sidebarCollapse.value ? getSidebarWidth.value : onHoveringWidth}px)`;
  222. } else {
  223. // 默认情况下,tabbar 的宽度为 100%
  224. width = '100%';
  225. }
  226. return {
  227. marginLeft: `${marginLeft}px`,
  228. width,
  229. };
  230. });
  231. const contentStyle = computed((): CSSProperties => {
  232. const fixed = headerFixed.value;
  233. const { footerEnable, footerFixed, footerHeight } = props;
  234. return {
  235. marginTop:
  236. fixed &&
  237. !isFullContent.value &&
  238. !headerIsHidden.value &&
  239. (!isHeaderAutoMode.value || scrollY.value < headerWrapperHeight.value)
  240. ? `${headerWrapperHeight.value}px`
  241. : 0,
  242. paddingBottom: `${footerEnable && footerFixed ? footerHeight : 0}px`,
  243. };
  244. });
  245. const headerZIndex = computed(() => {
  246. const { zIndex } = props;
  247. const offset = isMixedNav.value ? 1 : 0;
  248. return zIndex + offset;
  249. });
  250. const headerWrapperStyle = computed((): CSSProperties => {
  251. const fixed = headerFixed.value;
  252. return {
  253. height: isFullContent.value ? '0' : `${headerWrapperHeight.value}px`,
  254. left: isMixedNav.value ? 0 : mainStyle.value.sidebarAndExtraWidth,
  255. position: fixed ? 'fixed' : 'static',
  256. top:
  257. headerIsHidden.value || isFullContent.value
  258. ? `-${headerWrapperHeight.value}px`
  259. : 0,
  260. width: mainStyle.value.width,
  261. 'z-index': headerZIndex.value,
  262. };
  263. });
  264. /**
  265. * 侧边栏z-index
  266. */
  267. const sidebarZIndex = computed(() => {
  268. const { isMobile, zIndex } = props;
  269. let offset = isMobile || isSideMode.value ? 1 : -1;
  270. if (isMixedNav.value) {
  271. offset += 1;
  272. }
  273. return zIndex + offset;
  274. });
  275. const footerWidth = computed(() => {
  276. if (!props.footerFixed) {
  277. return '100%';
  278. }
  279. return mainStyle.value.width;
  280. });
  281. const maskStyle = computed((): CSSProperties => {
  282. return { zIndex: props.zIndex };
  283. });
  284. const showHeaderToggleButton = computed(() => {
  285. return (
  286. props.isMobile ||
  287. (props.headerToggleSidebarButton &&
  288. isSideMode.value &&
  289. !isSidebarMixedNav.value &&
  290. !isMixedNav.value &&
  291. !props.isMobile)
  292. );
  293. });
  294. const showHeaderLogo = computed(() => {
  295. return !isSideMode.value || isMixedNav.value || props.isMobile;
  296. });
  297. watch(
  298. () => props.isMobile,
  299. (val) => {
  300. if (val) {
  301. sidebarCollapse.value = true;
  302. }
  303. },
  304. {
  305. immediate: true,
  306. },
  307. );
  308. {
  309. const mouseMove = () => {
  310. mouseY.value > headerWrapperHeight.value
  311. ? (headerIsHidden.value = true)
  312. : (headerIsHidden.value = false);
  313. };
  314. watch(
  315. [() => props.headerMode, () => mouseY.value],
  316. () => {
  317. if (!isHeaderAutoMode.value || isMixedNav.value || isFullContent.value) {
  318. if (props.headerMode !== 'auto-scroll') {
  319. headerIsHidden.value = false;
  320. }
  321. return;
  322. }
  323. headerIsHidden.value = true;
  324. mouseMove();
  325. },
  326. {
  327. immediate: true,
  328. },
  329. );
  330. }
  331. {
  332. const checkHeaderIsHidden = useThrottleFn((top, bottom, topArrived) => {
  333. if (scrollY.value < headerWrapperHeight.value) {
  334. headerIsHidden.value = false;
  335. return;
  336. }
  337. if (topArrived) {
  338. headerIsHidden.value = false;
  339. return;
  340. }
  341. if (top) {
  342. headerIsHidden.value = false;
  343. } else if (bottom) {
  344. headerIsHidden.value = true;
  345. }
  346. }, 300);
  347. watch(
  348. () => scrollY.value,
  349. () => {
  350. if (
  351. props.headerMode !== 'auto-scroll' ||
  352. isMixedNav.value ||
  353. isFullContent.value
  354. ) {
  355. return;
  356. }
  357. if (isScrolling.value) {
  358. checkHeaderIsHidden(
  359. directions.top,
  360. directions.bottom,
  361. arrivedState.top,
  362. );
  363. }
  364. },
  365. );
  366. }
  367. function handleClickMask() {
  368. sidebarCollapse.value = true;
  369. }
  370. function handleHeaderToggle() {
  371. if (props.isMobile) {
  372. sidebarCollapse.value = false;
  373. } else {
  374. emit('toggleSidebar');
  375. }
  376. }
  377. </script>
  378. <template>
  379. <div class="relative flex min-h-full w-full">
  380. <LayoutSidebar
  381. v-if="sidebarEnableState"
  382. v-model:collapse="sidebarCollapse"
  383. v-model:expand-on-hover="sidebarExpandOnHover"
  384. v-model:expand-on-hovering="sidebarExpandOnHovering"
  385. v-model:extra-collapse="sidebarExtraCollapse"
  386. v-model:extra-visible="sidebarExtraVisible"
  387. :collapse-width="getSideCollapseWidth"
  388. :dom-visible="!isMobile"
  389. :extra-width="sidebarExtraWidth"
  390. :fixed-extra="sidebarExpandOnHover"
  391. :header-height="isMixedNav ? 0 : headerHeight"
  392. :is-sidebar-mixed="isSidebarMixedNav"
  393. :margin-top="sidebarMarginTop"
  394. :mixed-width="sidebarMixedWidth"
  395. :show="showSidebar"
  396. :theme="sidebarTheme"
  397. :width="getSidebarWidth"
  398. :z-index="sidebarZIndex"
  399. @leave="() => emit('sideMouseLeave')"
  400. >
  401. <template v-if="isSideMode && !isMixedNav" #logo>
  402. <slot name="logo"></slot>
  403. </template>
  404. <template v-if="isSidebarMixedNav">
  405. <slot name="mixed-menu"></slot>
  406. </template>
  407. <template v-else>
  408. <slot name="menu"></slot>
  409. </template>
  410. <template #extra>
  411. <slot name="side-extra"></slot>
  412. </template>
  413. <template #extra-title>
  414. <slot name="side-extra-title"></slot>
  415. </template>
  416. </LayoutSidebar>
  417. <div
  418. ref="contentRef"
  419. class="flex flex-1 flex-col overflow-hidden transition-all duration-300 ease-in"
  420. >
  421. <div
  422. :class="{
  423. 'shadow-[0_16px_24px_hsl(var(--background))]': scrollY > 20,
  424. }"
  425. :style="headerWrapperStyle"
  426. class="overflow-hidden transition-all duration-200"
  427. >
  428. <LayoutHeader
  429. v-if="headerVisible"
  430. :full-width="!isSideMode"
  431. :height="headerHeight"
  432. :is-mobile="isMobile"
  433. :show="!isFullContent && !headerHidden"
  434. :sidebar-width="sidebarWidth"
  435. :theme="headerTheme"
  436. :width="mainStyle.width"
  437. :z-index="headerZIndex"
  438. >
  439. <template v-if="showHeaderLogo" #logo>
  440. <slot name="logo"></slot>
  441. </template>
  442. <template #toggle-button>
  443. <VbenIconButton
  444. v-if="showHeaderToggleButton"
  445. class="my-0 ml-2 mr-1 rounded-md"
  446. @click="handleHeaderToggle"
  447. >
  448. <Menu class="size-4" />
  449. </VbenIconButton>
  450. </template>
  451. <slot name="header"></slot>
  452. </LayoutHeader>
  453. <LayoutTabbar
  454. v-if="tabbarEnable"
  455. :height="tabbarHeight"
  456. :style="tabbarStyle"
  457. >
  458. <slot name="tabbar"></slot>
  459. </LayoutTabbar>
  460. </div>
  461. <!-- </div> -->
  462. <LayoutContent
  463. :content-compact="contentCompact"
  464. :content-compact-width="contentCompactWidth"
  465. :padding="contentPadding"
  466. :padding-bottom="contentPaddingBottom"
  467. :padding-left="contentPaddingLeft"
  468. :padding-right="contentPaddingRight"
  469. :padding-top="contentPaddingTop"
  470. :style="contentStyle"
  471. class="transition-[margin-top] duration-200"
  472. >
  473. <slot name="content"></slot>
  474. <template #overlay>
  475. <slot name="content-overlay"></slot>
  476. </template>
  477. </LayoutContent>
  478. <LayoutFooter
  479. v-if="footerEnable"
  480. :fixed="footerFixed"
  481. :height="footerHeight"
  482. :show="!isFullContent"
  483. :width="footerWidth"
  484. :z-index="zIndex"
  485. >
  486. <slot name="footer"></slot>
  487. </LayoutFooter>
  488. </div>
  489. <slot name="extra"></slot>
  490. <div
  491. v-if="maskVisible"
  492. :style="maskStyle"
  493. class="bg-overlay fixed left-0 top-0 h-full w-full transition-[background-color] duration-200"
  494. @click="handleClickMask"
  495. ></div>
  496. </div>
  497. </template>