Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Add LocaleSwitch component #141

Merged
merged 10 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/web/app/components/FooterPageSection.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ const snsLinkList: LinkList[] = [
const internalLinkList: LinkList[] = [
{
href: '/privacy',
text: t('privacy.title'),
text: 'privacy.title',
},
{
href: '/code-of-conduct',
text: t('code_of_conduct.title'),
text: 'code_of_conduct.title',
},
]
const vueFesLinkList: LinkList[] = [
Expand Down Expand Up @@ -86,14 +86,14 @@ const vueFesLinkList: LinkList[] = [
<ul class="footer-list">
<li v-for="(link, index) in internalLinkList" :key="index">
<VFTextLink :href="`${localePath}${link.href}`" color="white">
{{ link.text }}
{{ t(link.text) }}
</VFTextLink>
</li>
</ul>
<ul class="footer-list">
<li v-for="(link, index) in vueFesLinkList" :key="index">
<VFTextLink :href="link.href" target="_blank" color="white">
{{ link.text }}
{{ t(link.text) }}
</VFTextLink>
</li>
</ul>
Expand Down
18 changes: 17 additions & 1 deletion packages/ui/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { setup } from '@storybook/vue3'
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport'
import { createI18n } from 'vue-i18n'
import { createRouter, createMemoryHistory } from 'vue-router'

import './global.css'

Expand Down Expand Up @@ -33,14 +34,29 @@ const i18n = createI18n({
// end dummy
sponsor: {
start_date: 'April 8',
end_date: '25,',
end_date: '25,',
},
},
},
})

const routes = [
{
path: '/',
name: 'ja',
},
{
path: '/en',
name: 'en',
},
]

// 画面遷移は発生しないが、StorybookではURLを直接使えないため、createMemoryHistoryで生成
const router = createRouter({ history: createMemoryHistory(), routes })

setup((app) => {
app.use(i18n)
app.use(router)
})

export const parameters = {
Expand Down
17 changes: 17 additions & 0 deletions packages/ui/components/common/LocaleSwitch.stories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StoryFn } from '@storybook/vue3'
import LocaleSwitch from './LocaleSwitch.vue'

export default {
title: 'common/LocaleSwitch',
component: LocaleSwitch,
}

const Template: StoryFn<unknown> = () => ({
components: { LocaleSwitch },
setup() {
return {}
},
template: '<LocaleSwitch />',
})

export const Default = Template.bind({})
148 changes: 148 additions & 0 deletions packages/ui/components/common/LocaleSwitch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<script setup lang="ts">
import { onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'

const LANGUAGES = {
JAPANESE: 'ja',
ENGLISH: 'en',
} as const

const router = useRouter()
const isLoaded = ref(false)
const isChecked = ref(false)

const getPath = () => {
if (isChecked.value) {
return `/${LANGUAGES.ENGLISH}${router.currentRoute.value.path}`
}
return router.currentRoute.value.path.replace(`/${LANGUAGES.ENGLISH}`, '')
}
const setSwitchStatus = () => {
isChecked.value = router.currentRoute.value.path.includes(LANGUAGES.ENGLISH)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pathのなかに en があるだけでマッチしそうなので、 /en/ とかにしたほうがよいよーなきがします
たとえば /children とかでも 英語だと判断される?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

探索的に確認はしていないですが、トップページでhttps://vuefes.jp/2024/enとなるケースがないのであれば、確かに/en/の方が妥当だと思います。
説明にも書きましたが、トレイリングスラッシュの対応がされている場合は以下の処理を削除できます。

  // トレイリングスラッシュ対策
  if (path === '') {
    router.push('/')
    return
  }

}
KannoStanfoot marked this conversation as resolved.
Show resolved Hide resolved

const toggleStatus = () => {
isChecked.value = !isChecked.value
const path = getPath()
// トレイリングスラッシュ対策
if (path === '') {
router.push('/')
return
}
router.push(path)
}
onMounted(() => {
setSwitchStatus()
isLoaded.value = true
})
watch(
() => router.currentRoute.value.path,
() => {
setSwitchStatus()
},
)
</script>

<template>
<button
v-if="isLoaded"
type="button"
role="switch"
class="locale-switch-button"
aria-label="translate english"
:aria-checked="isChecked"
@click="toggleStatus"
>
<span
class="locale-switch-button-switch"
:class="{ 'locale-switch-button-switch-checked': !isChecked }"
aria-hidden="true"
>
<span
v-if="isChecked"
class="locale-switch-button-language"
:class="{ 'locale-switch-button-language-checked': !isChecked }"
>
<span>{{ LANGUAGES.JAPANESE }}</span>
</span>
<span class="locale-switch-button-circle">
{{ isChecked ? LANGUAGES.ENGLISH : LANGUAGES.JAPANESE }}
</span>
<span
v-if="!isChecked"
class="locale-switch-button-language"
:class="{ 'locale-switch-button-language-checked': isChecked }"
>
<span>{{ LANGUAGES.ENGLISH }}</span>
</span>
</span>
</button>
</template>

<style scoped>
* {
--box-shadow: 0 1px 4px color-mix(in srgb, var(--color-vue-blue), transparent calc(100% - 14%));
}
Copy link
Contributor

@toshick toshick May 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全タグに対して変数宣言しているよーに見えますが、なにかこれが悪さしないかちょっとだけ心配です。
でも、動いていればとりあえずokです。
(もしくはこれが一般的な書き方?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scopedなのであくまで影響範囲はLocaleSwitchコンポーネント内のため、問題なしと判断しました。
一般的かどうかまでは調べきれていないので、コンポーネント全体への定義で他に一般的な使用方法があればそちらを検討したいです。(locale-switch-buttonへの定義でも構わない)


.locale-switch-button {
border: 0;
user-select: none;
line-height: 1.2;
width: 5.125rem;
height: 2.5rem;
display: grid;
place-content: center;
color: var(--color-white);
background-color: transparent;
position: relative;
}

.locale-switch-button-switch {
display: flex;
align-items: center;
box-shadow: var(--box-shadow);
background-color: #d2d6db;
border-radius: 0.9375rem;
height: 1.8125rem;
width: 3.5625rem;
font-size: 1.125rem;
font-weight: bold;
padding-left: 0.5rem;
box-sizing: border-box;
}
KannoStanfoot marked this conversation as resolved.
Show resolved Hide resolved

.locale-switch-button-switch-checked {
justify-content: flex-end;
padding-left: 0;
padding-right: 0.5rem;
}

.locale-switch-button-language {
font-size: 0.875rem;
font-weight: bold;
}

.locale-switch-button-circle {
position: absolute;
display: flex;
justify-content: center;
padding-top: 0.25rem;
box-sizing: border-box;
border-radius: 50%;
top: calc(50% - 1rem);
right: 50%;
height: 2rem;
width: 2rem;
background: #34495e;
box-shadow: var(--box-shadow);
}

.locale-switch-button[aria-checked='true'] .locale-switch-button-circle {
right: 0;
left: 50%;
}

.locale-switch-button:hover {
cursor: pointer;
}
</style>
Loading