gradido/src/components/SwitchLang.vue

126 lines
4.3 KiB
Vue

<template>
<div class="relative">
<button
href="#"
class="flex items-center"
@click="toggleVisibility"
@keydown.space.exact.prevent="toggleVisibility"
@keydown.esc.exact="hideDropdown"
@keydown.shift.tab="hideDropdown"
@keydown.up.exact.prevent="startArrowKeys"
@keydown.down.exact.prevent="startArrowKeys"
>
<img :src="`/flag_${$i18n.locale}.svg`" alt="flag" class="w-8 h-8">
<span class="ml-2">{{ $i18n.locale.toUpperCase() }}</span>
<svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path class="heroicon-ui" d="M15.3 9.3a1 1 0 0 1 1.4 1.4l-4 4a1 1 0 0 1-1.4 0l-4-4a1 1 0 0 1 1.4-1.4l3.3 3.29 3.3-3.3z"></path></svg>
</button>
<transition name="dropdown-fade">
<ul v-on-clickaway="hideDropdown" v-if="isVisible" ref="dropdown" class="absolute normal-case z-30 font-normal xs:left-0 lg:right-0 bg-white shadow overflow-hidden rounded w-48 border mt-2 py-1 lg:z-20">
<li>
<a
href="#"
@click.prevent="setLocale('en')"
ref="account"
class="flex items-center px-3 py-3 hover:bg-gray-200"
@keydown.up.exact.prevent=""
@keydown.tab.exact="focusNext(false)"
@keydown.down.exact.prevent="focusNext(true)"
@keydown.esc.exact="hideDropdown"
>
<img src="/flag_en.svg" alt="english flag" class="h-8 w-8">
<span class="ml-2">English</span>
</a>
</li>
<!-- <li>
<a
href="#"
class="flex items-center px-3 py-3 hover:bg-gray-200"
@keydown.tab.exact="focusNext(false)"
@keydown.shift.tab="focusPrevious(false)"
@keydown.up.exact.prevent="focusPrevious(true)"
@keydown.down.exact.prevent="focusNext(true)"
@keydown.esc.exact="hideDropdown"
>
<svg fill="currentColor" class="text-gray-600" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"><path class="heroicon-ui" d="M12 22a10 10 0 1 1 0-20 10 10 0 0 1 0 20zm0-2a8 8 0 1 0 0-16 8 8 0 0 0 0 16zm1-11v2h1a3 3 0 0 1 0 6h-1v1a1 1 0 0 1-2 0v-1H8a1 1 0 0 1 0-2h3v-2h-1a3 3 0 0 1 0-6h1V6a1 1 0 0 1 2 0v1h3a1 1 0 0 1 0 2h-3zm-2 0h-1a1 1 0 1 0 0 2h1V9zm2 6h1a1 1 0 0 0 0-2h-1v2z"></path></svg>
<span class="ml-2">Billing</span>
</a>
</li> -->
<li>
<a
href="#"
@click.prevent="setLocale('de')"
class="flex items-center px-3 py-3 hover:bg-gray-200"
@keydown.shift.tab="focusPrevious(false)"
@keydown.up.exact.prevent="focusPrevious(true)"
@keydown.down.exact.prevent=""
@keydown.tab.exact="hideDropdown"
@keydown.esc.exact="hideDropdown"
>
<img src="/flag_de.svg" alt="english flag" class="h-8 w-8">
<span class="ml-2">Deutsch</span>
</a>
</li>
</ul>
</transition>
</div>
</template>
<script>
import { mixin as clickaway } from 'vue-clickaway'
export default {
mixins: [ clickaway ],
data() {
return {
isVisible: false,
focusedIndex: 0,
}
},
methods: {
toggleVisibility() {
this.isVisible = !this.isVisible
},
hideDropdown() {
this.isVisible = false
this.focusedIndex = 0
},
startArrowKeys() {
if (this.isVisible) {
// this.$refs.account.focus()
this.$refs.dropdown.children[0].children[0].focus()
}
},
focusPrevious(isArrowKey) {
this.focusedIndex = this.focusedIndex - 1
if (isArrowKey) {
this.focusItem()
}
},
focusNext(isArrowKey) {
this.focusedIndex = this.focusedIndex + 1
if (isArrowKey) {
this.focusItem()
}
},
focusItem() {
this.$refs.dropdown.children[this.focusedIndex].children[0].focus()
},
setLocale(locale) {
this.$i18n.locale = locale
this.$router.push({
params: { lang: locale }
})
this.hideDropdown()
}
}
}
</script>
<style scoped>
.dropdown-fade-enter-active, .dropdown-fade-leave-active {
transition: all .1s ease-in-out;
}
.dropdown-fade-enter, .dropdown-fade-leave-to {
opacity: 0;
transform: translateY(-12px);
}
</style>