forked from incluud/accessible-astro-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkipLinks.astro
68 lines (61 loc) · 1.48 KB
/
SkipLinks.astro
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
<div>
<nav class="skip-links" aria-label="Skiplinks">
<ul>
<li>
<a href="#main-navigation">
Skip to main menu
</a>
</li>
<li>
<a href="#main-content">
Skip to main content
</a>
</li>
</ul>
</nav>
</div>
<script type="module">
// variables
const skipLinks = [...document.querySelectorAll('.skip-links a')]
// execution
if (skipLinks.length > 0) {
skipLinks.forEach(link => {
link.addEventListener('keydown', event => {
if (!event.target.closest('a')) return
const key = event.key
if (key !== 'Enter') return
event.preventDefault()
const target = event.target.getAttribute('href')
if (target === '#main-navigation') {
const navigation = document.querySelector('#main-navigation')
navigation.querySelector('a').focus()
}
if (target === '#main-content') {
const h1 = document.querySelector('h1')
h1.setAttribute('tabindex', '-1')
h1.focus()
}
})
})
}
</script>
<style lang="scss" is:global>
.skip-links {
a {
color: var(--action-color, #222);
background-color: var(--background, #fff);
border-bottom-right-radius: 6px;
padding: 1rem 3.25rem;
position: absolute;
display: block;
z-index: 10;
top: -100vh;
left: 0;
&:hover,
&:focus {
top: 0;
outline-offset: 2px;
}
}
}
</style>