Files
IM/frontend/web/src/components/addMenu.vue
T

180 lines
4.1 KiB
Vue

<template>
<div class="add-menu-container" v-click-outside="closeMenu">
<button class="add-btn" :class="{ active: isShow }" @click="toggleMenu">
<span class="plus-icon">+</span>
</button>
<Transition name="pop">
<div v-if="isShow" class="menu-card">
<div class="arrow"></div>
<div class="menu-list">
<div class="menu-item" v-for="(item, index) in props.menuList" :key="index" @click="handleAction(item.action)">
<i class="icon" v-html="item.icon"></i>
<span>{{item.text}}</span>
</div>
</div>
</div>
</Transition>
</div>
</template>
<script setup>
import { ref, defineProps, onMounted, defineEmits } from 'vue';
const props = defineProps({
menuList: {
type: Object,
required: true
}
})
const emit = defineEmits(['actionActive'])
const isShow = ref(false);
const toggleMenu = () => {
isShow.value = !isShow.value;
};
const closeMenu = () => {
isShow.value = false;
};
const handleAction = (type) => {
emit('actionActive', type);
isShow.value = false; // 点击后关闭
};
/**
* 自定义指令:点击外部区域关闭菜单
* 也可以使用第三方库如 @vueuse/core 的 onClickOutside
*/
const vClickOutside = {
mounted(el, binding) {
el.clickOutsideEvent = (event) => {
if (!(el === event.target || el.contains(event.target))) {
binding.value();
}
};
document.addEventListener('click', el.clickOutsideEvent);
},
unmounted(el) {
document.removeEventListener('click', el.clickOutsideEvent);
},
};
</script>
<style scoped>
/* --- 以下是保持不动的原样部分 --- */
.add-menu-container {
position: relative;
display: inline-block;
}
.add-btn {
width: 32px;
height: 32px;
background: #dbdbdb;
border: none;
border-radius: 6px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
transition: all 0.2s;
}
.add-btn:hover {
background: #cccbcb;
}
.plus-icon {
font-size: 22px;
line-height: 1;
font-weight: 300;
}
.pop-enter-active, .pop-leave-active {
transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}
.pop-enter-from, .pop-leave-to {
opacity: 0;
transform: scale(0.9) translateY(-10px);
}
.arrow {
position: absolute;
top: -6px;
right: 12px;
width: 0;
height: 0;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-bottom: 6px solid white;
}
/* --- 重点优化:仅限菜单列表部分 --- */
.menu-card {
position: absolute;
top: 45px;
right: 0;
width: 120px; /* 稍微加宽,避免局促 */
background: white;
border-radius: 10px;
/* 优化:使用更柔和的复合阴影,提升高级感 */
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 10px 15px -3px rgba(0, 0, 0, 0.1);
z-index: 100;
transform-origin: top right;
padding: 4px; /* 增加内边距,让列表项不贴边 */
border: 1px solid rgba(0, 0, 0, 0.05); /* 增加极细边框,防止白底背景重叠 */
}
.menu-list {
padding: 0; /* 清除默认,改由父级 padding 控制 */
display: flex;
flex-direction: column;
gap: 2px; /* 增加项与项之间的微小缝隙 */
}
.menu-item {
display: flex;
align-items: center;
padding: 8px 10px; /* 增加点击区域和呼吸感 */
cursor: pointer;
border-radius: 6px; /* 每一行也给圆角,悬浮时更好看 */
transition: all 0.2s;
color: #4a4a4a;
justify-content: center;
}
.menu-item:hover {
background: #c6c6c6; /* 换成淡淡的品牌色背景 */
}
/* 移除旧的、生硬的边角覆盖逻辑,改用上面统一的 border-radius */
.menu-item:first-child:hover,
.menu-item:last-child:hover {
border-top-left-radius: 6px;
border-top-right-radius: 6px;
border-bottom-left-radius: 6px;
border-bottom-right-radius: 6px;
}
.icon {
margin-right: 8px; /* 增加图标与文字的距离 */
font-size: 14px; /* 稍微调大一点点 */
display: flex;
align-items: center;
justify-content: center;
width: 16px;
}
.menu-item span {
font-size: 13px; /* 稍微增大字号,更容易阅读 */
font-weight: 500; /* 增加字重,更有质感 */
}
</style>