Skip to content

Commit f99ce7a

Browse files
authored
Keep the keyboard focus in the deck when its revealjs slide is left (#15089)
Since #15010 a slide that is left becomes inert, and when it held the keyboard focus, for example on a link the reader tabbed to, the browser dropped the focus to the body. Screen readers pass the arrow keys on to reveal.js only while the focus is in the deck, so after one slide change NVDA and JAWS went back to browse mode until Tab was pressed again. updateInertSlides() now moves the focus to the deck, made focusable with tabindex="-1", before it makes the slide that has the focus inert. A focus outside the slide being left is not touched, and slidechanged handlers that focus something on the new slide still can, since this runs first, in the capture phase. Fixes #15078 Co-authored-by: JooYoung Seo <19754711+jooyoungseo@users.noreply.github.com>
1 parent ce3ea0d commit f99ce7a

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

‎src/resources/formats/revealjs/plugins/support/support.js‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,22 @@ window.QuartoSupport = function () {
360360

361361
// Prevent offscreen slides from receiving keyboard focus.
362362
// Mirrors reveal.js 6.0.2 (hakimel/reveal.js#1587); remove this block
363-
// when the bundled reveal.js is >= 6.0.2.
363+
// when the bundled reveal.js is >= 6.0.2, but keep moving the focus to the
364+
// deck unless reveal.js does by then: 6.0.2 drops it to the body.
364365
function updateInertSlides(deck) {
365366
const allowAllSlides = deck.isOverview() || deck.isScrollView() || deck.isPrintView();
366367
const currentSlide = deck.getCurrentSlide();
367-
deck.getRevealElement().querySelectorAll('.slides section').forEach((slide) => {
368-
slide.toggleAttribute(
369-
'inert',
370-
!allowAllSlides && slide !== currentSlide && !slide.contains(currentSlide)
371-
);
368+
const revealElement = deck.getRevealElement();
369+
revealElement.querySelectorAll('.slides section').forEach((slide) => {
370+
const inert = !allowAllSlides && slide !== currentSlide && !slide.contains(currentSlide);
371+
// Making the slide that has the focus inert would drop the focus to the
372+
// body, where screen readers stop passing the arrow keys on to the deck
373+
// (#15078). Move it to the deck first, so it stays in the presentation.
374+
if (inert && slide.contains(document.activeElement)) {
375+
if (!revealElement.hasAttribute('tabindex')) revealElement.setAttribute('tabindex', '-1');
376+
revealElement.focus({ preventScroll: true });
377+
}
378+
slide.toggleAttribute('inert', inert);
372379
});
373380
}
374381

‎tests/integration/playwright/tests/revealjs.spec.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,45 @@ test.describe('off-screen slides are not focusable (#14795)', () => {
198198
});
199199
});
200200

201+
test.describe('the focus stays in the deck when its slide is left (#15078)', () => {
202+
// Not getByRole: non-present slides carry aria-hidden, which getByRole skips.
203+
const slideButton = (page: Page, id: string) =>
204+
page.locator('button', { hasText: `Button on ${id}` });
205+
const deck = (page: Page) => page.locator('.reveal');
206+
207+
test.beforeEach(async ({ page }) => {
208+
await page.goto('./revealjs/offscreen-slides-focus.html#/slide-2');
209+
await expect(page.locator('.reveal.ready')).toBeAttached();
210+
});
211+
212+
test('moving on from the slide that has the focus hands it to the deck', async ({ page }) => {
213+
await slideButton(page, 'slide-2').focus();
214+
await page.keyboard.press('ArrowRight');
215+
await expect(page).toHaveURL(/#\/slide-3$/);
216+
await expect(deck(page)).toBeFocused();
217+
await expect(deck(page)).toHaveAttribute('tabindex', '-1');
218+
// From the deck, Tab goes on into the new slide.
219+
await page.keyboard.press('Tab');
220+
await expect(slideButton(page, 'slide-3')).toBeFocused();
221+
});
222+
223+
test('moving on leaves a focus that was not on the slide alone', async ({ page }) => {
224+
await page.keyboard.press('ArrowRight');
225+
await expect(page).toHaveURL(/#\/slide-3$/);
226+
await expect(deck(page)).not.toBeFocused();
227+
await expect(deck(page)).not.toHaveAttribute('tabindex');
228+
});
229+
230+
test('leaving the overview hands a focus on another slide to the deck', async ({ page }) => {
231+
await page.keyboard.press('o');
232+
await expect(page.locator('.reveal.overview')).toBeAttached();
233+
await slideButton(page, 'slide-5').focus();
234+
await page.keyboard.press('Escape');
235+
await expect(page.locator('.reveal.overview')).toHaveCount(0);
236+
await expect(deck(page)).toBeFocused();
237+
});
238+
});
239+
201240
// https://github.com/quarto-dev/quarto-cli/issues/15011
202241
test.describe('the closed slide menu is out of the tab order', () => {
203242
const deck = './revealjs/menu-focus-order.html';

0 commit comments

Comments
 (0)