fix(a11y): improve focus handling in router

pull/4943/head
Divyansh Singh 1 week ago
parent 850c429f14
commit b3f6772df7

@ -261,19 +261,15 @@ export function scrollTo(hash: string, smooth = false, scrollPosition = 0) {
return return
} }
let target: Element | null = null let target: HTMLElement | null = null
try { try {
target = document.getElementById(decodeURIComponent(hash).slice(1)) target = document.getElementById(decodeURIComponent(hash).slice(1))
} catch (e) { } catch (e) {
console.warn(e) console.warn(e)
} }
if (!target) return
if (target) { const targetPadding = parseInt(window.getComputedStyle(target).paddingTop, 10)
const targetPadding = parseInt(
window.getComputedStyle(target).paddingTop,
10
)
const targetTop = const targetTop =
window.scrollY + window.scrollY +
@ -281,16 +277,30 @@ export function scrollTo(hash: string, smooth = false, scrollPosition = 0) {
getScrollOffset() + getScrollOffset() +
targetPadding targetPadding
function scrollToTarget() { const scrollToTarget = () => {
// only smooth scroll if distance is smaller than screen height. // only smooth scroll if distance is smaller than screen height.
if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight) if (!smooth || Math.abs(targetTop - window.scrollY) > window.innerHeight)
window.scrollTo(0, targetTop) window.scrollTo(0, targetTop)
else window.scrollTo({ left: 0, top: targetTop, behavior: 'smooth' }) else window.scrollTo({ left: 0, top: targetTop, behavior: 'smooth' })
// focus the target element for better accessibility
target.focus({ preventScroll: true })
if (document.activeElement === target) return
// target is not focusable, make it temporarily focusable
target.setAttribute('tabindex', '-1')
target.addEventListener(
'blur',
() => {
target.removeAttribute('tabindex')
},
{ once: true }
)
target.focus({ preventScroll: true })
} }
requestAnimationFrame(scrollToTarget) requestAnimationFrame(scrollToTarget)
} }
}
function handleHMR(route: Route): void { function handleHMR(route: Route): void {
// update route.data on HMR updates of active page // update route.data on HMR updates of active page

@ -5,18 +5,12 @@ defineProps<{
headers: DefaultTheme.OutlineItem[] headers: DefaultTheme.OutlineItem[]
root?: boolean root?: boolean
}>() }>()
function onClick({ target: el }: Event) {
const id = (el as HTMLAnchorElement).href!.split('#')[1]
const heading = document.getElementById(decodeURIComponent(id))
heading?.focus({ preventScroll: true })
}
</script> </script>
<template> <template>
<ul class="VPDocOutlineItem" :class="root ? 'root' : 'nested'"> <ul class="VPDocOutlineItem" :class="root ? 'root' : 'nested'">
<li v-for="{ children, link, title } in headers"> <li v-for="{ children, link, title } in headers">
<a class="outline-link" :href="link" @click="onClick" :title> <a class="outline-link" :href="link" :title>
{{ title }} {{ title }}
</a> </a>
<template v-if="children?.length"> <template v-if="children?.length">

@ -8,39 +8,18 @@ const route = useRoute()
const backToTop = ref() const backToTop = ref()
watch(() => route.path, () => backToTop.value.focus()) watch(() => route.path, () => backToTop.value.focus())
function focusOnTargetAnchor({ target }: Event) {
const el = document.getElementById(
decodeURIComponent((target as HTMLAnchorElement).hash).slice(1)
)
if (el) {
const removeTabIndex = () => {
el.removeAttribute('tabindex')
el.removeEventListener('blur', removeTabIndex)
}
el.setAttribute('tabindex', '-1')
el.addEventListener('blur', removeTabIndex)
el.focus()
window.scrollTo(0, 0)
}
}
</script> </script>
<template> <template>
<span ref="backToTop" tabindex="-1" /> <span ref="backToTop" tabindex="-1" />
<a <a href="#VPContent" class="VPSkipLink visually-hidden">
href="#VPContent"
class="VPSkipLink visually-hidden"
@click="focusOnTargetAnchor"
>
{{ theme.skipToContentLabel || 'Skip to content' }} {{ theme.skipToContentLabel || 'Skip to content' }}
</a> </a>
</template> </template>
<style scoped> <style scoped>
.VPSkipLink { .VPSkipLink {
position: fixed;
top: 8px; top: 8px;
left: 8px; left: 8px;
padding: 8px 16px; padding: 8px 16px;

Loading…
Cancel
Save