Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/packs.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packs": {
"foundation": {
"version": "0.1.0",
"digest": "6a1ca1071d4664468a552798a0d8b8997ab68fd56aff8d5f2aeed3a196f04ee6"
"digest": "b1f1e7339b60aeb2ce529aa5596dd17fafacf73bb54dad577a17774238f298bd"
},
"foundation-alt": {
"version": "0.1.0",
Expand Down
2 changes: 1 addition & 1 deletion resources/packs/foundation/pack.json

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion src/generator/runtime-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ export const ACTION_SVELTE = `<script>
let anchor = $state();
let dialog = $state();
let caption = $state();
// Whether the caption needs its own scrolling area: Safari and other WebKit browsers leave such an area out of the
// Tab order, so it becomes a stop (and the arrow, Page and End keys scroll it) only then.
let captionScrolls = $state(false);
let slides = $state([]);
let index = $state(0);
// The link that opened the viewer, which gets focus back however far the visitor moves through the group.
Expand Down Expand Up @@ -239,6 +242,20 @@ export const ACTION_SVELTE = `<script>
if (current && caption) caption.scrollTop = 0;
});

$effect(() => {
if (!current || !caption) {
captionScrolls = false;
return;
}
const measure = () => {
captionScrolls = caption.scrollHeight > caption.clientHeight;
};
measure();
const observer = new ResizeObserver(measure);
observer.observe(caption);
return () => observer.disconnect();
});

// A viewer whose photo stops showing (a gallery hidden at the new screen width) would be an invisible modal: it closes.
$effect(() => {
if (!current) return;
Expand All @@ -260,6 +277,8 @@ export const ACTION_SVELTE = `<script>
}

function go(step) {
// A focused caption goes away with a photo that has none: focus moves to Close first, so the arrow keys keep working.
if (caption && document.activeElement === caption && !slides[(index + step + slides.length) % slides.length].caption) dialog?.querySelector('.so-viewer__close')?.focus();
index = (index + step + slides.length) % slides.length;
}

Expand Down Expand Up @@ -308,7 +327,7 @@ export const ACTION_SVELTE = `<script>
</button>
<figure class="so-viewer__figure">
<img class="so-viewer__img" src={current.src} alt={current.alt} width={current.width} height={current.height} decoding="async" />
{#if current.caption}<figcaption bind:this={caption} class="so-viewer__caption">{current.caption}</figcaption>{/if}
{#if current.caption}<!-- svelte-ignore a11y_no_noninteractive_tabindex --><figcaption bind:this={caption} class="so-viewer__caption" tabindex={captionScrolls ? 0 : undefined}>{current.caption}</figcaption>{/if}
</figure>
{#if slides.length > 1}
<div class="so-viewer__nav">
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/gallery-viewer-browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,27 @@ describe.skipIf(!browserInstalled)('the full-image viewer moving through a galle
}
});

it('makes the caption a keyboard stop only while it needs its own scrolling area (#1430)', async () => {
const { page, problems } = await open();
try {
await page.locator('a.so-action--image').first().click();
await page.waitForSelector('dialog.so-viewer[open]');
const tab = () => page.evaluate(`document.querySelector('dialog.so-viewer[open] .so-viewer__caption')?.getAttribute('tabindex') ?? null`);
await expect.poll(tab).toBe('0');
// Close has focus on opening; the caption is the next stop, and the keys scroll it.
await page.keyboard.press('Tab');
expect(await page.evaluate(`document.activeElement?.classList.contains('so-viewer__caption')`)).toBe(true);
await page.keyboard.press('End');
await expect.poll(async () => (await captionOf(page)).top).toBeGreaterThan(0);
// A tall window fits the whole caption: no extra stop.
await page.setViewportSize({ width: 1200, height: 3000 });
await expect.poll(tab).toBe(null);
expect(problems).toEqual([]);
} finally {
await page.close();
}
});

it('keeps focus on a caption the visitor is reading while the arrow keys change the photo', async () => {
const { page, problems } = await open();
try {
Expand Down
Loading