|
| 1 | +<template> |
| 2 | + <div class="lang-switcher" ref="wrapRef"> |
| 3 | + <button |
| 4 | + class="lang-btn" |
| 5 | + :aria-label="t('language_switcher.label')" |
| 6 | + :title="t('language_switcher.label')" |
| 7 | + :aria-expanded="open" |
| 8 | + aria-haspopup="listbox" |
| 9 | + @click.stop="open = !open" |
| 10 | + > |
| 11 | + <Icon icon="bi:globe" width="1em" height="1em" aria-hidden="true" /> |
| 12 | + <span class="lang-current">{{ currentLocale.toUpperCase() }}</span> |
| 13 | + <Icon icon="bi:chevron-down" class="lang-chevron" :class="{ 'lang-chevron--open': open }" width="0.75em" height="0.75em" aria-hidden="true" /> |
| 14 | + </button> |
| 15 | + <ul |
| 16 | + v-show="open" |
| 17 | + role="listbox" |
| 18 | + :aria-label="t('language_switcher.label')" |
| 19 | + class="lang-dropdown" |
| 20 | + > |
| 21 | + <li |
| 22 | + v-for="locale in SUPPORTED_LOCALES" |
| 23 | + :key="locale" |
| 24 | + role="option" |
| 25 | + :aria-selected="locale === currentLocale" |
| 26 | + class="lang-option" |
| 27 | + :class="{ 'lang-option--active': locale === currentLocale }" |
| 28 | + @click="select(locale)" |
| 29 | + > |
| 30 | + {{ LANGUAGE_NAMES[locale] }} |
| 31 | + </li> |
| 32 | + </ul> |
| 33 | + </div> |
| 34 | +</template> |
| 35 | + |
| 36 | +<script setup lang="ts"> |
| 37 | +import { ref, onMounted, onUnmounted } from 'vue'; |
| 38 | +import { useI18n } from 'vue-i18n'; |
| 39 | +import { Icon } from '@iconify/vue'; |
| 40 | +import { SUPPORTED_LOCALES, type SupportedLocale } from '../i18n/index'; |
| 41 | +import { currentLocale, setLocale } from '../i18n/localeState'; |
| 42 | +
|
| 43 | +const { t } = useI18n(); |
| 44 | +const open = ref(false); |
| 45 | +const wrapRef = ref<HTMLElement | null>(null); |
| 46 | +
|
| 47 | +const LANGUAGE_NAMES: Record<SupportedLocale, string> = { |
| 48 | + en: 'English', |
| 49 | + es: 'Español', |
| 50 | + de: 'Deutsch', |
| 51 | + fr: 'Français', |
| 52 | + pt: 'Português', |
| 53 | + 'zh-TW': '中文(繁體)', |
| 54 | + 'zh-CN': '中文(简体)', |
| 55 | + ja: '日本語', |
| 56 | + ko: '한국어', |
| 57 | +}; |
| 58 | +
|
| 59 | +function select(locale: SupportedLocale) { |
| 60 | + setLocale(locale); |
| 61 | + open.value = false; |
| 62 | +} |
| 63 | +
|
| 64 | +function handleOutsideClick(e: MouseEvent) { |
| 65 | + if (open.value && !wrapRef.value?.contains(e.target as Node)) { |
| 66 | + open.value = false; |
| 67 | + } |
| 68 | +} |
| 69 | +
|
| 70 | +onMounted(() => document.addEventListener('click', handleOutsideClick)); |
| 71 | +onUnmounted(() => document.removeEventListener('click', handleOutsideClick)); |
| 72 | +</script> |
| 73 | + |
| 74 | +<style scoped> |
| 75 | +.lang-switcher { |
| 76 | + position: fixed; |
| 77 | + top: 1rem; |
| 78 | + right: calc(1rem + 40px + 0.5rem); |
| 79 | + z-index: var(--z-controls); |
| 80 | +} |
| 81 | +
|
| 82 | +.lang-btn { |
| 83 | + display: flex; |
| 84 | + align-items: center; |
| 85 | + gap: 0.3em; |
| 86 | + height: 40px; |
| 87 | + padding: 0 0.625rem; |
| 88 | + background: var(--color-bg-popup); |
| 89 | + border: 1px solid var(--color-border); |
| 90 | + border-radius: 8px; |
| 91 | + cursor: pointer; |
| 92 | + color: var(--color-text); |
| 93 | + font-family: var(--font-family); |
| 94 | + font-size: 0.8125rem; |
| 95 | + font-weight: 600; |
| 96 | + box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15); |
| 97 | + transition: background-color 0.12s ease, color 0.12s ease, border-color 0.12s ease; |
| 98 | + white-space: nowrap; |
| 99 | +} |
| 100 | +
|
| 101 | +.lang-btn:hover { |
| 102 | + background: var(--color-primary); |
| 103 | + color: #fff; |
| 104 | + border-color: var(--color-primary); |
| 105 | +} |
| 106 | +
|
| 107 | +.lang-btn:focus-visible { |
| 108 | + outline: 2px solid var(--color-focus); |
| 109 | + outline-offset: 2px; |
| 110 | +} |
| 111 | +
|
| 112 | +.lang-current { |
| 113 | + font-size: 0.75rem; |
| 114 | + letter-spacing: 0.03em; |
| 115 | +} |
| 116 | +
|
| 117 | +.lang-chevron { |
| 118 | + transition: transform 0.15s ease; |
| 119 | + opacity: 0.7; |
| 120 | +} |
| 121 | +
|
| 122 | +.lang-chevron--open { |
| 123 | + transform: rotate(180deg); |
| 124 | +} |
| 125 | +
|
| 126 | +.lang-dropdown { |
| 127 | + position: absolute; |
| 128 | + right: 0; |
| 129 | + top: calc(100% + 4px); |
| 130 | + list-style: none; |
| 131 | + margin: 0; |
| 132 | + padding: 4px; |
| 133 | + background: var(--color-bg-panel); |
| 134 | + border: 1px solid var(--color-border); |
| 135 | + border-radius: 8px; |
| 136 | + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); |
| 137 | + min-width: 160px; |
| 138 | + max-height: 320px; |
| 139 | + overflow-y: auto; |
| 140 | +} |
| 141 | +
|
| 142 | +.lang-option { |
| 143 | + padding: 8px 12px; |
| 144 | + font-size: 0.875rem; |
| 145 | + cursor: pointer; |
| 146 | + border-radius: 4px; |
| 147 | + color: var(--color-text); |
| 148 | + font-family: var(--font-family); |
| 149 | + list-style: none; |
| 150 | +} |
| 151 | +
|
| 152 | +.lang-option:hover { |
| 153 | + background: var(--color-border); |
| 154 | +} |
| 155 | +
|
| 156 | +.lang-option--active { |
| 157 | + color: var(--color-primary); |
| 158 | + font-weight: 600; |
| 159 | +} |
| 160 | +
|
| 161 | +@media (max-width: 480px) { |
| 162 | + .lang-current { |
| 163 | + display: none; |
| 164 | + } |
| 165 | +
|
| 166 | + .lang-chevron { |
| 167 | + display: none; |
| 168 | + } |
| 169 | +} |
| 170 | +</style> |
0 commit comments