vben-layout.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 { Menu } 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 mouseMove = () => {
  353. mouseY.value > headerWrapperHeight.value
  354. ? (headerIsHidden.value = true)
  355. : (headerIsHidden.value = false);
  356. };
  357. watch(
  358. [() => props.headerMode, () => mouseY.value],
  359. () => {
  360. if (!isHeaderAutoMode.value || isMixedNav.value || isFullContent.value) {
  361. if (props.headerMode !== 'auto-scroll') {
  362. headerIsHidden.value = false;
  363. }
  364. return;
  365. }
  366. headerIsHidden.value = true;
  367. mouseMove();
  368. },
  369. {
  370. immediate: true,
  371. },
  372. );
  373. }
  374. {
  375. const checkHeaderIsHidden = useThrottleFn((top, bottom, topArrived) => {
  376. if (scrollY.value < headerWrapperHeight.value) {
  377. headerIsHidden.value = false;
  378. return;
  379. }
  380. if (topArrived) {
  381. headerIsHidden.value = false;
  382. return;
  383. }
  384. if (top) {
  385. headerIsHidden.value = false;
  386. } else if (bottom) {
  387. headerIsHidden.value = true;
  388. }
  389. }, 300);
  390. watch(
  391. () => scrollY.value,
  392. () => {
  393. if (
  394. props.headerMode !== 'auto-scroll' ||
  395. isMixedNav.value ||
  396. isFullContent.value
  397. ) {
  398. return;
  399. }
  400. if (isScrolling.value) {
  401. checkHeaderIsHidden(
  402. directions.top,
  403. directions.bottom,
  404. arrivedState.top,
  405. );
  406. }
  407. },
  408. );
  409. }
  410. function handleClickMask() {
  411. sidebarCollapse.value = true;
  412. }
  413. function handleHeaderToggle() {
  414. if (props.isMobile) {
  415. sidebarCollapse.value = false;
  416. } else {
  417. emit('toggleSidebar');
  418. }
  419. }
  420. const idMainContent = ELEMENT_ID_MAIN_CONTENT;
  421. </script>
  422. <template>
  423. <div class="relative flex min-h-full w-full">
  424. <LayoutSidebar
  425. v-if="sidebarEnableState"
  426. v-model:collapse="sidebarCollapse"
  427. v-model:expand-on-hover="sidebarExpandOnHover"
  428. v-model:expand-on-hovering="sidebarExpandOnHovering"
  429. v-model:extra-collapse="sidebarExtraCollapse"
  430. v-model:extra-visible="sidebarExtraVisible"
  431. :show-collapse-button="sidebarCollapsedButton"
  432. :show-fixed-button="sidebarFixedButton"
  433. :collapse-width="getSideCollapseWidth"
  434. :dom-visible="!isMobile"
  435. :extra-width="sidebarExtraWidth"
  436. :fixed-extra="sidebarExpandOnHover"
  437. :header-height="isMixedNav ? 0 : headerHeight"
  438. :is-sidebar-mixed="isSidebarMixedNav || isHeaderMixedNav"
  439. :margin-top="sidebarMarginTop"
  440. :mixed-width="sidebarMixedWidth"
  441. :show="showSidebar"
  442. :theme="sidebarTheme"
  443. :width="getSidebarWidth"
  444. :z-index="sidebarZIndex"
  445. @leave="() => emit('sideMouseLeave')"
  446. >
  447. <template v-if="isSideMode && !isMixedNav" #logo>
  448. <slot name="logo"></slot>
  449. </template>
  450. <template v-if="isSidebarMixedNav || isHeaderMixedNav">
  451. <slot name="mixed-menu"></slot>
  452. </template>
  453. <template v-else>
  454. <slot name="menu"></slot>
  455. </template>
  456. <template #extra>
  457. <slot name="side-extra"></slot>
  458. </template>
  459. <template #extra-title>
  460. <slot name="side-extra-title"></slot>
  461. </template>
  462. </LayoutSidebar>
  463. <div
  464. ref="contentRef"
  465. class="flex flex-1 flex-col overflow-hidden transition-all duration-300 ease-in"
  466. >
  467. <div
  468. :class="[
  469. {
  470. 'shadow-[0_16px_24px_hsl(var(--background))]': scrollY > 20,
  471. },
  472. SCROLL_FIXED_CLASS,
  473. ]"
  474. :style="headerWrapperStyle"
  475. class="overflow-hidden transition-all duration-200"
  476. >
  477. <LayoutHeader
  478. v-if="headerVisible"
  479. :full-width="!isSideMode"
  480. :height="headerHeight"
  481. :is-mobile="isMobile"
  482. :show="!isFullContent && !headerHidden"
  483. :sidebar-width="sidebarWidth"
  484. :theme="headerTheme"
  485. :width="mainStyle.width"
  486. :z-index="headerZIndex"
  487. >
  488. <template v-if="showHeaderLogo" #logo>
  489. <slot name="logo"></slot>
  490. </template>
  491. <template #toggle-button>
  492. <VbenIconButton
  493. v-if="showHeaderToggleButton"
  494. class="my-0 mr-1 rounded-md"
  495. @click="handleHeaderToggle"
  496. >
  497. <Menu class="size-4" />
  498. </VbenIconButton>
  499. </template>
  500. <slot name="header"></slot>
  501. </LayoutHeader>
  502. <LayoutTabbar
  503. v-if="tabbarEnable"
  504. :height="tabbarHeight"
  505. :style="tabbarStyle"
  506. >
  507. <slot name="tabbar"></slot>
  508. </LayoutTabbar>
  509. </div>
  510. <!-- </div> -->
  511. <LayoutContent
  512. :id="idMainContent"
  513. :content-compact="contentCompact"
  514. :content-compact-width="contentCompactWidth"
  515. :padding="contentPadding"
  516. :padding-bottom="contentPaddingBottom"
  517. :padding-left="contentPaddingLeft"
  518. :padding-right="contentPaddingRight"
  519. :padding-top="contentPaddingTop"
  520. :style="contentStyle"
  521. class="transition-[margin-top] duration-200"
  522. >
  523. <slot name="content"></slot>
  524. <template #overlay>
  525. <slot name="content-overlay"></slot>
  526. </template>
  527. </LayoutContent>
  528. <LayoutFooter
  529. v-if="footerEnable"
  530. :fixed="footerFixed"
  531. :height="footerHeight"
  532. :show="!isFullContent"
  533. :width="footerWidth"
  534. :z-index="zIndex"
  535. >
  536. <slot name="footer"></slot>
  537. </LayoutFooter>
  538. </div>
  539. <slot name="extra"></slot>
  540. <div
  541. v-if="maskVisible"
  542. :style="maskStyle"
  543. class="bg-overlay fixed left-0 top-0 h-full w-full transition-[background-color] duration-200"
  544. @click="handleClickMask"
  545. ></div>
  546. </div>
  547. </template>