diff --git a/stack.js b/stack.js index 77683f4..e52f9f8 100644 --- a/stack.js +++ b/stack.js @@ -211,12 +211,32 @@ const announce = (link) => { document.dispatchEvent(new CustomEvent('stack:active', { detail: { tool: link?.dataset.layer ?? null, link } })); }; + // On a touch screen a tap picks a layer instead of opening its site: the layer lights up and + // the tool card below switches to it, and the card links to the site. Tapping anywhere else + // puts the layer out. A mouse click, Enter or a screen reader's activation still opens it. + let tapped = null; for (const layer of layers) { - layer.link.addEventListener('pointerenter', () => announce(layer.link)); - layer.link.addEventListener('pointerleave', () => announce(null)); + layer.link.addEventListener('pointerenter', (event) => { if (event.pointerType !== 'touch') announce(layer.link); }); + layer.link.addEventListener('pointerleave', (event) => { if (event.pointerType !== 'touch') announce(null); }); + layer.link.addEventListener('pointerdown', (event) => { + tapped = event.pointerType === 'touch' ? layer.link : null; + }); + layer.link.addEventListener('click', (event) => { + const tap = tapped; + tapped = null; + if (tap !== layer.link) return; + event.preventDefault(); + // Safari doesn't focus a tapped link; the tool cards pick a focused layer straight away. + layer.link.focus({ preventScroll: true }); + announce(layer.link); + }); layer.link.addEventListener('focus', () => announce(layer.link)); layer.link.addEventListener('blur', () => announce(null)); } + document.addEventListener('pointerdown', (event) => { + if (event.pointerType !== 'touch' || event.target.closest?.('.stack-labels a')) return; + if (layers.some((layer) => layer.link.classList.contains('is-active'))) announce(null); + }); new ResizeObserver(() => { layout(); diff --git a/tests/website.spec.mjs b/tests/website.spec.mjs index ffc1ad1..919241e 100644 --- a/tests/website.spec.mjs +++ b/tests/website.spec.mjs @@ -175,3 +175,33 @@ for (const motion of ['no-preference', 'reduce']) { expect(await frame() !== first).toBe(motion === 'no-preference'); }); } + +for (const input of ['touch', 'mouse']) { + test(`a stack layer ${input === 'touch' ? 'is picked by a tap' : 'opens its site on a click'} with a ${input}`, async ({ browser }) => { + const context = await browser.newContext(input === 'touch' + ? { viewport: { width: 412, height: 915 }, isMobile: true, hasTouch: true } + : { viewport: { width: 1280, height: 800 } }); + await context.route('https://sqlbuild.com/**', (route) => route.fulfill({ body: 'SQLBuild' })); + const page = await context.newPage(); + await page.goto('/'); + const layer = page.locator('.stack-labels a[data-layer="sqlload"]').first(); + const box = await layer.boundingBox(); + const at = [box.x + box.width / 2, box.y + box.height / 2]; + + if (input === 'mouse') { + await page.locator('.stack-labels a[data-layer="sqlbuild"]').first().click(); + await page.waitForURL('https://sqlbuild.com/'); + } else { + for (let tap = 0; tap < 2; tap += 1) { + await page.touchscreen.tap(...at); + await expect(layer).toHaveClass(/is-active/); + await expect(layer.locator('.name')).toBeVisible(); + await expect(page.locator('article.tool.sqlload')).toHaveClass(/is-active/); + expect(new URL(page.url()).hostname).toBe('127.0.0.1'); + } + await page.touchscreen.tap(8, 8); + await expect(layer).not.toHaveClass(/is-active/); + } + await context.close(); + }); +}