|
| 1 | +<script setup lang="ts"> |
| 2 | +import { onMounted, ref, watch } from 'vue' |
| 3 | +import { useRouter } from 'vue-router' |
| 4 | +
|
| 5 | +const LANGUAGES = { |
| 6 | + JAPANESE: 'ja', |
| 7 | + ENGLISH: 'en', |
| 8 | +} as const |
| 9 | +
|
| 10 | +const router = useRouter() |
| 11 | +const isLoaded = ref(false) |
| 12 | +const isChecked = ref(false) |
| 13 | +
|
| 14 | +const getPath = () => { |
| 15 | + if (isChecked.value) { |
| 16 | + return `/${LANGUAGES.ENGLISH}${router.currentRoute.value.path}` |
| 17 | + } |
| 18 | + return router.currentRoute.value.path.replace(`/${LANGUAGES.ENGLISH}`, '') |
| 19 | +} |
| 20 | +const setSwitchStatus = () => { |
| 21 | + isChecked.value = router.currentRoute.value.path.includes(`/${LANGUAGES.ENGLISH}/`) |
| 22 | +} |
| 23 | +
|
| 24 | +const toggleStatus = () => { |
| 25 | + isChecked.value = !isChecked.value |
| 26 | + const path = getPath() |
| 27 | + router.push(path) |
| 28 | +} |
| 29 | +onMounted(() => { |
| 30 | + setSwitchStatus() |
| 31 | + isLoaded.value = true |
| 32 | +}) |
| 33 | +watch( |
| 34 | + () => router.currentRoute.value.path, |
| 35 | + () => { |
| 36 | + setSwitchStatus() |
| 37 | + }, |
| 38 | +) |
| 39 | +</script> |
| 40 | + |
| 41 | +<template> |
| 42 | + <button |
| 43 | + v-if="isLoaded" |
| 44 | + type="button" |
| 45 | + role="switch" |
| 46 | + class="locale-switch-button" |
| 47 | + aria-label="translate english" |
| 48 | + :aria-checked="isChecked" |
| 49 | + @click="toggleStatus" |
| 50 | + > |
| 51 | + <span |
| 52 | + class="locale-switch-button-switch" |
| 53 | + :class="{ 'locale-switch-button-switch-checked': !isChecked }" |
| 54 | + aria-hidden="true" |
| 55 | + > |
| 56 | + <span |
| 57 | + v-if="isChecked" |
| 58 | + class="locale-switch-button-language" |
| 59 | + :class="{ 'locale-switch-button-language-checked': !isChecked }" |
| 60 | + > |
| 61 | + <span>{{ LANGUAGES.JAPANESE }}</span> |
| 62 | + </span> |
| 63 | + <span class="locale-switch-button-circle"> |
| 64 | + {{ isChecked ? LANGUAGES.ENGLISH : LANGUAGES.JAPANESE }} |
| 65 | + </span> |
| 66 | + <span |
| 67 | + v-if="!isChecked" |
| 68 | + class="locale-switch-button-language" |
| 69 | + :class="{ 'locale-switch-button-language-checked': isChecked }" |
| 70 | + > |
| 71 | + <span>{{ LANGUAGES.ENGLISH }}</span> |
| 72 | + </span> |
| 73 | + </span> |
| 74 | + </button> |
| 75 | +</template> |
| 76 | + |
| 77 | +<style scoped> |
| 78 | +.locale-switch-button { |
| 79 | + --box-shadow: 0 1px 4px color-mix(in srgb, var(--color-vue-blue), transparent calc(100% - 14%)); |
| 80 | +
|
| 81 | + border: 0; |
| 82 | + user-select: none; |
| 83 | + line-height: 1.2; |
| 84 | + width: 5.125rem; |
| 85 | + height: 2.5rem; |
| 86 | + display: grid; |
| 87 | + place-content: center; |
| 88 | + color: var(--color-white); |
| 89 | + background-color: transparent; |
| 90 | + position: relative; |
| 91 | +} |
| 92 | +
|
| 93 | +.locale-switch-button-switch { |
| 94 | + display: flex; |
| 95 | + align-items: center; |
| 96 | + box-shadow: var(--box-shadow); |
| 97 | + background-color: #d2d6db; |
| 98 | + border-radius: 0.9375rem; |
| 99 | + height: 1.8125rem; |
| 100 | + width: 3.5625rem; |
| 101 | + font-size: 1.125rem; |
| 102 | + font-weight: bold; |
| 103 | + padding-left: 0.5rem; |
| 104 | + box-sizing: border-box; |
| 105 | +} |
| 106 | +
|
| 107 | +.locale-switch-button-switch-checked { |
| 108 | + justify-content: flex-end; |
| 109 | + padding-left: 0; |
| 110 | + padding-right: 0.5rem; |
| 111 | +} |
| 112 | +
|
| 113 | +.locale-switch-button-language { |
| 114 | + font-size: 0.875rem; |
| 115 | + font-weight: bold; |
| 116 | +} |
| 117 | +
|
| 118 | +.locale-switch-button-circle { |
| 119 | + position: absolute; |
| 120 | + display: flex; |
| 121 | + justify-content: center; |
| 122 | + padding-top: 0.25rem; |
| 123 | + box-sizing: border-box; |
| 124 | + border-radius: 50%; |
| 125 | + top: calc(50% - 1rem); |
| 126 | + right: 50%; |
| 127 | + height: 2rem; |
| 128 | + width: 2rem; |
| 129 | + background: #34495e; |
| 130 | + box-shadow: var(--box-shadow); |
| 131 | +} |
| 132 | +
|
| 133 | +.locale-switch-button[aria-checked='true'] .locale-switch-button-circle { |
| 134 | + right: 0; |
| 135 | + left: 50%; |
| 136 | +} |
| 137 | +
|
| 138 | +.locale-switch-button:hover { |
| 139 | + cursor: pointer; |
| 140 | +} |
| 141 | +</style> |
0 commit comments