vben-layout.vue 14 KB

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