Skip to content

Commit 4c5d4bc

Browse files
committed
feat: Set focus on SkipTo whenever the route path is changed
1 parent fdea86d commit 4c5d4bc

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/VueSkipTo.vue

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<template>
22
<div
33
class="vue-skip-to"
4+
ref="skipTo"
45
:class="{ 'focused': focused }"
56
>
67
<component
@@ -15,6 +16,7 @@
1516
<script>
1617
import VueSkipToSingle from './VueSkipToSingle.vue'
1718
import VueSkipToList from './VueSkipToList.vue'
19+
import { programmaticFocus } from './util'
1820
1921
export default {
2022
name: 'VueSkipTo',
@@ -40,6 +42,12 @@ export default {
4042
}
4143
},
4244
45+
mounted () {
46+
this.$route && this.$watch('this.$route.path', () => {
47+
this.$nextTick(() => programmaticFocus(this.$refs.skipTo))
48+
})
49+
},
50+
4351
computed: {
4452
isList () {
4553
return Array.isArray(this.to)

src/util.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function programmaticFocus (el) {
2+
el.setAttribute('tabindex', '-1')
3+
el.focus()
4+
el.removeAttribute('tabindex')
5+
}

0 commit comments

Comments
 (0)