vben-layout.vue 14 KB

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