优化左边栏的实现

This commit is contained in:
jingrow 2025-12-28 02:25:18 +08:00
parent 35968bc93e
commit 569e4d3e45
2 changed files with 41 additions and 61 deletions

View File

@ -65,7 +65,7 @@ const MobileNav = defineAsyncComponent(
const route = useRoute();
const team = getTeam();
// Naive UI - 使 tooltip
// Naive UI - 使 tooltip menu
const themeOverrides = {
Tooltip: {
color: '#fafafa',
@ -73,8 +73,24 @@ const themeOverrides = {
padding: '6px 12px',
borderRadius: '8px',
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.08)',
// Naive UI theme-overrides
// CSS
},
Menu: {
// 使
itemTextColor: '#4a5568',
itemTextColorHover: '#4a5568',
itemTextColorActive: '#18a058',
itemTextColorChildActive: '#18a058',
itemColorHover: 'rgba(0, 0, 0, 0.04)',
itemColorActive: 'rgba(24, 160, 88, 0.1)',
itemColorActiveHover: 'rgba(24, 160, 88, 0.15)',
itemBorderRadius: '8px',
itemPadding: '10px 12px',
itemIconColor: '#8b8e95',
itemIconColorHover: '#18a058',
itemIconColorActive: '#18a058',
itemIconColorChildActive: '#18a058',
itemIconSize: '18px',
fontSize: '14px',
},
};

View File

@ -30,16 +30,16 @@
<div class="sidebar-menu">
<NavigationItems>
<template v-slot="{ navigation }">
<n-menu
:collapsed="collapsed"
:collapsed-width="64"
:collapsed-icon-size="20"
:options="convertToMenuOptions(navigation)"
:value="activeKey"
:router="false"
@update:value="handleMenuSelect"
class="app-sidebar-menu"
/>
<n-menu
:collapsed="collapsed"
:collapsed-width="64"
:collapsed-icon-size="20"
:options="convertToMenuOptions(navigation)"
:value="activeKey"
:router="false"
@update:value="handleMenuSelect"
class="app-sidebar-menu"
/>
</template>
</NavigationItems>
</div>
@ -77,38 +77,14 @@ export default {
},
},
emits: ['update:collapsed'],
setup(props, { emit }) {
setup(props) {
const route = useRoute();
const router = useRouter();
const instance = getCurrentInstance();
const showTeamSwitcher = ref(false);
//
const isMobile = computed(() => {
if (typeof window === 'undefined') return false;
return window.innerWidth < 768;
});
const collapsed = computed({
get: () => {
// props 使 props
if (props.collapsed !== undefined) {
return props.collapsed;
}
return isMobile.value;
},
set: (value) => emit('update:collapsed', value),
});
//
if (typeof window !== 'undefined') {
const handleResize = () => {
if (window.innerWidth < 768 && !props.collapsed) {
emit('update:collapsed', true);
}
};
window.addEventListener('resize', handleResize);
}
// 使 props
const collapsed = computed(() => props.collapsed);
//
const dropdownOptions = computed(() => {
@ -155,29 +131,15 @@ export default {
return items
.filter((item) => item.condition !== false)
.map((item) => {
// item.icon h()
// Naive UI n-menu icon
// 使
// Naive UI NMenu icon
let iconComponent = undefined;
if (item.icon) {
iconComponent = () => {
try {
const iconVNode = item.icon();
// VNode props size
if (iconVNode && !iconVNode.props) {
iconVNode.props = { width: 16, height: 16 };
} else if (iconVNode && iconVNode.props) {
// size
if (!iconVNode.props.width && !iconVNode.props.size) {
iconVNode.props.width = 16;
}
if (!iconVNode.props.height && !iconVNode.props.size) {
iconVNode.props.height = 16;
}
}
// NIcon
return h(NIcon, null, { default: () => iconVNode });
// NIcon
return h(NIcon, { size: 18 }, { default: () => iconVNode });
} catch (e) {
console.error(`[AppSidebar] Icon error for "${item.name}":`, e);
return null;
@ -233,14 +195,16 @@ export default {
if (!path.startsWith('/')) {
path = '/' + path;
}
// /dashboard /
if (path === '/dashboard') {
path = '/';
// /dashboard /
if (path === '/dashboard' || path === '/') {
return path;
}
return path;
});
//
// base '/dashboard/'使 router="true"
//
const handleMenuSelect = (key) => {
// base '/dashboard/'key base '/sites'
// 使 router.pushVue Router base