Repository navigation
fix(core): a tap is not swallowed beside a control no tap can reach #13009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
vjeux
wants to merge
1
commit into
meta:main
Choose a base branch
from
vjeux:fix/hit-area-unreachable-neighbour
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+315
−12
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
| @vjeux | ||
155 changes: 155 additions & 0 deletions
155
packages/core/src/utils/__tests__/hitAreaUnreachableNeighbour.interaction.browser.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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 | ||
|
|
@@ -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), | ||
|
|
@@ -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. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
takesTapshandles 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 aninertsubtree, hidden byvisibilityor skipped content, or haspointer-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.