1
0
mirror of https://github.com/elk-zone/elk synced 2024-11-27 06:18:07 +09:00
elk/components/aria/AriaAnnouncer.vue
renovate[bot] 0fba07e6e5
chore(deps): update dependency @antfu/eslint-config to ^2.19.0 (#2726)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: TAKAHASHI Shuuji <shuuji3@gmail.com>
Co-authored-by: Daniel Roe <daniel@roe.dev>
2024-08-16 14:52:08 +00:00

58 lines
1.4 KiB
Vue

<script setup lang="ts">
import type { LocaleObject } from '@nuxtjs/i18n'
import type { AriaAnnounceType, AriaLive } from '~/composables/aria'
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>)
const ariaLive = ref<AriaLive>('polite')
const ariaMessage = ref<string>('')
function onMessage(event: AriaAnnounceType, message?: string) {
if (event === 'announce')
ariaMessage.value = message!
else if (event === 'mute')
ariaLive.value = 'off'
else
ariaLive.value = 'polite'
}
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) => {
if (from) {
setTimeout(() => {
requestAnimationFrame(() => {
const title = document.title.trim().split('|')
announce(t('a11y.route_loaded', [title[0]]))
})
}, 512)
}
})
})
</script>
<template>
<p sr-only role="status" :aria-live="ariaLive">
{{ ariaMessage }}
</p>
</template>