feat(sidebar): 重构菜单层级样式与图标占位对齐逻辑

- 新增菜单层级 depth 计算,限制最深缩进为5级避免内容截断
- 统一菜单行对齐样式,修复图标与文字垂直居中问题
- 新增层级引导线样式,替换部分层级加粗区分
- 优化三级及以下菜单的行高、圆角和文字样式
- 新增菜单图标占位元素,保证无图标时文案对齐
- 适配折叠态菜单的图标居中与占位元素隐藏
frontend_1.0.0
kangwenjing 2026-10-15 12:22:31 +08:00
parent dce74b065b
commit ad7aa82afc
3 changed files with 220 additions and 31 deletions

View File

@ -5,6 +5,79 @@
* 布局骨架(固定定位 / 折叠 / 移动端抽屉)沿用若依原有实现。
*/
/* ---------- 菜单行对齐 ---------- */
/* 图标盒子尺寸与图标—文案间距,展开态和折叠弹出菜单共用 */
$sidebar-icon-size: 16px;
$sidebar-icon-gap: 8px;
/*
* 菜单行(一级项 / 分组标题 / 子项)统一用 flex 做视觉居中。
* 原来靠 line-height + vertical-align 对齐,SvgIcon 组件自带的
* vertical-align: -0.15em 会让图标中心高于文字视觉中心约 3px,
* 且不同 viewBox 的图标高低不一;flex 下图标盒子直接对齐行中心。
*/
@mixin menu-row-align($icon-size: $sidebar-icon-size, $icon-gap: $sidebar-icon-gap) {
display: flex;
align-items: center;
.svg-icon,
[class^='el-icon-'] {
flex: 0 0 auto;
display: block;
width: $icon-size;
height: $icon-size;
margin-right: $icon-gap;
font-size: $icon-size;
line-height: 1;
text-align: center;
// 图标颜色跟随菜单文字(含 hover / 选中态)
color: inherit;
}
// 子级菜单的 Dot 占位点:字形只占画布约 30%,按整格间距点位和文字会显得松散,
// 单独收紧后它与文案的视觉距离才和真实图标一致(class 由 Sidebar/Item.vue 打标)
.svg-icon.menu-icon-bullet {
margin-right: 3px;
}
// 一级菜单没配图标时的空槽位,把图标列留给文案对齐(class 由 Sidebar/Item.vue 打标)
.menu-icon-placeholder {
flex: 0 0 auto;
display: block;
position: relative;
width: $icon-size;
height: $icon-size;
margin-right: $icon-gap;
}
// flex 下省略号要由文案自己负责,否则长菜单名会被直接裁掉
> span:not(.menu-icon-placeholder) {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
// 折叠态下 element 会把菜单项内容包进 tooltip 触发层(该层被绝对定位铺满整行,并自带
// 内联 display: inline-block 与 padding: 0 20px),这层也要 flex,图标才会跟着行一起居中
> .el-tooltip,
> .el-tooltip > div:not(.el-tooltip__popper) {
display: flex !important;
align-items: center;
}
}
/* ---------- 菜单层级(缩进 / 行高 / 引导线) ---------- */
/*
* Element 的缩进是每个 submenu 祖先 +20px(写成行内 padding-left),再叠加嵌套 .el-menu
* 自身的 12px 横向 padding,实际每级 32px;四级菜单文案起点到了 128px,可用宽度不到 80px,
* “应收收款核销记录”这类名称会被裁断。这里把横向缩进收回来交给 menu-depth-* 统一控制:
* 嵌套菜单不留横向 padding,胶囊左内边距 12px,三级起每级只走 16px,
* 层级关系改由竖向引导线表达。类名由 Sidebar/SidebarItem.vue 按递归深度输出(最深 5 级)。
*/
$sidebar-indent-by-depth: (1: 12px, 2: 28px, 3: 44px, 4: 60px, 5: 76px);
$sidebar-row-height: (1: 42px, 2: 38px, 3: 34px, 4: 32px, 5: 32px);
#app {
/* 同上:这里不能带 z-index,否则页面内的弹窗 / 抽屉会被 body 上的遮罩盖住 */
.main-container {
@ -62,7 +135,10 @@
display: none;
}
a {
// 这条 reset 只服务于菜单里的 app-link(每个菜单项外面套一个 a)。
// 它带 #app 前缀,特异性高于组件里的 scoped 样式,写宽到整个 sidebar-container
// 会把 logo 链接的 display: flex 也压成块级,名称就被挤到图标下面另起一行。
.el-menu a {
display: inline-block;
width: 100%;
overflow: hidden;
@ -78,6 +154,7 @@
.el-menu-item,
.el-submenu__title {
@include menu-row-align;
overflow: hidden !important;
text-overflow: ellipsis !important;
white-space: nowrap !important;
@ -98,20 +175,6 @@
}
}
.el-menu-item [class^='el-icon-'],
.el-submenu__title [class^='el-icon-'] {
margin-right: 12px;
width: 20px;
text-align: center;
font-size: 16px;
}
.svg-icon {
margin-right: 12px;
width: 18px;
height: 18px;
}
// 选中态:primary-50 胶囊 + 主色文字
.el-menu-item.is-active {
background-color: $s2-primary-50 !important;
@ -124,16 +187,73 @@
}
}
// 子菜单层级留白
.nest-menu .el-submenu > .el-submenu__title,
.el-submenu .el-menu-item {
min-width: auto !important;
height: 38px;
line-height: 38px;
// 嵌套子菜单:横向缩进交给 menu-depth-*,这里只留一点纵向留白
.el-menu--inline {
position: relative;
padding: 0 0 4px;
}
@each $d, $pad in $sidebar-indent-by-depth {
$child-h: if($d >= 5, 32px, map-get($sidebar-row-height, $d + 1));
// Element 的缩进写在行内样式上,覆盖它必须带 !important
.el-menu-item.menu-depth-#{$d},
.el-submenu.menu-depth-#{$d} > .el-submenu__title {
min-width: auto !important;
padding-left: $pad !important;
height: map-get($sidebar-row-height, $d);
line-height: map-get($sidebar-row-height, $d);
}
// 子级组左侧引导线:竖在上一层图标的中心,末端停在最后一行的行高中心
.el-submenu.menu-depth-#{$d} > .el-menu--inline::before {
content: '';
// element-ui 的 .el-menu::before 是 clearfix(display: table),
// 绝对定位下表格盒会按内容高度塌成 0,这里必须显式回到 block
display: block;
position: absolute;
top: 0;
bottom: $child-h / 2 + 6px;
left: $pad + $sidebar-icon-size / 2 - 0.5px;
width: 1px;
background-color: $s2-gray-200;
}
}
// 三四级行高更矮,胶囊圆角跟着收一档;标题不再加粗,靠缩进和引导线分层
.el-menu-item.menu-depth-3,
.el-menu-item.menu-depth-4,
.el-menu-item.menu-depth-5,
.el-submenu.menu-depth-3 > .el-submenu__title,
.el-submenu.menu-depth-4 > .el-submenu__title,
.el-submenu.menu-depth-5 > .el-submenu__title {
border-radius: 10px;
font-weight: 500;
}
// 深层叶子行右侧没有展开箭头,收一点留白把宽度让给菜单名,
// 否则“应收收款核销记录”这类 8 字名称还是会出省略号
.el-menu-item.menu-depth-3,
.el-menu-item.menu-depth-4,
.el-menu-item.menu-depth-5 {
padding-right: 12px !important;
}
// 四级是叶子节点,浅色主题下文字再轻一档;排除选中态避免盖掉主色,
// 深色主题沿用 accent-300(gray-500 在深色底上对比度不够)
&:not(.is-dark) {
.el-menu-item.menu-depth-4:not(.is-active),
.el-menu-item.menu-depth-5:not(.is-active) {
color: $s2-gray-500;
}
}
/* ---------- 深色分支 ---------- */
&.is-dark {
.el-submenu > .el-menu--inline::before {
background-color: rgba(255, 255, 255, 0.12);
}
.el-menu-item,
.el-submenu__title {
color: $s2-accent-300;
@ -170,11 +290,33 @@
justify-content: center;
}
// 折叠态图标位于 tooltip 触发层内,同样水平居中
.el-menu-item > .el-tooltip,
.el-menu-item > .el-tooltip > div:not(.el-tooltip__popper) {
justify-content: center;
padding: 0 !important;
}
.svg-icon,
.svg-icon.menu-icon-bullet,
.el-menu-item [class^='el-icon-'],
.el-submenu__title [class^='el-icon-'] {
margin-right: 0;
}
// 折叠态只留图标:menu-depth-* 的缩进选择器比上面那条更具体,
// 不逐层取消掉的话图标会被 padding-left 挤离行中心
@each $d, $pad in $sidebar-indent-by-depth {
.el-menu-item.menu-depth-#{$d},
.el-submenu.menu-depth-#{$d} > .el-submenu__title {
padding-left: 0 !important;
}
}
// 折叠态没有文案需要对齐,占位槽直接收掉
.menu-icon-placeholder {
display: none;
}
}
.main-container {
@ -248,8 +390,9 @@
/* ---------- 折叠后的弹出子菜单 ---------- */
.el-menu--vertical {
& > .el-menu {
.svg-icon {
margin-right: 12px;
.el-menu-item,
.el-submenu__title {
@include menu-row-align;
}
}

View File

@ -1,4 +1,15 @@
<!--
* 菜单行内容:图标 + 文案。
* 对齐方式(flex 居中、图标尺寸与间距)统一在 assets/styles/sidebar.scss 的 menu-row-align 里,
* 这里只负责内容;Dot 这类“占位点”字形需要额外收紧间距,用一个标记 class 传给样式层。
-->
<script>
// 仅用于子级菜单的占位点图标:画布大部分是留白,按整格间距会显得点位和文字离得远
const BULLET_ICONS = ['Dot']
// 子级菜单漏配 vue_icon 时兜底用的点位图标:菜单树里“子级用点占位”本来就是既有约定,
// 直接复用同一个图标,才能和真正的 Dot 行像素对齐(四级菜单目前全部没配图标)
const FALLBACK_BULLET = 'Dot'
export default {
name: 'MenuItem',
functional: true,
@ -10,14 +21,24 @@ export default {
title: {
type: String,
default: ''
},
// 所在层级,一级和子级的“没图标”处理方式不同
depth: {
type: Number,
default: 1
}
},
render(h, context) {
const { icon, title } = context.props
const { icon, title, depth } = context.props
const vnodes = []
if (icon) {
vnodes.push(<svg-icon icon-class={icon}/>)
if (icon || depth > 1) {
const name = icon || FALLBACK_BULLET
const isBullet = BULLET_ICONS.indexOf(name) > -1
vnodes.push(<svg-icon icon-class={name} class-name={isBullet ? 'menu-icon-bullet' : undefined}/>)
} else {
// 一级没配图标的行也要占住图标列,否则同层里有图标和没图标的行文案起点会错开
vnodes.push(<span class="menu-icon-placeholder" aria-hidden="true"/>)
}
if (title) {

View File

@ -2,21 +2,22 @@
<div v-if="!item.hidden">
<template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
<app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path, onlyOneChild.query)">
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
<item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" />
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="itemClass">
<item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" :depth="level" />
</el-menu-item>
</app-link>
</template>
<el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
<el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body :class="submenuClass">
<template slot="title">
<item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title" />
<item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title" :depth="level" />
</template>
<sidebar-item
v-for="(child, index) in item.children"
:key="child.path + index"
:is-nest="true"
:item="child"
:depth="level + 1"
:base-path="resolvePath(child.path)"
class="nest-menu"
/>
@ -48,12 +49,34 @@ export default {
basePath: {
type: String,
default: ''
},
// 菜单层级,由上层递归传入;样式层按层级控制缩进、行高和层级引导线
depth: {
type: Number,
default: 1
}
},
data() {
this.onlyOneChild = null
return {}
},
computed: {
// 超过五级不再继续加深缩进,否则深层菜单名会被挤到截断
level() {
return Math.min(this.depth, 5)
},
itemClass() {
return {
'submenu-title-noDropdown': !this.isNest,
['menu-depth-' + this.level]: true
}
},
submenuClass() {
return {
['menu-depth-' + this.level]: true
}
}
},
methods: {
hasOneShowingChild(children = [], parent) {
if (!children) {
@ -98,7 +121,9 @@ export default {
}
</script>
<style lang="scss" scoped>
::v-deep .el-submenu__title {
/* 加粗只留给一二级分组标题;三四级再加粗,四级菜单整棵树会显得很吵 */
::v-deep .menu-depth-1 > .el-submenu__title,
::v-deep .menu-depth-2 > .el-submenu__title {
font-size: 14px;
font-weight: bold;
}