Skip to content

Commit

Permalink
Change the way focus is set in skip link example code (#3164)
Browse files Browse the repository at this point in the history
update skip link example code
  • Loading branch information
zacharesmer authored Feb 3, 2025
1 parent c0e81ef commit 7119fae
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/guide/best-practices/accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ You should add a link at the top of each page that goes directly to the main con
Typically this is done on the top of `App.vue` as it will be the first focusable element on all your pages:

```vue-html
<span ref="backToTop" tabindex="-1" />
<ul class="skip-links">
<li>
<a href="#main" ref="skipLink" class="skip-link">Skip to main content</a>
Expand All @@ -23,6 +24,9 @@ Typically this is done on the top of `App.vue` as it will be the first focusable
To hide the link unless it is focused, you can add the following style:

```css
.skip-links {
list-style: none;
}
.skip-link {
white-space: nowrap;
margin: 1em auto;
Expand All @@ -40,7 +44,7 @@ To hide the link unless it is focused, you can add the following style:
}
```

Once a user changes route, bring focus back to the skip link. This can be achieved by calling focus on the skip link's template ref (assuming usage of `vue-router`):
Once a user changes route, bring focus back to the very beginning of the page, right before the skip link. This can be achieved by calling focus on the `backToTop` template ref (assuming usage of `vue-router`):

<div class="options-api">

Expand All @@ -49,7 +53,7 @@ Once a user changes route, bring focus back to the skip link. This can be achiev
export default {
watch: {
$route() {
this.$refs.skipLink.focus()
this.$refs.backToTop.focus()
}
}
}
Expand All @@ -65,12 +69,12 @@ import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const skipLink = ref()
const backToTop = ref()
watch(
() => route.path,
() => {
skipLink.value.focus()
backToTop.value.focus()
}
)
</script>
Expand Down

0 comments on commit 7119fae

Please sign in to comment.