vben-layout.vue 15 KB

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