Skip to content

Commit 61c642e

Browse files
committed
Add method to Layout.vue to make scrolling code blocks keyboard-focusable
1 parent 3a8d52d commit 61c642e

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,18 @@ export default {
282282
283283
this.$router.afterEach(() => {
284284
this.isSidebarOpen = false;
285+
this.$nextTick(() => {
286+
this.makeScrollingCodeBlocksFocusable();
287+
});
288+
285289
});
286290
291+
this.$nextTick(() => {
292+
this.makeScrollingCodeBlocksFocusable();
293+
});
294+
295+
this.makeScrollingCodeBlocksFocusable();
296+
287297
// temporary means of scrolling to URL hash on load
288298
// https://github.com/vuejs/vuepress/issues/2428
289299
const hash = document.location.hash;
@@ -364,6 +374,14 @@ export default {
364374
},
365375
366376
methods: {
377+
makeScrollingCodeBlocksFocusable() {
378+
document.querySelectorAll('pre').forEach(el => {
379+
if (el.scrollWidth > el.clientWidth) {
380+
el.setAttribute('tabindex', '0');
381+
}
382+
});
383+
},
384+
367385
toggleSidebar(to) {
368386
this.temporarilyAnimateBody();
369387
this.isSidebarOpen = typeof to === "boolean" ? to : !this.isSidebarOpen;

0 commit comments

Comments
 (0)