Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .changeset/hit-area-unreachable-neighbour.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] Under a finger, a tap on a control's grown touch area runs the control when the nearer neighbour is one that no tap can reach. Before, a neighbour inside an `inert` subtree, disabled, hidden by `visibility` or by skipped content, or with `pointer-events: none` counted as nearer, so every tap at that spot was swallowed. A neighbour that takes taps, including one drawn at opacity 0, still keeps a tap nearer it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This promises that any neighbour no tap can reach stops swallowing the tap, but takesTaps handles five states only: at this head a neighbour behind an open modal dialog, or one covered by the control's own panel, still swallows it. Suggested first sentence: "Under a finger, a tap on a control's grown touch area runs the control when the nearer neighbour is disabled, inside an inert subtree, hidden by visibility or skipped content, or has pointer-events: none." Then say that a neighbour behind an open modal dialog or under another element still keeps a tap nearer it, and match the title.


@vjeux
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file hitAreaUnreachableNeighbour.interaction.browser.spec.ts
* @input The hit-area runtime bundled alone, a phone-sized touch page with one
* watched row button, and a neighbour that appears after the row is measured
* @output Proves that a finger's tap on the row's grown hit area runs the row
* when the nearer neighbour is one that no tap can reach, and that a
* neighbour that takes taps still swallows it
* @position Browser regression for hitArea.ts, run by test:interaction; the
* pure model is hitArea.test.ts
*/

import {fileURLToPath} from 'node:url';
import {build} from 'esbuild';
import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
import type * as hitArea from '../hitArea';

declare global {
interface Window {
hitAreaProof: typeof hitArea;
rowClicks: number;
}
}

let bundle = '';

test.beforeAll(async () => {
const result = await build({
entryPoints: [fileURLToPath(new URL('../hitArea.ts', import.meta.url))],
bundle: true,
write: false,
format: 'iife',
globalName: 'hitAreaProof',
platform: 'browser',
target: 'es2022',
logLevel: 'silent',
});
bundle = result.outputFiles[0].text;
});

// The row is 20 px tall, so its hit area grows 12 px below it. The press is
// 3 px below the row, in that growth, and the neighbour starts 2 px below the
// press: it is nearer the press than the row's own box.
const PRESS = {x: 190, y: 123};

/**
* Opens a page with the row measured and nothing near it, then places
* `neighbour` below the press, outside the row's parent, so that nothing
* measures the row again before the touch.
*/
async function openRowWithNeighbour(
page: Page,
neighbour: string,
): Promise<void> {
await page.goto('about:blank');
await page.setContent(`<!doctype html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { margin: 0; }
#row { position: absolute; left: 40px; top: 100px; width: 200px;
height: 20px; margin: 0; padding: 0; box-sizing: border-box; }
#row::after { content: ''; position: absolute;
inset: calc(var(--astryx-hit-inset-top, 0px) * -1)
calc(var(--astryx-hit-inset-right, 0px) * -1)
calc(var(--astryx-hit-inset-bottom, 0px) * -1)
calc(var(--astryx-hit-inset-left, 0px) * -1); }
.spot { position: absolute; left: 176px; top: 125px; }
.spot button { display: block; width: 28px; height: 28px; margin: 0;
padding: 0; box-sizing: border-box; }
</style>
<div><button id="row">Open</button></div>
<div id="elsewhere"></div>`);
await page.addScriptTag({content: bundle});
await page.evaluate(() => {
const row = document.getElementById('row') as HTMLElement;
const {watchHitArea, writeHitInsets} = window.hitAreaProof;
watchHitArea(row, insets => writeHitInsets(row, insets));
// A swallowed click stops before it reaches the document, as it stops
// before an app's delegated click handler.
window.rowClicks = 0;
document.addEventListener('click', event => {
if ((event.target as Element).closest('#row') != null) {
window.rowClicks++;
}
});
});
await expect(page.locator('#row')).toHaveCSS(
'--astryx-hit-inset-bottom',
'12px',
);
await page.evaluate(html => {
document.getElementById('elsewhere')?.insertAdjacentHTML('beforeend', html);
}, neighbour);
// The browser itself takes the touch to the row, through its hit area.
expect(
await page.evaluate(({x, y}) => document.elementFromPoint(x, y)?.id, PRESS),
).toBe('row');
}

