vben-layout.vue 14 KB

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