vben-layout.vue 14 KB

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