vben-layout.vue 14 KB

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