UserInfo.vue 3.88 KB
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";

const showDropdown = ref(false);

const userInfo = {
	name: "管理员",
	avatar: "👤",
	id: "001",
};

const userInitial = "管";

const toggleDropdown = () => {
	showDropdown.value = !showDropdown.value;
};

const handleClickOutside = (event: MouseEvent) => {
	const target = event.target as HTMLElement;
	if (!target.closest(".user-info")) {
		showDropdown.value = false;
	}
};

const handleProfile = () => {
	showDropdown.value = false;
};

const handleSettings = () => {
	showDropdown.value = false;
};

onMounted(() => {
	document.addEventListener("click", handleClickOutside);
});

onUnmounted(() => {
	document.removeEventListener("click", handleClickOutside);
});
</script>

<template>
    <div class="user-info">
        <div class="user-info-card" @click.stop="toggleDropdown">
            <div class="avatar">
                {{ userInitial }}
            </div>
            <p class="name">
                {{ userInfo.name }}
            </p>
        </div>

        <div v-if="showDropdown"
             class="user-dropdown"
             @click.stop>
            <div class="dropdown-header">
                <div class="dropdown-avatar">
                    {{ userInfo.avatar }}
                </div>
                <div class="dropdown-info">
                    <div class="dropdown-name">
                        {{ userInfo.name }}
                    </div>
                    <div class="dropdown-id">
                        ID: {{ userInfo.id }}
                    </div>
                </div>
            </div>

            <div class="dropdown-divider"></div>

            <div class="dropdown-menu">
                <div class="menu-item" @click="handleProfile">
                    <span class="menu-icon">👤</span>
                    <span>个人中心</span>
                </div>
                <div class="menu-item" @click="handleSettings">
                    <span class="menu-icon">⚙️</span>
                    <span>设置</span>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped>
.user-info {
	position: relative;
	padding: 8px;
}

.user-info-card {
	position: relative;
	display: flex;
	gap: 6px;
	align-items: center;
	justify-content: center;
	padding: 8px 12px;
	cursor: pointer;
	border-radius: 6px;
	transition: all 0.2s;
}

.user-info-card:hover {
	background: rgb(0 0 0 / 4%);
}

.user-info-card .avatar {
	flex-shrink: 0;
	width: 28px;
	height: 28px;
	font-size: 16px;
	font-weight: bold;
	line-height: 28px;
	color: #1956fe;
	text-align: center;
	background: #e6efff;
	border-radius: 50%;
	opacity: 1;
}

.user-info-card .name {
	font-size: 12px;
	color: #777c85;
	white-space: nowrap;
}

.user-dropdown {
	position: absolute;
	top: calc(100% + 8px);
	right: 0;
	z-index: 1000;
	min-width: 280px;
	overflow: hidden;
	background: white;
	border-radius: 8px;
	box-shadow: 0 8px 24px rgb(0 0 0 / 15%);
}

.dropdown-header {
	display: flex;
	gap: 12px;
	align-items: center;
	padding: 16px;
}

.dropdown-avatar {
	display: flex;
	flex-shrink: 0;
	align-items: center;
	justify-content: center;
	width: 56px;
	height: 56px;
	font-size: 32px;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 50%;
}

.dropdown-info {
	flex: 1;
	min-width: 0;
}

.dropdown-name {
	margin-bottom: 4px;
	overflow: hidden;
	text-overflow: ellipsis;
	font-size: 18px;
	font-weight: 600;
	color: #262626;
	white-space: nowrap;
}

.dropdown-id {
	font-size: 13px;
	color: #8c8c8c;
}

.dropdown-divider {
	height: 1px;
	margin: 0 8px;
	background: #f0f0f0;
}

.dropdown-menu {
	padding: 8px;
}

.menu-item {
	display: flex;
	gap: 12px;
	align-items: center;
	padding: 12px 16px;
	font-size: 14px;
	color: #262626;
	cursor: pointer;
	border-radius: 6px;
	transition: all 0.2s;
}

.menu-item:hover {
	color: #667eea;
	background: #f5f5f5;
}

.menu-icon {
	font-size: 18px;
}
</style>