|
| 1 | +<template> |
| 2 | + <div class="relative"> |
| 3 | + <button |
| 4 | + @click="isOpen = !isOpen" |
| 5 | + class="flex items-center space-x-3 px-3 py-2 hover:bg-gray-50 rounded-lg transition-colors" |
| 6 | + > |
| 7 | + <div class="text-right hidden sm:block"> |
| 8 | + <p class="text-sm font-semibold text-gray-900">{{ userName }}</p> |
| 9 | + </div> |
| 10 | + <div class="w-9 h-9 bg-gradient-to-br from-blue-500 to-blue-600 rounded-full flex items-center justify-center shadow-md"> |
| 11 | + <span class="text-sm font-bold text-white">{{ userInitials }}</span> |
| 12 | + </div> |
| 13 | + <svg class="w-4 h-4 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 14 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"/> |
| 15 | + </svg> |
| 16 | + </button> |
| 17 | + |
| 18 | + <!-- Dropdown Menu --> |
| 19 | + <div |
| 20 | + v-if="isOpen" |
| 21 | + @click.stop |
| 22 | + class="absolute right-0 mt-2 w-60 bg-white rounded-xl shadow-xl border border-gray-200 py-2 z-50" |
| 23 | + > |
| 24 | + <!-- User Info Header --> |
| 25 | + <div class="px-4 py-3 border-b border-gray-100"> |
| 26 | + <p class="text-sm font-semibold text-gray-900">{{ userName }}</p> |
| 27 | + <p v-if="profileName" class="text-xs text-gray-500 mt-0.5">{{ profileName }}</p> |
| 28 | + </div> |
| 29 | + |
| 30 | + <!-- Menu Items --> |
| 31 | + <div class="py-1"> |
| 32 | + <slot name="menu-items"></slot> |
| 33 | + </div> |
| 34 | + |
| 35 | + <!-- Divider --> |
| 36 | + <hr v-if="showDivider" class="my-2 border-gray-100"> |
| 37 | + |
| 38 | + <!-- Additional Actions --> |
| 39 | + <slot name="additional-actions"></slot> |
| 40 | + |
| 41 | + <!-- Divider --> |
| 42 | + <hr v-if="showLogout" class="my-2 border-gray-100"> |
| 43 | + |
| 44 | + <!-- Logout --> |
| 45 | + <button |
| 46 | + v-if="showLogout" |
| 47 | + @click="handleLogout" |
| 48 | + class="w-full text-left px-4 py-2.5 text-sm text-red-600 hover:bg-red-50 flex items-center space-x-3 transition-colors" |
| 49 | + > |
| 50 | + <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 51 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/> |
| 52 | + </svg> |
| 53 | + <span>Logout</span> |
| 54 | + </button> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | +</template> |
| 58 | + |
| 59 | +<script setup> |
| 60 | +import { ref, computed, onMounted, onUnmounted } from 'vue' |
| 61 | +
|
| 62 | +const props = defineProps({ |
| 63 | + userName: { |
| 64 | + type: String, |
| 65 | + required: true |
| 66 | + }, |
| 67 | + profileName: { |
| 68 | + type: String, |
| 69 | + default: null |
| 70 | + }, |
| 71 | + showLogout: { |
| 72 | + type: Boolean, |
| 73 | + default: true |
| 74 | + }, |
| 75 | + showDivider: { |
| 76 | + type: Boolean, |
| 77 | + default: true |
| 78 | + } |
| 79 | +}) |
| 80 | +
|
| 81 | +const emit = defineEmits(['logout']) |
| 82 | +
|
| 83 | +const isOpen = ref(false) |
| 84 | +
|
| 85 | +const userInitials = computed(() => { |
| 86 | + const parts = props.userName.split(" ") |
| 87 | + if (parts.length >= 2) { |
| 88 | + return (parts[0][0] + parts[1][0]).toUpperCase() |
| 89 | + } |
| 90 | + return props.userName.substring(0, 2).toUpperCase() |
| 91 | +}) |
| 92 | +
|
| 93 | +function handleLogout() { |
| 94 | + isOpen.value = false |
| 95 | + emit('logout') |
| 96 | +} |
| 97 | +
|
| 98 | +function handleClickOutside(event) { |
| 99 | + const userMenuButton = event.target.closest('button') |
| 100 | + const userMenu = event.target.closest('.absolute.right-0.mt-2') |
| 101 | +
|
| 102 | + if (isOpen.value && !userMenuButton && !userMenu) { |
| 103 | + isOpen.value = false |
| 104 | + } |
| 105 | +} |
| 106 | +
|
| 107 | +onMounted(() => { |
| 108 | + document.addEventListener('click', handleClickOutside) |
| 109 | +}) |
| 110 | +
|
| 111 | +onUnmounted(() => { |
| 112 | + document.removeEventListener('click', handleClickOutside) |
| 113 | +}) |
| 114 | +</script> |
0 commit comments