-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathVueSkipToList.vue
62 lines (55 loc) · 1.38 KB
/
VueSkipToList.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
<template>
<nav class="vue-skip-to__nav">
<span id="list-title">{{ listLabel }}</span>
<ul class="vue-skip-to__nav-list">
<li
v-for="el in to"
:key="el.anchor"
class="vue-skip-to__nav-list-item"
@keydown.prevent.up="handleKeydown"
@keydown.prevent.down="handleKeydown"
>
<VueSkipToSingle
:to="el.anchor"
:aria-label="el.ariaLabel || `${listLabel} ${el.label}`"
>
{{ el.label }}
</VueSkipToSingle>
</li>
</ul>
</nav>
</template>
<script>
import VueSkipToSingle from './VueSkipToSingle.vue'
export default {
name: 'VueSkipToList',
props: {
listLabel: {
type: String,
default: 'Skip to'
},
to: {
validator: function (val) {
return Array.isArray(val) &&
val.every(({ anchor, label }) => (
typeof anchor === 'string' &&
anchor.startsWith('#') &&
typeof String(label) === 'string'
))
}
}
},
components: {
VueSkipToSingle
},
methods: {
handleKeydown ({ key, target }) {
const parent = target.parentElement
const itemList = key === 'ArrowUp' ? parent.previousElementSibling : parent.nextElementSibling
if (!itemList) return
const link = itemList.getElementsByTagName('a')
if (link.length) link[0].focus()
}
}
}
</script>