-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
VPSkipLink.vue
68 lines (59 loc) · 1.27 KB
/
VPSkipLink.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<script lang="ts" setup>
import { ref, watch } from 'vue'
import { useRoute } from 'vitepress'
const route = useRoute()
const backToTop = ref()
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>
<template>
<span ref="backToTop" tabindex="-1" />
<a
href="#VPContent"
class="VPSkipLink visually-hidden"
@click="focusOnTargetAnchor"
>
Skip to content
</a>
</template>
<style scoped>
.VPSkipLink {
top: 8px;
left: 8px;
padding: 8px 16px;
z-index: 999;
border-radius: 8px;
font-size: 12px;
font-weight: bold;
text-decoration: none;
color: var(--vp-c-brand-1);
box-shadow: var(--vp-shadow-3);
background-color: var(--vp-c-bg);
}
.VPSkipLink:focus {
height: auto;
width: auto;
clip: auto;
clip-path: none;
}
@media (min-width: 1280px) {
.VPSkipLink {
top: 14px;
left: 16px;
}
}
</style>