SidebarMenuItem.vue
4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<script setup lang="ts">
import type { MenuItem } from '@/types/menu';
import { computed } from 'vue';
import SidebarMenuItem from './SidebarMenuItem.vue';
interface Props {
menu: MenuItem;
activePath: string;
expandedKeys: Set<string>;
}
const props = defineProps<Props>();
const emit = defineEmits<{
click: [menu: MenuItem];
toggle: [menuId: string];
}>();
// 是否有子菜单
const hasChildren = computed(() => {
return props.menu.children && props.menu.children.length > 0;
});
// 是否展开
const isExpanded = computed(() => {
return props.expandedKeys.has(props.menu.id);
});
// 是否激活(当前路由或子路由激活)
const isActive = computed(() => {
return (
props.activePath === props.menu.path ||
props.activePath.startsWith(`${props.menu.path}/`)
);
});
// 点击菜单项
const handleClick = () => {
if (hasChildren.value) {
handleToggle();
} else {
emit('click', props.menu);
}
};
// 展开/收起子菜单
const handleToggle = () => {
emit('toggle', props.menu.id);
};
</script>
<template>
<div class="sidebar-menu-item">
<!-- 菜单项本身 -->
<div
class="menu-item-content"
:class="{
active: isActive,
'has-children': hasChildren,
[`level-${menu.level || 1}`]: true,
}"
@click="handleClick"
>
<!-- 展开/收起图标 -->
<span v-if="hasChildren" class="expand-icon" @click.stop="handleToggle">
{{ isExpanded ? '▼' : '▶' }}
</span>
<!-- 菜单图标 -->
<span v-if="menu.icon" class="menu-icon">{{ menu.icon }}</span>
<!-- 菜单标题 -->
<span class="menu-title">{{ menu.title }}</span>
<!-- 徽章 -->
<span v-if="menu.meta?.badge" class="menu-badge">
{{ menu.meta.badge }}
</span>
</div>
<!-- 子菜单 -->
<transition name="submenu-expand">
<div v-if="hasChildren && isExpanded" class="submenu">
<SidebarMenuItem
v-for="child in menu.children"
:key="child.id"
:menu="child"
:active-path="activePath"
:expanded-keys="expandedKeys"
@click="$emit('click', $event)"
@toggle="$emit('toggle', $event)"
/>
</div>
</transition>
</div>
</template>
<style scoped>
.sidebar-menu-item {
width: 100%;
}
.menu-item-content {
position: relative;
display: flex;
gap: 8px;
align-items: center;
padding: 10px 16px;
color: #333;
cursor: pointer;
user-select: none;
transition: all 0.3s;
}
/* 层级缩进 */
.menu-item-content.level-1 {
padding-left: 16px;
}
.menu-item-content.level-2 {
padding-left: 32px;
font-size: 13px;
}
.menu-item-content.level-3 {
padding-left: 48px;
font-size: 12px;
}
.menu-item-content:hover {
color: #1890ff;
background-color: #f5f5f5;
}
.menu-item-content.active {
font-weight: 500;
color: #1890ff;
background-color: #e6f7ff;
}
.menu-item-content.active::before {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 3px;
content: '';
background-color: #1890ff;
}
.expand-icon {
display: flex;
flex-shrink: 0;
align-items: center;
justify-content: center;
width: 16px;
height: 16px;
font-size: 10px;
color: #666;
transition: transform 0.3s;
}
.menu-icon {
display: flex;
flex-shrink: 0;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
font-size: 16px;
}
.menu-title {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.menu-badge {
flex-shrink: 0;
min-width: 18px;
height: 18px;
padding: 0 6px;
font-size: 11px;
line-height: 18px;
color: #fff;
text-align: center;
background-color: #ff4d4f;
border-radius: 9px;
}
.submenu {
overflow: hidden;
}
/* 子菜单展开动画 */
.submenu-expand-enter-active,
.submenu-expand-leave-active {
transition: all 0.3s ease;
}
.submenu-expand-enter-from,
.submenu-expand-leave-to {
max-height: 0;
opacity: 0;
transform: translateY(-10px);
}
.submenu-expand-enter-to,
.submenu-expand-leave-from {
max-height: 1000px;
opacity: 1;
transform: translateY(0);
}
/* 有子菜单的项目样式 */
.menu-item-content.has-children {
font-weight: 500;
}
/* 二级菜单样式 */
.level-2 .menu-item-content {
background-color: rgb(0 0 0 / 2%);
}
.level-2 .menu-item-content:hover {
background-color: #f0f0f0;
}
/* 三级菜单样式 */
.level-3 .menu-item-content {
background-color: rgb(0 0 0 / 4%);
}
.level-3 .menu-item-content:hover {
background-color: #ebebeb;
}
</style>