vben-layout.vue 16 KB

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