2022-12-24 00:08:36 +09:00
|
|
|
<script setup lang="ts">
|
2024-02-25 01:46:14 +09:00
|
|
|
import type { LocaleObject } from '@nuxtjs/i18n'
|
2022-12-28 23:57:06 +09:00
|
|
|
import type { AriaAnnounceType, AriaLive } from '~/composables/aria'
|
2022-12-24 00:08:36 +09:00
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
const { t, locale, locales } = useI18n()
|
|
|
|
const { ariaAnnouncer, announce } = useAriaAnnouncer()
|
|
|
|
|
|
|
|
const localeMap = (locales.value as LocaleObject[]).reduce((acc, l) => {
|
|
|
|
acc[l.code!] = l.name!
|
|
|
|
return acc
|
|
|
|
}, {} as Record<string, string>)
|
|
|
|
|
2024-02-22 00:20:08 +09:00
|
|
|
const ariaLive = ref<AriaLive>('polite')
|
|
|
|
const ariaMessage = ref<string>('')
|
2022-12-24 00:08:36 +09:00
|
|
|
|
2023-03-31 04:01:24 +09:00
|
|
|
function onMessage(event: AriaAnnounceType, message?: string) {
|
2022-12-24 00:08:36 +09:00
|
|
|
if (event === 'announce')
|
2024-02-22 00:20:08 +09:00
|
|
|
ariaMessage.value = message!
|
2022-12-24 00:08:36 +09:00
|
|
|
else if (event === 'mute')
|
2024-02-22 00:20:08 +09:00
|
|
|
ariaLive.value = 'off'
|
2022-12-24 00:08:36 +09:00
|
|
|
else
|
2024-02-22 00:20:08 +09:00
|
|
|
ariaLive.value = 'polite'
|
2022-12-24 00:08:36 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
watch(locale, (l, ol) => {
|
|
|
|
if (ol) {
|
|
|
|
announce(t('a11y.locale_changing', [localeMap[ol] ?? ol]))
|
|
|
|
setTimeout(() => {
|
|
|
|
announce(t('a11y.locale_changed', [localeMap[l] ?? l]))
|
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
}, { immediate: true })
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
ariaAnnouncer.on(onMessage)
|
|
|
|
router.beforeEach(() => {
|
|
|
|
announce(t('a11y.loading_page'))
|
|
|
|
})
|
|
|
|
router.afterEach((to, from) => {
|
2024-08-16 23:52:08 +09:00
|
|
|
if (from) {
|
|
|
|
setTimeout(() => {
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
const title = document.title.trim().split('|')
|
|
|
|
announce(t('a11y.route_loaded', [title[0]]))
|
|
|
|
})
|
|
|
|
}, 512)
|
|
|
|
}
|
2022-12-24 00:08:36 +09:00
|
|
|
})
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<p sr-only role="status" :aria-live="ariaLive">
|
|
|
|
{{ ariaMessage }}
|
|
|
|
</p>
|
|
|
|
</template>
|