|
| 1 | +<script lang="ts" setup> |
| 2 | +import { ref, computed, onMounted, onUnmounted } from "vue"; |
| 3 | +import type { SupportedLocale } from "~/types/types"; |
| 4 | +
|
| 5 | +const { locale, locales, setLocale } = useI18n(); |
| 6 | +const { t } = useI18n(); |
| 7 | +
|
| 8 | +interface Props { |
| 9 | + theme?: 'purple' | 'white' | 'dark'; |
| 10 | +} |
| 11 | +
|
| 12 | +const props = withDefaults(defineProps<Props>(), { |
| 13 | + theme: 'white' |
| 14 | +}); |
| 15 | +
|
| 16 | +// Populate available locales from i18n plugin |
| 17 | +const availableLocales = computed(() => locales.value); |
| 18 | +const currentLocaleName = computed(() => { |
| 19 | + const currentLocale = locales.value.find( |
| 20 | + (lang) => lang.code === locale.value, |
| 21 | + ); |
| 22 | + return currentLocale?.name || ""; |
| 23 | +}); |
| 24 | +
|
| 25 | +const dropdownOpen = ref(false); |
| 26 | +
|
| 27 | +// Close dropdown when clicking outside |
| 28 | +const handleClickOutside = (event: MouseEvent) => { |
| 29 | + const target = event.target as HTMLElement; |
| 30 | + if (!target.closest('.language-picker-container')) { |
| 31 | + dropdownOpen.value = false; |
| 32 | + } |
| 33 | +}; |
| 34 | +
|
| 35 | +// Load locale from session storage on mount and set up click outside handler |
| 36 | +onMounted(() => { |
| 37 | + const savedLocale = sessionStorage.getItem("locale"); |
| 38 | + if (savedLocale && locales.value.some((lang) => lang.code === savedLocale)) { |
| 39 | + setLocale(savedLocale as SupportedLocale); |
| 40 | + } |
| 41 | + document.addEventListener('click', handleClickOutside); |
| 42 | +}); |
| 43 | +
|
| 44 | +onUnmounted(() => { |
| 45 | + document.removeEventListener('click', handleClickOutside); |
| 46 | +}); |
| 47 | +
|
| 48 | +const changeLocale = (locale: { code: string }): void => { |
| 49 | + setLocale(locale.code as SupportedLocale); |
| 50 | + sessionStorage.setItem("locale", locale.code); |
| 51 | + dropdownOpen.value = false; |
| 52 | +}; |
| 53 | +
|
| 54 | +const dropdownClasses = computed(() => { |
| 55 | + switch (props.theme) { |
| 56 | + case 'white': |
| 57 | + return "origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-black ring-opacity-5 z-50"; |
| 58 | + case 'dark': |
| 59 | + return "origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-gray-800 ring-1 ring-gray-600 z-50"; |
| 60 | + case 'purple': |
| 61 | + default: |
| 62 | + return "origin-top-right absolute right-0 mt-2 w-56 rounded-md shadow-lg bg-white ring-1 ring-gray-200 z-50"; |
| 63 | + } |
| 64 | +}); |
| 65 | +
|
| 66 | +const itemClasses = computed(() => { |
| 67 | + switch (props.theme) { |
| 68 | + case 'white': |
| 69 | + return "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 transition-colors"; |
| 70 | + case 'dark': |
| 71 | + return "block px-4 py-2 text-sm text-white hover:bg-gray-700 transition-colors"; |
| 72 | + case 'purple': |
| 73 | + default: |
| 74 | + return "block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100 transition-colors"; |
| 75 | + } |
| 76 | +}); |
| 77 | +</script> |
| 78 | + |
| 79 | +<template> |
| 80 | + <div class="relative inline-block text-left language-picker-container"> |
| 81 | + <div> |
| 82 | + <button |
| 83 | + type="button" |
| 84 | + class="relative w-10 h-10 rounded-full bg-white flex items-center justify-center hover:bg-gray-50 transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-500" |
| 85 | + @click.stop="dropdownOpen = !dropdownOpen" |
| 86 | + :title="t('header.languagePicker')" |
| 87 | + > |
| 88 | + <svg |
| 89 | + class="w-5 h-5 text-gray-600" |
| 90 | + xmlns="http://www.w3.org/2000/svg" |
| 91 | + fill="none" |
| 92 | + viewBox="0 0 24 24" |
| 93 | + stroke="currentColor" |
| 94 | + > |
| 95 | + <path |
| 96 | + stroke-linecap="round" |
| 97 | + stroke-linejoin="round" |
| 98 | + stroke-width="2" |
| 99 | + d="M3.055 11H5a2 2 0 012 2v1a2 2 0 002 2 2 2 0 012 2v2.945M8 3.935V5.5A2.5 2.5 0 0010.5 8h.5a2 2 0 012 2 2 2 0 104 0 2 2 0 012-2h1.064M15 20.488V18a2 2 0 012-2h3.064M21 12a9 9 0 11-18 0 9 9 0 0118 0z" |
| 100 | + /> |
| 101 | + </svg> |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + <div |
| 105 | + v-if="dropdownOpen" |
| 106 | + :class="dropdownClasses" |
| 107 | + @click.stop |
| 108 | + > |
| 109 | + <div class="py-1"> |
| 110 | + <a |
| 111 | + v-for="lang in availableLocales" |
| 112 | + :key="lang.code" |
| 113 | + href="#" |
| 114 | + :class="itemClasses" |
| 115 | + @click=" |
| 116 | + ($event.preventDefault(), |
| 117 | + $event.stopPropagation(), |
| 118 | + changeLocale(lang)) |
| 119 | + " |
| 120 | + > |
| 121 | + {{ lang.name }} |
| 122 | + </a> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | +</template> |
0 commit comments