Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Layer): a still mouse presses through an entrance's input hold (#10303)

Christopher Chedeau3bd5a4c1b429
diff --git a/.changeset/still-mouse-passes-entrance-hold.md b/.changeset/still-mouse-passes-entrance-hold.md
new file mode 100644
index 0000000000..5eb440236a
--- /dev/null
+++ b/.changeset/still-mouse-passes-entrance-hold.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] On a touch screen with a mouse, after a finger opens a menu, a dialog, a drawer, a mobile nav or a popover, a click with a mouse that has not moved since reaches what is under it while the layer enters. Before, that click landed on the entrance's input hold and did nothing. A finger's or a pen's tap during the entrance is still ignored. A mouse that has sent no pointer event since the page loaded still meets the hold.
+
+@vjeux
diff --git a/docs/specs/AST-094-touch-presentations/spec.md b/docs/specs/AST-094-touch-presentations/spec.md
index f47a320fda..378453223d 100644
--- a/docs/specs/AST-094-touch-presentations/spec.md
+++ b/docs/specs/AST-094-touch-presentations/spec.md
@@ -942,10 +942,11 @@ under a finger, and a mouse follows `module:DropdownMenu/useMenuPress` FR3 and
MobileNav and DropdownMenu hold an entrance only under a coarse primary
pointer: where the primary pointer is fine and the screen also takes touch
(a touchscreen laptop), a finger's tap or drag during the entrance is not
- held. After a
- finger, the first press of a mouse that has not moved since lands on the
- entering presentation's shield and does nothing, where FR35 keeps today's
- behavior for a mouse (DEC-16); a move of the mouse lowers the shield first.
+ held. After a finger, the first press of a mouse that has not moved since
+ passes the entering presentation's shield where the page last heard the
+ mouse. A mouse that has sent no pointer event since the page loaded still
+ lands on the shield, and its press does nothing. A move of the mouse lowers
+ the shield first.
- BottomSheet, Toast, Lightbox, NavigationStack and keyboard dismissal: a
drag that commits follows FR36–FR40.
- `spec:AST-100` owns release velocity, which FR17 reads.
diff --git a/docs/specs/components/DropdownMenu/DropdownMenu.spec.md b/docs/specs/components/DropdownMenu/DropdownMenu.spec.md
index 866251e6dc..041720944b 100644
--- a/docs/specs/components/DropdownMenu/DropdownMenu.spec.md
+++ b/docs/specs/components/DropdownMenu/DropdownMenu.spec.md
@@ -143,7 +143,7 @@ component-local concepts; each keeps its released default.
| FR17 | A disabled menu row MUST be reached by the keyboard and stay inert (DEC-11): in DropdownMenu, its sub-menus, MoreMenu, ContextMenu and NavHeadingMenu, ArrowDown, ArrowUp, Home, End and type-ahead visit a disabled row as they visit any other row, the row takes focus and exposes `aria-disabled="true"`, and Enter, Space or a click on it does nothing and leaves the menu open. Opening a menu from the keyboard still lands on an enabled row: the first, or the last after ArrowUp on the trigger (FR12, AR4). Implementation gap: the arrow keys and type-ahead skip disabled rows today. | DEC-11; WAI-ARIA Menu pattern |
| FR20 | With `header` or `footer` on a sub-menu (`DropdownMenuSubMenu`), its surface is a modal dialog named by its row (AR7), holding the header, the sub-menu's `role="menu"` (rows only), then the footer. It sits in a host `DropdownMenu` renders beside its menu, never inside a menu; under a root with no such host (`ContextMenu`) the sub-menu shows its rows alone. On a fine pointer the dialog is the flyout; on a compact touch display, the drilled-in view, which replaces the root's menu and regions until popped. A region keeps its own keys and presses. The rows are one Tab stop: Tab and Shift+Tab walk the header's controls, the rows, then the footer's, and wrap; from a deeper flyout, that flyout closes and the walk goes on from the rows. An open lands as for any sub-menu (FR11, FR12), not on a region. Escape in the dialog, outside an IME composition and unless a control there handled it, returns focus to the row and leaves the root open. A pointer leaving the flyout keeps it open until focus leaves, and it closes with the flyout holding its row. Without either prop nothing changes. | Current behavior; FR15; AR5 |
| FR21 | Under a finger, in the touch modes (`presentation` not pinned to `'popover'`), the pointer menu MUST grow out of the Trigger button on the theme's entrance duration and easing tokens and collapse back into it on the theme's exit duration and easing (`spec:AST-094` FR7, FR31, FR45), and MUST close as a pan that starts outside it begins (FR32); a pinned `'popover'` keeps the layer's own entrance. A pan dismissal is the menu's own close: focus that was in the menu returns to the Trigger button, whose `aria-expanded` turns false. A mouse keeps the layer's own entrance and exit. Implementation gap: the menu keeps the layer's own entrance and exit under a finger too, and has no pan-outside dismissal of its own. | `spec:AST-094` FR31, FR32; `family:overlay-dismissal` AV6 |
-| FR22 | Under a finger, in the touch modes, where the primary pointer is coarse (a phone or a tablet), a tap or a drag that lands while the pointer menu enters MUST reach nothing: it closes nothing, scrolls nothing, moves no focus and clicks nothing (`spec:AST-094` FR35). The finger that opened the menu still picks the row it lifts on (FR8). Keys and assistive technology stay live. A mouse, a pinned `'popover'` and a finger where the primary pointer is fine (a touchscreen laptop) hold nothing, as for `component:Drawer`. Implementation gap: after a finger, a still mouse's first press lands on the entering menu's shield and does nothing (`spec:AST-094` FR35). | `spec:AST-094` FR35; `architecture:layer-runtime` |
+| FR22 | Under a finger, in the touch modes, where the primary pointer is coarse (a phone or a tablet), a tap or a drag that lands while the pointer menu enters MUST reach nothing: it closes nothing, scrolls nothing, moves no focus and clicks nothing (`spec:AST-094` FR35). The finger that opened the menu still picks the row it lifts on (FR8). Keys and assistive technology stay live. A mouse, a pinned `'popover'` and a finger where the primary pointer is fine (a touchscreen laptop) hold nothing, as for `component:Drawer`. Implementation gap: after a finger, the first press of a mouse that has sent no pointer event since the page loaded lands on the entering menu's shield (`spec:AST-094` FR35). | `spec:AST-094` FR35; `architecture:layer-runtime` |
| FR23 | Tab or Shift+Tab on a row at any level of a menu without `header` or `footer`, its own rows or an open sub-menu flyout's at any depth, closes every open level and the menu, calls `onOpenChange(false)` once, and leaves focus to move on from the trigger in page order (WAI-ARIA Menu Button). A level with a header or footer keeps FR15's and FR20's Tab cycle within it. | WAI-ARIA Menu Button pattern; current tests |
### Allowed variation
@@ -376,9 +376,9 @@ than adding a DropdownMenu-owned heading target.
leaves with the layer's own entrance and exit instead of growing out of and
back into the Trigger button, and a pan that starts outside does not close
it, against `spec:AST-094` FR31 and FR32.
-- **Still mouse after a finger (FR22).** Known defect: after a finger, a still
- mouse's first press lands on the entering menu's shield and does nothing,
- against FR22's touch-only scope.
+- **Still mouse after a finger (FR22).** Known gap: after a finger, a mouse
+ that has sent no pointer event since the page loaded still presses the
+ entering menu's shield, so its first press does nothing.
- **Delegated Trigger Button gaps (FR3).** The Trigger button scales on a press
under reduced motion and holds a fixed height, as `component:Button` records,
against `design:reduced-motion` DR1 and `spec:AST-102` FR4.
diff --git a/packages/core/src/Dialog/__tests__/DialogTouchEntrance.interaction.browser.spec.ts b/packages/core/src/Dialog/__tests__/DialogTouchEntrance.interaction.browser.spec.ts
index 553dad7d13..be1fb09e87 100644
--- a/packages/core/src/Dialog/__tests__/DialogTouchEntrance.interaction.browser.spec.ts
+++ b/packages/core/src/Dialog/__tests__/DialogTouchEntrance.interaction.browser.spec.ts
@@ -6,15 +6,15 @@
* the AlertDialog story "Delete", real touch input through the interaction
* harness in its phone context, and a mouse there and on a fine-pointer
* page
- * @output Browser evidence for spec:AST-094 FR35 on Dialog: under a finger, a
- * tap or a drag that lands while the dialog is still entering closes
- * nothing, runs no action, moves no focus and scrolls nothing, and the
- * dialog finishes entering and stays open; keys stay live and the content
- * stays in the accessibility tree meanwhile; an alert's entrance holds its
- * action alike. With a mouse, a click during the entrance acts as before:
- * on a touch screen the hold decides by each event's pointer, and a press
- * where the mouse already rests acts once the page last heard a mouse;
- * under a fine primary pointer nothing is held.
+ * @output Browser evidence for spec:AST-094 FR35 on Dialog. Under a finger,
+ * a tap or a drag that lands while the dialog is still entering closes
+ * nothing and runs no action. It moves no focus and scrolls nothing, and
+ * the dialog finishes entering and stays open. Keys stay live and the
+ * content stays in the accessibility tree meanwhile. An alert's entrance
+ * holds its action alike. With a mouse, a click during the entrance acts as
+ * before. On a touch screen the hold decides by each event's pointer. A
+ * press where the mouse already rests acts, after a finger opened the
+ * dialog as after a mouse. Under a fine primary pointer nothing is held.
* @position Browser proof for Dialog's entrance hold (Dialog.tsx, through the
* Layer module's holdEntranceInput); the hold itself is proven in
* Layer/__tests__/inputHold.interaction.browser.spec.ts
@@ -406,6 +406,55 @@ test.describe('with a mouse on a touch screen', () => {
await expect.poll(clicks).toEqual(['Open Info Modal', 'Got it']);
await expect(openDialogs(page)).toHaveCount(0);
});
+
+ test('after a finger opens the dialog, a press where the mouse already rests reaches its action while the dialog enters (spec:AST-094 FR35)', async ({
+ page,
+ }) => {
+ // Under Reduce Motion the dialog only fades in, so its action stays where
+ // the mouse rests.
+ await page.emulateMedia({reducedMotion: 'reduce'});
+ await openStory(page, storybook(), INFO);
+ const opener = page.getByRole('button', {name: 'Open Info Modal'});
+ // A first presentation, which a mouse opens and a key closes, finds where
+ // the action appears.
+ await opener.click();
+ await expect(openDialogs(page)).toHaveCount(1);
+ await finishEntrance(page);
+ const action = await centerOf(page.getByRole('button', {name: 'Got it'}));
+ await page.keyboard.press('Escape');
+ await expect(openDialogs(page)).toHaveCount(0);
+ // The mouse comes to rest where the action appears; a finger opens the
+ // dialog again, so the page last heard a finger.
+ await page.mouse.move(action.x - 40, action.y);
+ await page.mouse.move(action.x, action.y, {steps: 4});
+ await page.evaluate(() => {
+ const clicks: string[] = [];
+ (window as unknown as {__clicks: string[]}).__clicks = clicks;
+ window.addEventListener(
+ 'click',
+ event => {
+ const target = event.target as Element;
+ clicks.push(target.closest('button')?.textContent?.trim() ?? '');
+ },
+ {capture: true},
+ );
+ });
+ await slowNextEntrance(page);
+ const driver = await touchDriver(page);
+ await driver.tap(await centerOf(opener));
+ await holdEntrance(page);
+
+ const clicks = async () =>
+ page.evaluate(() => (window as unknown as {__clicks: string[]}).__clicks);
+ await expect(openDialogs(page)).toHaveCount(1);
+ await expect(page.getByRole('button', {name: 'Got it'})).toBeVisible();
+ // The mouse has not moved since the finger: its press reaches the action
+ // (after the finger's own click on the opener).
+ await page.mouse.down();
+ await page.mouse.up();
+ await expect.poll(clicks).toEqual(['Open Info Modal', 'Got it']);
+ await expect(openDialogs(page)).toHaveCount(0);
+ });
});
test.describe('with a mouse', () => {
diff --git a/packages/core/src/DropdownMenu/__tests__/DropdownMenuEntranceHold.interaction.browser.spec.ts b/packages/core/src/DropdownMenu/__tests__/DropdownMenuEntranceHold.interaction.browser.spec.ts
index ce88142a97..6ab8db5a4e 100644
--- a/packages/core/src/DropdownMenu/__tests__/DropdownMenuEntranceHold.interaction.browser.spec.ts
+++ b/packages/core/src/DropdownMenu/__tests__/DropdownMenuEntranceHold.interaction.browser.spec.ts
@@ -9,7 +9,8 @@
* @output Proof that a menu that enters under a finger takes no new input. A
* tap that lands then neither closes it nor reaches the page nor moves
* focus. Once the entrance ends, a tap outside dismisses it. The finger
- * that opened the menu still picks the row it lifts on. A mouse is not held.
+ * that opened the menu still picks the row it lifts on. A mouse is not
+ * held: after a finger opens the menu, a mouse resting on a row picks it.
* @position Evidence for DropdownMenu's entrance hold; the layer runtime's
* hold has its own spec in Layer/__tests__
*/
@@ -219,6 +220,93 @@ test('the finger that opened the menu still picks the row it lifts on while the
expect(await readPicked(page)).toEqual(['Duplicate']);
});
+test("after a finger opens the menu, a mouse resting on a row picks it while the menu enters: a mouse's press is not held (spec:AST-094 FR35)", async ({
+ page,
+}) => {
+ // Under Reduce Motion the menu only fades in, so its rows stay where the
+ // mouse rests.
+ await page.emulateMedia({reducedMotion: 'reduce'});
+ await openStory(page, storybook(), 'core-dropdownmenu--default');
+ await installPickLog(page);
+ const driver = await touchDriver(page);
+ const trigger = page.getByRole('button', {name: 'Actions'});
+ const box = await boxOf(trigger);
+ const opener = {
+ x: Math.round(box.x + box.width / 2),
+ y: Math.round(box.y + box.height / 2),
+ };
+ // Measure the open menu's rows once its entrance has come to rest.
+ await driver.tap(opener);
+ await expect(trigger).toHaveAttribute('aria-expanded', 'true');
+ const layer = menuLayer(page);
+ await waitForRest(layer);
+ const duplicate = await boxOf(
+ page.getByRole('menuitem', {name: 'Duplicate'}),
+ );
+ await page.keyboard.press('Escape');
+ await expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ await waitForRest(layer);
+ await lengthenEntrance(page);
+ // A mouse on the same touch screen comes to rest over the row. Then a
+ // finger opens the menu.
+ const row = {
+ x: Math.round(duplicate.x + duplicate.width / 2),
+ y: Math.round(duplicate.y + duplicate.height / 2),
+ };
+ await page.mouse.move(row.x - 30, row.y);
+ await page.mouse.move(row.x, row.y, {steps: 4});
+ await driver.tap(opener);
+ await expect(trigger).toHaveAttribute('aria-expanded', 'true');
+ expect(await runningEntrances(layer)).toBeGreaterThan(0);
+ // The mouse has not moved since the finger: its click picks the row.
+ await page.mouse.down();
+ await page.mouse.up();
+ await expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(await readPicked(page)).toEqual(['Duplicate']);
+});
+
+test('after a finger opens the menu, a mouse that moves onto another row while the menu enters picks that row: a move lowers the shield (spec:AST-094 FR35)', async ({
+ page,
+}) => {
+ await page.emulateMedia({reducedMotion: 'reduce'});
+ await openStory(page, storybook(), 'core-dropdownmenu--default');
+ await installPickLog(page);
+ const driver = await touchDriver(page);
+ const trigger = page.getByRole('button', {name: 'Actions'});
+ const box = await boxOf(trigger);
+ const opener = {
+ x: Math.round(box.x + box.width / 2),
+ y: Math.round(box.y + box.height / 2),
+ };
+ await driver.tap(opener);
+ await expect(trigger).toHaveAttribute('aria-expanded', 'true');
+ const layer = menuLayer(page);
+ await waitForRest(layer);
+ const edit = await boxOf(page.getByRole('menuitem', {name: 'Edit'}));
+ const duplicate = await boxOf(
+ page.getByRole('menuitem', {name: 'Duplicate'}),
+ );
+ await page.keyboard.press('Escape');
+ await expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ await waitForRest(layer);
+ await lengthenEntrance(page);
+ // The mouse rests over the first row; a finger opens the menu.
+ const x = Math.round(edit.x + edit.width / 2);
+ await page.mouse.move(x - 30, Math.round(edit.y + edit.height / 2));
+ await page.mouse.move(x, Math.round(edit.y + edit.height / 2), {steps: 4});
+ await driver.tap(opener);
+ await expect(trigger).toHaveAttribute('aria-expanded', 'true');
+ expect(await runningEntrances(layer)).toBeGreaterThan(0);
+ // While the menu enters, the mouse moves onto the second row and clicks.
+ await page.mouse.move(x, Math.round(duplicate.y + duplicate.height / 2), {
+ steps: 4,
+ });
+ await page.mouse.down();
+ await page.mouse.up();
+ await expect(trigger).toHaveAttribute('aria-expanded', 'false');
+ expect(await readPicked(page)).toEqual(['Duplicate']);
+});
+
test.describe('under a desktop mouse', () => {
test.use({
isMobile: false,
diff --git a/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.ts b/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.ts
index cb39c9c83f..6f19de05c1 100644
--- a/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.ts
+++ b/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.ts
@@ -4,23 +4,23 @@
* @file inputHold.interaction.browser.spec.ts
* @input The input-hold fixture bundled with esbuild, a phone-sized
* touch screen, and real engine touch input (touchDriver)
- * @output Browser proof of what the unit tests cannot see: the hold's shield
- * is the hit target over the whole viewport, above a modal dialog in the
- * top layer; a tap during a hold reaches no click and no recognizer on the
- * layer (the gesture host's touch gate) and leaves a light-dismissing
- * popover open; a mouse the page heard last passes a presentation's hold,
- * and after a finger a mouse press meets its shield to the press's end; a
- * layer root that takes no hits itself (pointer-events:
- * none, or visibility: hidden, its content opting back in) is held all the
- * same; keys still act during a presentation's input hold; a dismissal's
- * hold makes the content inert and moves focus out of it, so no key acts
- * on it either (a focused button that turns inert still takes Enter and
- * Space in both engines); each release gives input back; a hit test
- * that skips shields (isInputShield) finds what lies beneath, and
- * a layer above the held one still wins; a one-finger
- * pan on the shield scrolls nothing while a pinch on it still zooms the
- * page (spec:AST-094 FR10, FR18, FR35; Chromium, which pinch-zooms from
- * injected touches)
+ * @output Browser proof of what the unit tests cannot see. The hold's
+ * shield is the hit target over the whole viewport, above a modal dialog
+ * in the top layer. A tap during a hold reaches no click and no recognizer
+ * on the layer (the gesture host's touch gate). It leaves a
+ * light-dismissing popover open. A mouse the page heard last passes a
+ * presentation's hold. After a finger, a still mouse's press passes it
+ * where the mouse rests, while a finger's tap there is held. A layer root
+ * that takes no hits itself is held all the same: pointer-events: none, or
+ * visibility: hidden, with its content opting back in. Keys still act
+ * during a presentation's input hold. A dismissal's hold makes the content
+ * inert and moves focus out of it, so no key acts on it either. A focused
+ * button that turns inert still takes Enter and Space in both engines.
+ * Each release gives input back. A hit test that skips shields
+ * (isInputShield) finds what lies beneath, and a layer above the held one
+ * still wins. A one-finger pan on the shield scrolls nothing, while a pinch
+ * on it still zooms the page. It proves spec:AST-094 FR10, FR18 and FR35.
+ * The pinch case runs in Chromium, which pinch-zooms from injected touches.
* @position Browser tests of packages/core/src/Layer/inputHold.ts, run
* by the interaction project (`pnpm test:interaction inputHold`)
*/
@@ -280,32 +280,35 @@ test("a mouse the page heard last passes a presentation's hold at once: a press
expect(await landings()).toEqual(['pointerup:action', 'click:action']);
});
-test("after a finger, a mouse press made before the mouse moves meets a presentation's shield to its end: its release and click land on the shield and reach nothing, and once the mouse moves its press reaches the dialog's recognizer (spec:AST-094 FR35)", async ({
+test("after a finger, a still mouse's press passes a presentation's shield where the mouse rests: the press, its release and its click reach the dialog's recognizer, while a finger's tap on that spot is still held (spec:AST-094 FR35)", async ({
page,
}) => {
const driver = await load(page);
await open(page, 'dialog');
const action = await centre(page, 'action');
const message = await centre(page, 'message');
+ // Recorded from before the first tap: a recognized tap's click guard then
+ // listens after it, and cannot hide the click from it.
+ const landings = await recordLandings(page);
await page.mouse.move(action.x - 40, action.y);
await page.mouse.move(action.x, action.y, {steps: 4});
// A finger is the page's last pointer: it taps the dialog's text, which
// the recognizer on the dialog counts.
await driver.tap(message);
await expect.poll(async () => (await readLog(page)).taps).toBe(1);
- const landings = await recordLandings(page);
await hold(page, 'dialog', 'viewport');
- await page.mouse.down();
- await page.mouse.up();
+ // A finger's tap where the mouse rests is held all the same.
+ await driver.tap(action);
await settle(page);
- expect(await landings()).toEqual(['pointerup:shield', 'click:shield']);
expect(await readLog(page)).toEqual({clicks: [], taps: 1, toggles: []});
- // The mouse moves: its next press reaches the dialog's recognizer, as with
- // no hold.
- await page.mouse.move(action.x + 4, action.y, {steps: 4});
+ // The mouse has not moved since the finger: its press lands where it rests.
+ await page.evaluate(() => {
+ (window as unknown as {__seen: string[]}).__seen.splice(0);
+ });
await page.mouse.down();
await page.mouse.up();
await expect.poll(async () => (await readLog(page)).taps).toBe(2);
+ await expect.poll(landings).toEqual(['pointerup:action', 'click:action']);
});
test('a touch that lands during a hold and lifts after its release reaches nothing to its end: no release, end or click lands anywhere, and the next tap clicks (spec:AST-094 FR35)', async ({
diff --git a/packages/core/src/Layer/holdEntranceInput.test.ts b/packages/core/src/Layer/holdEntranceInput.test.ts
index 036631b33f..a75edc35df 100644
--- a/packages/core/src/Layer/holdEntranceInput.test.ts
+++ b/packages/core/src/Layer/holdEntranceInput.test.ts
@@ -3,6 +3,7 @@
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import {stubMatchMedia} from '../__tests__/stubMatchMedia';
import {holdEntranceInput} from './holdEntranceInput';
+import {subscribeInput} from '../utils/inputStream';
import {isInHeldLayer, isInputShield} from './inputHold';
function deferred() {
@@ -161,6 +162,42 @@ describe('holdEntranceInput', () => {
]);
});
+ it("after a finger, leaves open the point where the mouse rests: a still mouse's press reaches the layer, a finger's is held (spec:AST-094 FR35)", () => {
+ // Press feedback hears the page's pointer moves through the stream on any
+ // page with a pressable. The mouse came to rest; then a finger opened the
+ // layer.
+ const stop = subscribeInput(window, 'pointermove', () => {});
+ document.body.dispatchEvent(
+ new PointerEvent('pointermove', {
+ ...INIT,
+ pointerType: 'mouse',
+ clientX: 50,
+ clientY: 30,
+ }),
+ );
+ document.body.dispatchEvent(
+ new PointerEvent('pointermove', {...INIT, pointerType: 'touch'}),
+ );
+ stop();
+ const layer = mountLayer([fakeAnimation(new Promise(() => {}))]);
+ const button = layer.firstElementChild as Element;
+
+ hold(layer);
+ const shield = [...layer.children].find(child =>
+ isInputShield(child),
+ ) as HTMLElement;
+ expect(shield.style.pointerEvents).toBe('auto');
+ expect(shield.style.getPropertyValue('clip-path')).toContain(
+ '48px 28px, 48px 32px, 52px 32px, 52px 28px',
+ );
+ expect(tap(button, 'touch', 1)).toEqual([]);
+ expect(tap(button, 'mouse', 2)).toEqual([
+ 'pointerdown',
+ 'pointerup',
+ 'click',
+ ]);
+ });
+
it('holds nothing under a fine primary pointer: a mouse press made before the mouse moves reaches the layer, and no window listener is added (spec:AST-094 FR35, FR24)', () => {
stubMatchMedia({pointer: 'fine'});
const added = vi.spyOn(window, 'addEventListener');
diff --git a/packages/core/src/Layer/inputHold.test.tsx b/packages/core/src/Layer/inputHold.test.tsx
index 14d180b8e2..b6591142a1 100644
--- a/packages/core/src/Layer/inputHold.test.tsx
+++ b/packages/core/src/Layer/inputHold.test.tsx
@@ -514,6 +514,79 @@ describe("holdInput decides by each event's pointer (spec:AST-094 FR35)", () =>
expect(shield.style.pointerEvents).toBe('auto');
});
+ /** A mouse comes to rest at (x, y), heard as press feedback hears it. */
+ function mouseRestsAt(x: number, y: number) {
+ const stop = subscribeInput(window, 'pointermove', () => {});
+ document.body.dispatchEvent(
+ new PointerEvent('pointermove', {
+ ...INIT,
+ pointerType: 'mouse',
+ clientX: x,
+ clientY: y,
+ }),
+ );
+ stop();
+ }
+
+ /** The clip-path that leaves a 4 px square open around (x, y). */
+ const openingAt = (x: number, y: number) =>
+ `polygon(0 0, 100% 0, 100% 100%, 0 100%, 0 ${y - 2}px, ${x - 2}px ${y - 2}px, ${x - 2}px ${y + 2}px, ${x + 2}px ${y + 2}px, ${x + 2}px ${y - 2}px, 0 ${y - 2}px)`;
+
+ it("leaves the point where the mouse rests open on a presentation's shield that a finger raised: a still mouse's press reaches what is under it, a finger's press is still held, and a dismissal's shield stays whole (spec:AST-094 FR35)", () => {
+ const {layer} = mountLayer();
+ const {button, reached} = recorder(layer);
+ mouseRestsAt(120, 80);
+ // A finger opened the layer: it is the stream's last pointer.
+ lastPointerWas('touch');
+ hold(layer);
+ const shield = layer.lastElementChild as HTMLElement;
+ expect(shield.style.pointerEvents).toBe('auto');
+ expect(shield.style.getPropertyValue('clip-path')).toBe(openingAt(120, 80));
+ // A finger's or a pen's press is held wherever it lands, there too.
+ pointer(button, 'pointerdown', 'touch', 2);
+ pointer(button, 'pointerup', 'touch', 2);
+ click(button, 'touch');
+ pointer(button, 'pointerdown', 'pen', 4);
+ pointer(button, 'pointerup', 'pen', 4);
+ click(button, 'pen');
+ expect(reached).toEqual([]);
+ expect(shield.style.getPropertyValue('clip-path')).toBe(openingAt(120, 80));
+ // The still mouse's press, which the browser hit-tests through the opening.
+ pointer(button, 'pointerdown', 'mouse', 1, 1);
+ pointer(button, 'pointerup', 'mouse');
+ click(button, 'mouse');
+ expect(reached).toEqual(['pointerdown', 'pointerup', 'click']);
+ // A moving mouse lowers the shield, and the opening goes with it.
+ mouseRestsAt(130, 90);
+ expect(shield.style.pointerEvents).toBe('none');
+ expect(shield.style.getPropertyValue('clip-path')).toBe('');
+ // A finger raises it again, open where the mouse now rests.
+ pointer(button, 'pointerdown', 'touch', 3);
+ expect(shield.style.pointerEvents).toBe('auto');
+ expect(shield.style.getPropertyValue('clip-path')).toBe(openingAt(130, 90));
+ pointer(button, 'pointerup', 'touch', 3);
+ // A dismissal's shield takes every hit, a mouse's included.
+ hold(layer, 'layer');
+ expect(shield.style.pointerEvents).toBe('auto');
+ expect(shield.style.getPropertyValue('clip-path')).toBe('');
+ });
+
+ it("stops a shield over the layer's box alone from taking hits once the stream heard a mouse, while a finger's press on the layer is still held (spec:AST-094 FR35)", () => {
+ const {layer} = mountLayer();
+ const {button, reached} = recorder(layer);
+ mouseRestsAt(40, 40);
+ lastPointerWas('touch');
+ hold(layer, 'surface');
+ const shield = layer.lastElementChild as HTMLElement;
+ expect(isInputShield(shield)).toBe(true);
+ expect(shield.style.pointerEvents).toBe('none');
+ pointer(button, 'pointerdown', 'touch', 2);
+ pointer(button, 'pointerup', 'touch', 2);
+ click(button, 'touch');
+ expect(reached).toEqual([]);
+ expect(shield.style.pointerEvents).toBe('none');
+ });
+
it("starts a presentation's shield up when the stream last heard a finger, or no pointer at all", () => {
const {layer, child} = mountLayer();
lastPointerWas('touch');
diff --git a/packages/core/src/Layer/inputHold.ts b/packages/core/src/Layer/inputHold.ts
index 25d8ddd9fe..af212c9b76 100644
--- a/packages/core/src/Layer/inputHold.ts
+++ b/packages/core/src/Layer/inputHold.ts
@@ -20,7 +20,11 @@ import {
deepActiveElement,
parentAcrossShadow,
} from '../utils/flatTree';
-import {getLastPointerType, subscribeInput} from '../utils/inputStream';
+import {
+ getLastMousePoint,
+ getLastPointerType,
+ subscribeInput,
+} from '../utils/inputStream';
import {isDocument, isElement, isHTMLElement, isNode} from '../utils/nodeType';
/**
@@ -260,6 +264,59 @@ function addShield(layer: Element, isFull: boolean): HTMLElement {
return shield;
}
+/** Half the side of the opening a presentation's shield leaves the mouse. */
+const MOUSE_OPENING_PX = 2;
+
+/**
+ * Leaves the mouse a way through a presentation's shield that takes hits.
+ * The browser hit-tests a press before any script reads its pointer. So a
+ * mouse that has not moved since a finger raised the shield would press the
+ * shield. A full shield leaves open the point where the stream last heard
+ * the mouse. A shield over the layer's box alone stops taking hits, because
+ * the box moves with the layer's entrance. A finger's press there is still
+ * held, because the press hold judges each press by its own pointer. A
+ * dismissal's shield keeps every hit.
+ */
+function openToMouse(state: HeldLayer, doc: Document): void {
+ const {shield} = state;
+ if (shield == null) {
+ return;
+ }
+ const view = doc.defaultView;
+ const point = view == null ? null : getLastMousePoint(view);
+ if (
+ point == null ||
+ state.layer > 0 ||
+ shield.style.pointerEvents === 'none'
+ ) {
+ shield.style.removeProperty('clip-path');
+ return;
+ }
+ if (!state.isShieldFull) {
+ shield.style.pointerEvents = 'none';
+ return;
+ }
+ const left = point.x - MOUSE_OPENING_PX;
+ const right = point.x + MOUSE_OPENING_PX;
+ const top = point.y - MOUSE_OPENING_PX;
+ const bottom = point.y + MOUSE_OPENING_PX;
+ // The viewport, then the opening drawn the other way round. The nonzero
+ // rule leaves the opening out, and a clipped-out area takes no hits.
+ const outline = [
+ '0 0',
+ '100% 0',
+ '100% 100%',
+ '0 100%',
+ `0 ${top}px`,
+ `${left}px ${top}px`,
+ `${left}px ${bottom}px`,
+ `${right}px ${bottom}px`,
+ `${right}px ${top}px`,
+ `0 ${top}px`,
+ ];
+ shield.style.clipPath = `polygon(${outline.join(', ')})`;
+}
+
/**
* Holds input away from `layer` until `until` settles. Returns the release
* (idempotent), for a hold that ends early. Holds on one layer stack, and
@@ -312,6 +369,7 @@ export function holdInput(
state.inertContent = makeContentInert(layer, state.shield);
}
}
+ openToMouse(state, layer.ownerDocument);
let isReleased = false;
const release = () => {
if (isReleased) {
@@ -506,6 +564,7 @@ function createPressHold(doc: Document): PressHold | null {
if (state.shield.style.pointerEvents !== value) {
state.shield.style.pointerEvents = value;
}
+ openToMouse(state, doc);
}
};
diff --git a/packages/core/src/utils/inputStream.test.ts b/packages/core/src/utils/inputStream.test.ts
index 7c21056e4d..d1f72062ab 100644
--- a/packages/core/src/utils/inputStream.test.ts
+++ b/packages/core/src/utils/inputStream.test.ts
@@ -21,7 +21,11 @@ import path from 'node:path';
import {afterEach, describe, expect, it, vi} from 'vitest';
import {getGestureHost} from '../gestures/GestureHost';
-import {getLastPointerType, subscribeInput} from './inputStream';
+import {
+ getLastMousePoint,
+ getLastPointerType,
+ subscribeInput,
+} from './inputStream';
import {installPressFeedback} from './pressFeedback';
type Registration = {
@@ -255,6 +259,30 @@ describe('inputStream', () => {
expect(getLastPointerType(view)).toBe('touch');
});
+ it("records where the latest delivered mouse pointer event was, and keeps it through a finger's events", () => {
+ const view = document.defaultView as Window;
+ const stop = subscribeInput(view, 'pointermove', () => {});
+ document.body.dispatchEvent(
+ new PointerEvent('pointermove', {
+ bubbles: true,
+ pointerType: 'mouse',
+ clientX: 40,
+ clientY: 60,
+ }),
+ );
+ expect(getLastMousePoint(view)).toEqual({x: 40, y: 60});
+ document.body.dispatchEvent(
+ new PointerEvent('pointermove', {
+ bubbles: true,
+ pointerType: 'touch',
+ clientX: 200,
+ clientY: 10,
+ }),
+ );
+ expect(getLastMousePoint(view)).toEqual({x: 40, y: 60});
+ stop();
+ });
+
it('keeps the press feedback controller free of the gesture system', () => {
const utils = path.resolve(__dirname);
const seen = new Set<string>();
diff --git a/packages/core/src/utils/inputStream.ts b/packages/core/src/utils/inputStream.ts
index 1fdcc35f8f..8891060767 100644
--- a/packages/core/src/utils/inputStream.ts
+++ b/packages/core/src/utils/inputStream.ts
@@ -4,8 +4,9 @@
* @file inputStream.ts
* @input A window, and subscribers to its input events (the press feedback
* controller, the gesture host)
- * @output subscribeInput, and getLastPointerType: the type of the latest
- * pointer event the stream delivered
+ * @output subscribeInput, getLastPointerType and getLastMousePoint: the
+ * type of the latest pointer event the stream delivered, and where the
+ * latest mouse one was
* @position The one entry for document-wide input listeners in core: one
* native listener per event type on the window's capture phase, shared by
* every subscriber
@@ -68,6 +69,14 @@ export type InputEventMap = WindowEventMap & DocumentEventMap;
const streams = new WeakMap<Window, Map<string, Channel>>();
/** The type of the latest pointer event each window's stream delivered. */
const lastPointerTypes = new WeakMap<Window, string>();
+/** Where the latest mouse pointer event each window's stream delivered was. */
+const lastMousePoints = new WeakMap<Window, MousePoint>();
+
+/** A point in the viewport, in CSS pixels. */
+export interface MousePoint {
+ readonly x: number;
+ readonly y: number;
+}
/**
* The type of the latest pointer event `view`'s stream delivered: `mouse`,
@@ -79,6 +88,15 @@ export function getLastPointerType(view: Window): string | null {
return lastPointerTypes.get(view) ?? null;
}
+/**
+ * Where the latest mouse pointer event `view`'s stream delivered was, in
+ * viewport coordinates, or `null` before any. A mouse that has not moved
+ * since rests there. Reading it adds no listener.
+ */
+export function getLastMousePoint(view: Window): MousePoint | null {
+ return lastMousePoints.get(view) ?? null;
+}
+
/**
* Listen to `type` on `view`'s capture phase through its shared native
* listener. Returns the unsubscribe; calling it again does nothing.
@@ -161,10 +179,17 @@ function register(view: Window, type: string, channel: Channel): void {
function dispatch(view: Window, channel: Channel, event: Event): void {
// Recorded before any subscriber runs, so each reads the pointer at hand.
- const {pointerType} = event as Partial<PointerEvent>;
+ const {pointerType, clientX, clientY} = event as Partial<PointerEvent>;
if (typeof pointerType === 'string' && pointerType !== '') {
lastPointerTypes.set(view, pointerType);
}
+ if (
+ pointerType === 'mouse' &&
+ typeof clientX === 'number' &&
+ typeof clientY === 'number'
+ ) {
+ lastMousePoints.set(view, {x: clientX, y: clientY});
+ }
// A subscriber may subscribe or unsubscribe while the event runs; the
// event goes to those subscribed when it arrived.
for (const subscriber of [...channel.subscribers]) {