SidebarMenuItem.vue 4.47 KB
<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>