elk/components/aria/AriaAnnouncer.vue

56 lines
1.3 KiB
Vue
Raw Permalink Normal View History

2022-12-23 15:08:36 +00:00
<script setup lang="ts">
import type { AriaAnnounceType, AriaLive } from '~/composables/aria'
2023-01-09 20:20:26 +00:00
import { locale, locales } from '~/config/i18n'
2022-12-23 15:08:36 +00:00
const router = useRouter()
2023-01-09 20:20:26 +00:00
const { $t } = useFluent()
2022-12-23 15:08:36 +00:00
const { ariaAnnouncer, announce } = useAriaAnnouncer()
2023-01-09 20:20:26 +00:00
const localeMap = locales.reduce((acc, l) => {
2022-12-23 15:08:36 +00:00
acc[l.code!] = l.name!
return acc
}, {} as Record<string, string>)
let ariaLive = $ref<AriaLive>('polite')
let ariaMessage = $ref<string>('')
const onMessage = (event: AriaAnnounceType, message?: string) => {
if (event === 'announce')
ariaMessage = message!
else if (event === 'mute')
ariaLive = 'off'
else
ariaLive = 'polite'
}
watch(locale, (l, ol) => {
if (ol) {
2023-01-09 20:20:26 +00:00
announce($t('a11y_locale_changing', { lang: localeMap[ol] ?? ol }))
2022-12-23 15:08:36 +00:00
setTimeout(() => {
2023-01-09 20:20:26 +00:00
announce($t('a11y_locale_changed', { lang: localeMap[l] ?? l }))
2022-12-23 15:08:36 +00:00
}, 1000)
}
}, { immediate: true })
onMounted(() => {
ariaAnnouncer.on(onMessage)
router.beforeEach(() => {
2023-01-09 20:20:26 +00:00
announce($t('a11y_loading_page'))
2022-12-23 15:08:36 +00:00
})
router.afterEach((to, from) => {
from && setTimeout(() => {
requestAnimationFrame(() => {
const title = document.title.trim().split('|')
2023-01-09 20:20:26 +00:00
announce($t('a11y_route_loaded', { title: title[0] }))
2022-12-23 15:08:36 +00:00
})
}, 512)
})
})
</script>
<template>
<p sr-only role="status" :aria-live="ariaLive">
{{ ariaMessage }}
</p>
</template>