const UNREACHABLE = [
{kind: 'in an inert subtree', html: '<div class="spot" inert>'},
{
kind: 'in an inert subtree whose contents are skipped',
html: '<div class="spot" inert style="content-visibility: hidden">',
},
{
kind: 'in a hidden subtree',
html: '<div class="spot" style="visibility: hidden">',
},
{
kind: 'that takes no pointer events',
html: '<div class="spot" style="pointer-events: none">',
},
];

for (const {kind, html} of UNREACHABLE) {
test(`a tap on the row's hit area runs the row when the nearer neighbour is ${kind}`, async ({
page,
}) => {
await openRowWithNeighbour(
page,
`${html}<button id="pager">Next</button></div>`,
);
await page.touchscreen.tap(PRESS.x, PRESS.y);
await expect
.poll(async () => page.evaluate(() => window.rowClicks))
.toBe(1);
});
}

test("a tap on the row's hit area runs the row when the nearer neighbour is disabled", async ({
page,
}) => {
await openRowWithNeighbour(
page,
'<div class="spot"><button id="pager" disabled>Next</button></div>',
);
await page.touchscreen.tap(PRESS.x, PRESS.y);
await expect.poll(async () => page.evaluate(() => window.rowClicks)).toBe(1);
});

test("a tap on the row's hit area is still swallowed when the nearer neighbour takes taps", async ({
page,
}) => {
await openRowWithNeighbour(
page,
'<div class="spot"><button id="pager">Next</button></div>',
);
await page.touchscreen.tap(PRESS.x, PRESS.y);
// Then a tap on the row's own box, which runs: by its click, the first
// tap's click has arrived, and it did not run the row.
await page.touchscreen.tap(PRESS.x, 110);
await expect.poll(async () => page.evaluate(() => window.rowClicks)).toBe(1);
});
101 changes: 101 additions & 0 deletions packages/core/src/utils/hitArea.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,107 @@ describe('watchHitArea', () => {
stop();
});

describe('a neighbour that no tap can reach', () => {
/**
* Measures the switch with the link 8 px to its right, then sends a touch
* 6 px past the switch and 2 px from the link, and its click. Whether the
* click was swallowed.
*/
function tapBesideLink(): boolean {
const control = byId('switch');
place(control, rect(100, 100, 24, 24));
place(byId('link'), rect(132, 100, 60, 24));
const stop = watchHitArea(control, () => {});
show(control);
flush();
const press = new Event('pointerdown', {bubbles: true});
Object.assign(press, {pointerType: 'touch', clientX: 130, clientY: 112});
control.dispatchEvent(press);
const click = new MouseEvent('click', {
bubbles: true,
cancelable: true,
detail: 1,
});
control.dispatchEvent(click);
stop();
return click.defaultPrevented;
}

/** Moves the link into a new `<div>` that has `attributes`. */
function wrapLink(attributes: Record<string, string>): void {
const link = byId('link');
const wrapper = document.createElement('div');
for (const [name, value] of Object.entries(attributes)) {
wrapper.setAttribute(name, value);
}
link.replaceWith(wrapper);
wrapper.append(link);
}

it('still swallows a touch nearer a neighbour that takes taps', () => {
expect(tapBesideLink()).toBe(true);
});

it('lets a touch through beside a neighbour in an inert subtree', () => {
wrapLink({inert: ''});
expect(tapBesideLink()).toBe(false);
});

it('lets a touch through beside a disabled neighbour', () => {
byId('link').outerHTML = '<button id="link" disabled>Details</button>';
expect(tapBesideLink()).toBe(false);
});

it('lets a touch through beside a neighbour that takes no pointer events', () => {
wrapLink({style: 'pointer-events: none'});
expect(tapBesideLink()).toBe(false);
});

it('lets a touch through beside a neighbour that checkVisibility calls hidden', () => {
const checkVisibility = vi.fn(() => false);
Object.defineProperty(byId('link'), 'checkVisibility', {
configurable: true,
value: checkVisibility,
});
expect(tapBesideLink()).toBe(false);
expect(checkVisibility).toHaveBeenCalledWith({checkVisibilityCSS: true});
});

describe('where checkVisibility is missing, as in jsdom', () => {
it('lets a touch through beside a neighbour in a hidden subtree', () => {
expect(typeof byId('link').checkVisibility).toBe('undefined');
wrapLink({style: 'visibility: hidden'});
expect(tapBesideLink()).toBe(false);
});

it('lets a touch through beside a neighbour whose ancestor skips its contents', () => {
wrapLink({style: 'content-visibility: hidden'});
expect(tapBesideLink()).toBe(false);
});
});

it('still swallows a touch nearer a neighbour drawn at opacity 0, which still takes taps', () => {
wrapLink({style: 'opacity: 0'});
expect(tapBesideLink()).toBe(true);
});

it('still stops the hit area halfway to a neighbour that no tap can reach, reading no style in the pass', () => {
wrapLink({inert: ''});
const control = byId('switch');
layOut(control, 24, 24);
place(control, rect(100, 100, 24, 24));
place(byId('link'), rect(132, 100, 60, 24));
const getComputedStyle = vi.spyOn(window, 'getComputedStyle');
const heard: HitInsets[] = [];
const stop = watchHitArea(control, insets => heard.push(insets));
show(control);
flush();
expect(heard.at(-1)).toEqual({top: 10, right: 4, bottom: 10, left: 10});
expect(getComputedStyle).not.toHaveBeenCalled();
stop();
});
});

