admin-vben/packages/@core/ui-kit/layout-ui/src/vben-layout.vue

579 lines
14 KiB
Vue
Raw Normal View History

2024-05-19 13:20:42 +00:00
<script setup lang="ts">
import type { CSSProperties } from 'vue';
2024-06-08 11:49:06 +00:00
import { computed, ref, watch } from 'vue';
2024-05-19 13:20:42 +00:00
import { useMouse, useScroll, useThrottleFn } from '@vueuse/core';
import {
LayoutContent,
LayoutFooter,
LayoutHeader,
2024-06-09 07:39:11 +00:00
LayoutSidebar,
LayoutTabbar,
2024-05-19 13:20:42 +00:00
} from './components';
import { VbenLayoutProps } from './vben-layout';
interface Props extends VbenLayoutProps {}
defineOptions({
name: 'VbenLayout',
});
const props = withDefaults(defineProps<Props>(), {
contentCompact: 'wide',
contentCompactWidth: 1200,
2024-05-19 13:20:42 +00:00
contentPadding: 0,
contentPaddingBottom: 0,
contentPaddingLeft: 0,
contentPaddingRight: 0,
contentPaddingTop: 0,
2024-06-09 05:31:43 +00:00
footerEnable: false,
2024-05-19 13:20:42 +00:00
footerFixed: true,
footerHeight: 32,
headerHeight: 50,
headerHeightOffset: 10,
2024-06-01 14:17:52 +00:00
headerHidden: false,
2024-05-19 13:20:42 +00:00
headerMode: 'fixed',
headerToggleSidebarButton: true,
2024-05-19 13:20:42 +00:00
headerVisible: true,
isMobile: false,
2024-06-09 07:39:11 +00:00
layout: 'sidebar-nav',
sidebarCollapseShowTitle: false,
sidebarExtraCollapsedWidth: 60,
2024-06-09 07:39:11 +00:00
sidebarHidden: false,
sidebarMixedWidth: 80,
sidebarSemiDark: true,
sidebarTheme: 'dark',
sidebarWidth: 180,
sideCollapseWidth: 60,
2024-06-09 07:39:11 +00:00
tabbarEnable: true,
2024-07-18 13:31:34 +00:00
tabbarHeight: 36,
2024-05-19 13:20:42 +00:00
zIndex: 200,
});
2024-06-09 07:39:11 +00:00
const emit = defineEmits<{ sideMouseLeave: []; toggleSidebar: [] }>();
const sidebarCollapse = defineModel<boolean>('sidebarCollapse');
const sidebarExtraVisible = defineModel<boolean>('sidebarExtraVisible');
const sidebarExtraCollapse = defineModel<boolean>('sidebarExtraCollapse');
const sidebarExpandOnHover = defineModel<boolean>('sidebarExpandOnHover');
const sidebarEnable = defineModel<boolean>('sidebarEnable', { default: true });
2024-05-19 13:20:42 +00:00
const {
arrivedState,
directions,
isScrolling,
y: scrollY,
} = useScroll(document);
2024-05-19 13:20:42 +00:00
const { y: mouseY } = useMouse({ type: 'client' });
// side是否处于hover状态展开菜单中
2024-06-09 07:39:11 +00:00
const sidebarExpandOnHovering = ref(false);
2024-05-19 13:20:42 +00:00
const headerIsHidden = ref(false);
const realLayout = computed(() =>
props.isMobile ? 'sidebar-nav' : props.layout,
);
2024-05-19 13:20:42 +00:00
/**
* 是否全屏显示content不需要侧边底部顶部tab区域
*/
const fullContent = computed(() => realLayout.value === 'full-content');
/**
* 是否侧边混合模式
*/
2024-06-09 07:39:11 +00:00
const isSidebarMixedNav = computed(
() => realLayout.value === 'sidebar-mixed-nav',
);
2024-05-19 13:20:42 +00:00
/**
* 是否为头部导航模式
*/
const isHeaderNav = computed(() => realLayout.value === 'header-nav');
/**
* 是否为混合导航模式
*/
const isMixedNav = computed(() => realLayout.value === 'mixed-nav');
/**
* 顶栏是否自动隐藏
*/
const isHeaderAutoMode = computed(() => props.headerMode === 'auto');
2024-05-19 13:20:42 +00:00
/**
* header区域高度
*/
const getHeaderHeight = computed(() => {
2024-06-01 14:17:52 +00:00
const { headerHeight, headerHeightOffset } = props;
2024-05-19 13:20:42 +00:00
2024-06-01 14:17:52 +00:00
// if (!headerVisible) {
// return 0;
// }
2024-05-19 13:20:42 +00:00
// 顶部存在导航时增加10
const offset = isMixedNav.value || isHeaderNav.value ? headerHeightOffset : 0;
return headerHeight + offset;
});
const headerWrapperHeight = computed(() => {
let height = 0;
2024-06-01 14:17:52 +00:00
if (props.headerVisible && !props.headerHidden) {
2024-05-19 13:20:42 +00:00
height += getHeaderHeight.value;
}
2024-06-09 07:39:11 +00:00
if (props.tabbarEnable) {
height += props.tabbarHeight;
2024-05-19 13:20:42 +00:00
}
return height;
});
const getSideCollapseWidth = computed(() => {
const { sidebarCollapseShowTitle, sidebarMixedWidth, sideCollapseWidth } =
2024-06-09 07:39:11 +00:00
props;
2024-06-30 14:58:57 +00:00
2024-06-09 07:39:11 +00:00
return sidebarCollapseShowTitle || isSidebarMixedNav.value
? sidebarMixedWidth
2024-05-19 13:20:42 +00:00
: sideCollapseWidth;
});
/**
* 动态获取侧边区域是否可见
*/
2024-06-09 07:39:11 +00:00
const sidebarEnableState = computed(() => {
return !isHeaderNav.value && sidebarEnable.value;
2024-05-19 13:20:42 +00:00
});
/**
* 侧边区域离顶部高度
*/
const sidebarMarginTop = computed(() => {
2024-05-19 13:20:42 +00:00
const { isMobile } = props;
return isMixedNav.value && !isMobile ? getHeaderHeight.value : 0;
});
/**
* 动态获取侧边宽度
*/
2024-06-09 07:39:11 +00:00
const getSidebarWidth = computed(() => {
const { isMobile, sidebarHidden, sidebarMixedWidth, sidebarWidth } = props;
2024-05-19 13:20:42 +00:00
let width = 0;
2024-06-09 07:39:11 +00:00
if (sidebarHidden) {
2024-06-01 14:17:52 +00:00
return width;
}
2024-05-19 13:20:42 +00:00
if (
2024-06-09 07:39:11 +00:00
!sidebarEnableState.value ||
(sidebarHidden && !isSidebarMixedNav.value && !isMixedNav.value)
2024-05-19 13:20:42 +00:00
) {
return width;
}
2024-06-09 07:39:11 +00:00
if (isSidebarMixedNav.value && !isMobile) {
width = sidebarMixedWidth;
} else if (sidebarCollapse.value) {
2024-05-19 13:20:42 +00:00
width = isMobile ? 0 : getSideCollapseWidth.value;
} else {
2024-06-09 07:39:11 +00:00
width = sidebarWidth;
2024-05-19 13:20:42 +00:00
}
return width;
});
/**
* 获取扩展区域宽度
*/
const sidebarExtraWidth = computed(() => {
const { sidebarExtraCollapsedWidth, sidebarWidth } = props;
2024-06-30 14:58:57 +00:00
return sidebarExtraCollapse.value ? sidebarExtraCollapsedWidth : sidebarWidth;
2024-05-19 13:20:42 +00:00
});
/**
* 是否侧边栏模式包含混合侧边
*/
const isSideMode = computed(() =>
2024-06-09 07:39:11 +00:00
['mixed-nav', 'sidebar-mixed-nav', 'sidebar-nav'].includes(realLayout.value),
2024-05-19 13:20:42 +00:00
);
2024-06-09 07:39:11 +00:00
const showSidebar = computed(() => {
2024-06-01 14:17:52 +00:00
// if (isMixedNav.value && !props.sideHidden) {
// return false;
// }
2024-06-09 07:39:11 +00:00
return isSideMode.value && sidebarEnable.value;
2024-05-19 13:20:42 +00:00
});
2024-06-09 07:39:11 +00:00
const sidebarFace = computed(() => {
const { sidebarSemiDark, sidebarTheme } = props;
const isDark = sidebarTheme === 'dark' || sidebarSemiDark;
2024-05-19 13:20:42 +00:00
return {
theme: isDark ? 'dark' : 'light',
};
});
/**
* 遮罩可见性
*/
2024-06-09 07:39:11 +00:00
const maskVisible = computed(() => !sidebarCollapse.value && props.isMobile);
2024-05-19 13:20:42 +00:00
/**
* header fixed值
*/
const headerFixed = computed(() => {
2024-07-16 14:07:28 +00:00
const { headerMode } = props;
2024-05-19 13:20:42 +00:00
return (
isMixedNav.value ||
2024-07-16 14:07:28 +00:00
headerMode === 'fixed' ||
headerMode === 'auto-scroll' ||
headerMode === 'auto'
2024-05-19 13:20:42 +00:00
);
});
const mainStyle = computed(() => {
let width = '100%';
2024-06-09 07:39:11 +00:00
let sidebarAndExtraWidth = 'unset';
2024-05-19 13:20:42 +00:00
if (
headerFixed.value &&
2024-07-16 14:07:28 +00:00
realLayout.value !== 'header-nav' &&
realLayout.value !== 'mixed-nav' &&
2024-06-09 07:39:11 +00:00
showSidebar.value &&
2024-05-19 13:20:42 +00:00
!props.isMobile
) {
2024-06-09 07:39:11 +00:00
// fixed模式下生效
2024-05-19 13:20:42 +00:00
const isSideNavEffective =
2024-06-09 07:39:11 +00:00
isSidebarMixedNav.value &&
sidebarExpandOnHover.value &&
sidebarExtraVisible.value;
2024-05-19 13:20:42 +00:00
if (isSideNavEffective) {
2024-06-09 07:39:11 +00:00
const sideCollapseWidth = sidebarCollapse.value
2024-05-19 13:20:42 +00:00
? getSideCollapseWidth.value
2024-06-09 07:39:11 +00:00
: props.sidebarMixedWidth;
const sideWidth = sidebarExtraCollapse.value
2024-05-19 13:20:42 +00:00
? getSideCollapseWidth.value
2024-06-09 07:39:11 +00:00
: props.sidebarWidth;
2024-05-19 13:20:42 +00:00
// 100% - 侧边菜单混合宽度 - 菜单宽度
2024-06-09 07:39:11 +00:00
sidebarAndExtraWidth = `${sideCollapseWidth + sideWidth}px`;
width = `calc(100% - ${sidebarAndExtraWidth})`;
2024-05-19 13:20:42 +00:00
} else {
2024-06-09 07:39:11 +00:00
sidebarAndExtraWidth =
sidebarExpandOnHovering.value && !sidebarExpandOnHover.value
2024-05-19 13:20:42 +00:00
? `${getSideCollapseWidth.value}px`
2024-06-09 07:39:11 +00:00
: `${getSidebarWidth.value}px`;
width = `calc(100% - ${sidebarAndExtraWidth})`;
2024-05-19 13:20:42 +00:00
}
}
return {
2024-06-09 07:39:11 +00:00
sidebarAndExtraWidth,
2024-05-19 13:20:42 +00:00
width,
};
});
// 计算 tabbar 的样式
2024-06-09 07:39:11 +00:00
const tabbarStyle = computed((): CSSProperties => {
2024-05-19 13:20:42 +00:00
let width = '';
let marginLeft = 0;
// 如果不是混合导航tabbar 的宽度为 100%
2024-05-19 13:20:42 +00:00
if (!isMixedNav.value) {
width = '100%';
2024-06-09 07:39:11 +00:00
} else if (sidebarEnable.value) {
// 鼠标在侧边栏上时,且侧边栏展开时的宽度
const onHoveringWidth = sidebarExpandOnHover.value
? props.sidebarWidth
: getSideCollapseWidth.value;
// 设置 marginLeft根据侧边栏是否折叠来决定
2024-06-09 07:39:11 +00:00
marginLeft = sidebarCollapse.value
2024-05-19 13:20:42 +00:00
? getSideCollapseWidth.value
: onHoveringWidth;
2024-07-15 16:14:24 +00:00
// 设置 tabbar 的宽度,计算方式为 100% 减去侧边栏的宽度
width = `calc(100% - ${sidebarCollapse.value ? getSidebarWidth.value : onHoveringWidth}px)`;
2024-05-19 13:20:42 +00:00
} else {
// 默认情况下tabbar 的宽度为 100%
2024-05-19 13:20:42 +00:00
width = '100%';
}
return {
marginLeft: `${marginLeft}px`,
width,
};
});
const contentStyle = computed((): CSSProperties => {
const fixed = headerFixed.value;
const { footerEnable, footerFixed, footerHeight } = props;
2024-05-19 13:20:42 +00:00
return {
marginTop:
fixed &&
!fullContent.value &&
!headerIsHidden.value &&
(!isHeaderAutoMode.value || scrollY.value < headerWrapperHeight.value)
2024-05-19 13:20:42 +00:00
? `${headerWrapperHeight.value}px`
: 0,
paddingBottom: `${footerEnable && footerFixed ? footerHeight : 0}px`,
2024-05-19 13:20:42 +00:00
};
});
const headerZIndex = computed(() => {
const { zIndex } = props;
const offset = isMixedNav.value ? 1 : 0;
return zIndex + offset;
});
const headerWrapperStyle = computed((): CSSProperties => {
const fixed = headerFixed.value;
return {
height: fullContent.value ? '0' : `${headerWrapperHeight.value}px`,
2024-06-09 07:39:11 +00:00
left: isMixedNav.value ? 0 : mainStyle.value.sidebarAndExtraWidth,
2024-05-19 13:20:42 +00:00
position: fixed ? 'fixed' : 'static',
top:
headerIsHidden.value || fullContent.value
? `-${headerWrapperHeight.value}px`
: 0,
width: mainStyle.value.width,
'z-index': headerZIndex.value,
};
});
/**
* 侧边栏z-index
*/
2024-06-09 07:39:11 +00:00
const sidebarZIndex = computed(() => {
2024-05-19 13:20:42 +00:00
const { isMobile, zIndex } = props;
let offset = isMobile || isSideMode.value ? 1 : -1;
if (isMixedNav.value) {
offset += 1;
}
2024-05-19 13:20:42 +00:00
return zIndex + offset;
});
2024-06-09 07:39:11 +00:00
const footerWidth = computed(() => {
if (!props.footerFixed) {
return '100%';
}
return mainStyle.value.width;
});
2024-05-19 13:20:42 +00:00
const maskStyle = computed((): CSSProperties => {
2024-06-09 07:39:11 +00:00
return { zIndex: props.zIndex };
2024-05-19 13:20:42 +00:00
});
const showHeaderToggleButton = computed(() => {
return (
props.headerToggleSidebarButton &&
2024-05-19 13:20:42 +00:00
isSideMode.value &&
2024-06-09 07:39:11 +00:00
!isSidebarMixedNav.value &&
2024-05-19 13:20:42 +00:00
!isMixedNav.value &&
!props.isMobile
);
});
const showHeaderLogo = computed(() => {
return !isSideMode.value || isMixedNav.value || props.isMobile;
});
watch(
() => props.isMobile,
(val) => {
if (val) {
sidebarCollapse.value = true;
}
},
{
immediate: true,
2024-05-19 13:20:42 +00:00
},
);
{
const mouseMove = () => {
mouseY.value > headerWrapperHeight.value
? (headerIsHidden.value = true)
: (headerIsHidden.value = false);
};
watch(
[() => props.headerMode, () => mouseY.value],
() => {
if (!isHeaderAutoMode.value || isMixedNav.value || fullContent.value) {
2024-05-19 13:20:42 +00:00
return;
}
headerIsHidden.value = true;
mouseMove();
},
{
immediate: true,
},
);
}
{
const checkHeaderIsHidden = useThrottleFn((top, bottom, topArrived) => {
if (scrollY.value < headerWrapperHeight.value) {
headerIsHidden.value = false;
return;
}
if (topArrived) {
headerIsHidden.value = false;
return;
}
if (top) {
headerIsHidden.value = false;
} else if (bottom) {
headerIsHidden.value = true;
}
}, 300);
watch(
() => scrollY.value,
() => {
if (
props.headerMode !== 'auto-scroll' ||
isMixedNav.value ||
fullContent.value
) {
return;
}
if (isScrolling.value) {
checkHeaderIsHidden(
directions.top,
directions.bottom,
arrivedState.top,
);
}
},
);
}
function handleClickMask() {
2024-06-09 07:39:11 +00:00
sidebarCollapse.value = true;
2024-05-19 13:20:42 +00:00
}
function handleOpenMenu() {
2024-06-09 07:39:11 +00:00
sidebarCollapse.value = false;
2024-05-19 13:20:42 +00:00
}
</script>
<template>
2024-05-28 15:38:36 +00:00
<div class="relative flex min-h-full w-full">
2024-06-01 15:15:29 +00:00
<slot name="preferences"></slot>
2024-06-09 07:39:11 +00:00
<slot name="floating-groups"></slot>
<LayoutSidebar
v-if="sidebarEnableState"
v-model:collapse="sidebarCollapse"
v-model:expand-on-hover="sidebarExpandOnHover"
v-model:expand-on-hovering="sidebarExpandOnHovering"
v-model:extra-collapse="sidebarExtraCollapse"
v-model:extra-visible="sidebarExtraVisible"
2024-06-09 05:31:43 +00:00
:collapse-width="getSideCollapseWidth"
2024-05-19 13:20:42 +00:00
:dom-visible="!isMobile"
:extra-width="sidebarExtraWidth"
2024-06-09 07:39:11 +00:00
:fixed-extra="sidebarExpandOnHover"
2024-05-19 13:20:42 +00:00
:header-height="isMixedNav ? 0 : getHeaderHeight"
2024-06-09 07:39:11 +00:00
:is-sidebar-mixed="isSidebarMixedNav"
:margin-top="sidebarMarginTop"
2024-06-09 07:39:11 +00:00
:mixed-width="sidebarMixedWidth"
:show="showSidebar"
:theme="sidebarFace.theme"
2024-06-09 07:39:11 +00:00
:width="getSidebarWidth"
:z-index="sidebarZIndex"
2024-05-19 13:20:42 +00:00
@leave="() => emit('sideMouseLeave')"
>
<template v-if="isSideMode && !isMixedNav" #logo>
<slot name="logo"></slot>
</template>
2024-06-09 07:39:11 +00:00
<template v-if="isSidebarMixedNav">
2024-05-19 13:20:42 +00:00
<slot name="mixed-menu"></slot>
</template>
<template v-else>
<slot name="menu"></slot>
</template>
<template #extra>
<slot name="side-extra"></slot>
</template>
<template #extra-title>
<slot name="side-extra-title"></slot>
</template>
2024-06-09 07:39:11 +00:00
</LayoutSidebar>
2024-05-19 13:20:42 +00:00
2024-05-28 15:38:36 +00:00
<div
class="flex flex-1 flex-col overflow-hidden transition-all duration-300 ease-in"
>
<div
:style="headerWrapperStyle"
2024-06-01 14:17:52 +00:00
class="overflow-hidden transition-all duration-200"
2024-05-28 15:38:36 +00:00
>
2024-05-19 13:20:42 +00:00
<LayoutHeader
v-if="headerVisible"
:full-width="!isSideMode"
:height="getHeaderHeight"
2024-06-09 05:31:43 +00:00
:is-mixed-nav="isMixedNav"
:is-mobile="isMobile"
2024-06-01 14:17:52 +00:00
:show="!fullContent && !headerHidden"
2024-05-19 13:20:42 +00:00
:show-toggle-btn="showHeaderToggleButton"
2024-06-09 07:39:11 +00:00
:sidebar-width="sidebarWidth"
2024-05-19 13:20:42 +00:00
:width="mainStyle.width"
:z-index="headerZIndex"
@open-menu="handleOpenMenu"
@toggle-sidebar="() => emit('toggleSidebar')"
2024-05-19 13:20:42 +00:00
>
<template v-if="showHeaderLogo" #logo>
<slot name="logo"></slot>
</template>
<slot name="header"></slot>
</LayoutHeader>
2024-06-09 07:39:11 +00:00
<LayoutTabbar
v-if="tabbarEnable"
:height="tabbarHeight"
2024-06-09 07:39:11 +00:00
:style="tabbarStyle"
>
<slot name="tabbar"></slot>
</LayoutTabbar>
2024-05-19 13:20:42 +00:00
</div>
<!-- </div> -->
<LayoutContent
:content-compact="contentCompact"
:content-compact-width="contentCompactWidth"
:padding="contentPadding"
:padding-bottom="contentPaddingBottom"
:padding-left="contentPaddingLeft"
:padding-right="contentPaddingRight"
:padding-top="contentPaddingTop"
2024-06-09 05:31:43 +00:00
:style="contentStyle"
class="transition-[margin-top] duration-200"
2024-05-19 13:20:42 +00:00
>
<slot name="content"></slot>
</LayoutContent>
<LayoutFooter
2024-06-09 05:31:43 +00:00
v-if="footerEnable"
2024-05-19 13:20:42 +00:00
:fixed="footerFixed"
:height="footerHeight"
:show="!fullContent"
:width="footerWidth"
:z-index="zIndex"
>
<slot name="footer"></slot>
</LayoutFooter>
</div>
<slot name="extra"></slot>
2024-05-19 13:20:42 +00:00
<div
v-if="maskVisible"
:style="maskStyle"
2024-07-16 15:13:03 +00:00
class="bg-overlay fixed left-0 top-0 h-full w-full transition-[background-color] duration-200"
2024-05-19 13:20:42 +00:00
@click="handleClickMask"
></div>
</div>
</template>