Skip to content

Commit 925e650

Browse files
committed
Make overflowing code containers keyboard focusable on mount and route change
1 parent 61c642e commit 925e650

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

docs/.vuepress/theme/layouts/Layout.vue

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ import {
170170
getPageWithRelativePath,
171171
fixDoubleSlashes,
172172
getSameContentForVersion,
173+
makeOverflowingContainersFocusable,
173174
} from "../util";
174175
175176
import { getStorage, setStorage, unsetStorage } from "../Storage";
@@ -187,7 +188,10 @@ export default {
187188
188189
watch: {
189190
'$route.path'() {
190-
this.$refs.backToTop.focus()
191+
this.$refs.backToTop.focus();
192+
this.$nextTick(function () {
193+
makeOverflowingContainersFocusable();
194+
});
191195
}
192196
},
193197
@@ -282,18 +286,12 @@ export default {
282286
283287
this.$router.afterEach(() => {
284288
this.isSidebarOpen = false;
285-
this.$nextTick(() => {
286-
this.makeScrollingCodeBlocksFocusable();
287-
});
288-
289289
});
290290
291-
this.$nextTick(() => {
292-
this.makeScrollingCodeBlocksFocusable();
291+
this.$nextTick(function () {
292+
makeOverflowingContainersFocusable();
293293
});
294294
295-
this.makeScrollingCodeBlocksFocusable();
296-
297295
// temporary means of scrolling to URL hash on load
298296
// https://github.com/vuejs/vuepress/issues/2428
299297
const hash = document.location.hash;
@@ -374,14 +372,6 @@ export default {
374372
},
375373
376374
methods: {
377-
makeScrollingCodeBlocksFocusable() {
378-
document.querySelectorAll('pre').forEach(el => {
379-
if (el.scrollWidth > el.clientWidth) {
380-
el.setAttribute('tabindex', '0');
381-
}
382-
});
383-
},
384-
385375
toggleSidebar(to) {
386376
this.temporarilyAnimateBody();
387377
this.isSidebarOpen = typeof to === "boolean" ? to : !this.isSidebarOpen;

docs/.vuepress/theme/util/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,21 @@ function getAnchorHash(path) {
623623
return false;
624624
}
625625

626+
/**
627+
* Adds tabindex="0" to all containers (e.g., <pre>, <div>, etc.) that have overflowing content,
628+
* making them focusable for keyboard navigation accessibility.
629+
*
630+
* This utility is typically used to improve accessibility for code blocks or other scrollable regions,
631+
* allowing users to focus and scroll them using the keyboard.
632+
*/
633+
export function makeOverflowingContainersFocusable() {
634+
document.querySelectorAll('pre,.toggle-tip .wrapper,.viewport.limit-height').forEach(el => {
635+
if (el.scrollWidth > el.clientWidth || el.scrollHeight > el.clientHeight) {
636+
el.setAttribute('tabindex', '0');
637+
}
638+
});
639+
}
640+
626641
/**
627642
* Returns doc set base paths combinations with their configs,
628643
* accounting for set base, version, and/or language.

0 commit comments

Comments
 (0)