describe('when an animation moves boxes between the touch and its click', () => {
/** One running animation of `target`, from keyframes jsdom cannot run. */
const running = (target: Element, frames: Keyframe[]) => ({
Expand Down
64 changes: 52 additions & 12 deletions packages/core/src/utils/hitArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,11 +715,52 @@ function readCandidates(doc: Document): Candidate[] {
}));
}

/**
* Whether a tap can reach `element`. A touch nearer a neighbour that no tap
* reaches is still the control's. Only the press asks: the measuring pass
* reads no style, so such a neighbour still stops a hit area halfway, which
* takes no tap from anyone. Opacity does not count: a control drawn at
Comment on lines +720 to +722

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (comment style): this sentence runs 26 words over three topics. Suggested: "Only the touch check asks. The measuring pass reads no style, so such a neighbour still stops a hit area halfway. That strip takes no tap from anyone."

* opacity 0, such as a native input over its own drawn box, takes the tap.
*/
function takesTaps(element: Element): boolean {
if (element.matches(':disabled') || element.closest('[inert]') != null) {
return false;
}
const view = element.ownerDocument.defaultView;
if (view == null) {
return true;
}
const style = view.getComputedStyle(element);
if (style.pointerEvents === 'none') {
return false;
}
if (typeof element.checkVisibility === 'function') {
return element.checkVisibility({checkVisibilityCSS: true});
}
// Without `checkVisibility`, its two checks are read from the styles.
if (style.visibility === 'hidden' || style.visibility === 'collapse') {
return false;
}
for (
let node = element.parentElement;
node != null;
node = node.parentElement
) {
if (
view.getComputedStyle(node).getPropertyValue('content-visibility') ===
'hidden'
) {
return false;
}
}
return true;
}

/** A control's rect and clamped insets, from candidates already read. */
function measureOne(
watch: Watch,
candidates: ReadonlyArray<Candidate>,
): {target: HitRect; insets: HitInsets; neighbours: HitRect[]} {
): {target: HitRect; insets: HitInsets; neighbours: Candidate[]} {
const target = plainRect(watch.element);
const scale = screenScale(watch.element, target, HIT_AREA_TOLERANCE);
// Two targets each growing up to half the minimum reach each other from a
Expand All @@ -731,15 +772,13 @@ function measureOne(
bottom: reach * scale.y,
left: reach * scale.x,
});
const neighbours = candidates
.filter(
({element, rect}) =>
rect.right > rect.left &&
rect.bottom > rect.top &&
intersects(band, rect) &&
!isOwn(watch, element),
)
.map(({rect}) => rect);
const neighbours = candidates.filter(
({element, rect}) =>
rect.right > rect.left &&
rect.bottom > rect.top &&
intersects(band, rect) &&
!isOwn(watch, element),
);
return {
target,
// The insets are in the control's layout px. The returned rects stay in
Expand All @@ -749,7 +788,7 @@ function measureOne(
// still counts that neighbour.
insets: clampedHitInsets(
inLayout(target, target, scale),
neighbours.map(rect => inLayout(rect, target, scale)),
neighbours.map(({rect}) => inLayout(rect, target, scale)),
watch.minSize,
watch.growth?.(inLayout(target, target, scale), rect =>
inLayout(rect, target, scale),
Expand Down Expand Up @@ -1107,7 +1146,8 @@ function isNearerNeighbour(
press.y,
);
isNearer ||= neighbours.some(
rect => distanceTo(rect, press.x, press.y) < own,
({element: neighbour, rect}) =>
distanceTo(rect, press.x, press.y) < own && takesTaps(neighbour),
);
// The press still scales the control, so these insets read a scaled box.
// A pass after the press's motion ends takes the insets instead.
Expand Down