vben-layout.vue 14 KB

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