An open source design system that's fully customizable and agent ready
TypeScript
fix(Layer): a press that went down before an input hold lifts through the hold's shield (#10398)
Cindy Zhang95572ae809cc
diff --git a/.changeset/input-hold-earlier-press-opening.md b/.changeset/input-hold-earlier-press-opening.mdnew file mode 100644index 0000000000..b0f294b672--- /dev/null+++ b/.changeset/input-hold-earlier-press-opening.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Layers with an entrance input hold: a finger or pen that was already down when a light-dismissing layer opened, and lifts while it enters, now lifts through the hold's shield onto what is under it in WebKit, so the layer dismisses as it already did in Chromium. The shield leaves a small opening at each such press until it lifts. New presses during the entrance are still held.++@cixzhangdiff --git a/docs/specs/AST-094-touch-presentations/spec.md b/docs/specs/AST-094-touch-presentations/spec.mdindex a08daae7ee..37101d4860 100644--- a/docs/specs/AST-094-touch-presentations/spec.md+++ b/docs/specs/AST-094-touch-presentations/spec.md@@ -950,9 +950,11 @@ under a finger, and a mouse follows `module:DropdownMenu/useMenuPress` FR3 andheld. After a finger, the first press of a mouse that has not moved sincepasses the entering presentation's shield where the page last heard themouse. The shield reads that point when it comes up, and again at each- press or move of a finger or a pen. A shield over the viewport opens only- at the point last read, and not at all if the page had heard no mouse by- then. A still mouse resting anywhere else presses the shield, and its press+ press or move of a finger or a pen. A shield over the viewport opens for a+ mouse only at the point last read, and not at all if the page had heard no+ mouse by then. It also opens where each finger or pen that was already down+ when it came up rests, and that opening follows the press until it lifts.+ A still mouse resting anywhere else presses the shield, and its pressdoes nothing. A non-modal Drawer shields only its panel, and that shieldstops a mouse only if the page had heard none at the last read. A move ofthe mouse lowers the shield first.diff --git a/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.ts b/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.tsindex 6f19de05c1..2a58e81747 100644--- a/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.ts+++ b/packages/core/src/Layer/__tests__/inputHold.interaction.browser.spec.ts@@ -447,6 +447,44 @@ test('a light-dismissing popover stays open when a hold takes a tap outside it;.toEqual(['pop:open', 'pop:closed']);});+test('a finger that goes down outside a light-dismissing popover before its hold and lifts during it dismisses the popover, as with no hold, every time', async ({+ page,+}) => {+ const driver = await load(page);+ const viewport = page.viewportSize();+ if (viewport == null) {+ throw new Error('no viewport');+ }+ const outside = {x: 20, y: viewport.height - 20};+ // The lift that lands on the shield is intermittent in WebKit, so the case+ // runs ten times.+ for (let run = 0; run < 10; run += 1) {+ await open(page, 'pop');+ await driver.play(+ [+ {t: 0, fingers: [{id: 7, ...outside}]},+ {t: 600, fingers: [{id: 7, ...outside}]},+ {t: 616, fingers: []},+ ],+ {+ onEvent: async step => {+ if (step.type === 'touchStart') {+ await hold(page, 'pop', 'viewport');+ }+ },+ },+ );+ await expect+ .poll(async () =>+ page.evaluate(() =>+ document.getElementById('pop')?.matches(':popover-open'),+ ),+ )+ .toBe(false);+ await release(page);+ }+});+test('a hit test that skips shields sees past a hold: under a held modal dialog’s shield, its own action', async ({page,}) => {diff --git a/packages/core/src/Layer/inputHold.test.tsx b/packages/core/src/Layer/inputHold.test.tsxindex b6591142a1..7accfe7c50 100644--- a/packages/core/src/Layer/inputHold.test.tsx+++ b/packages/core/src/Layer/inputHold.test.tsx@@ -571,6 +571,138 @@ describe("holdInput decides by each event's pointer (spec:AST-094 FR35)", () =>expect(shield.style.getPropertyValue('clip-path')).toBe('');});+ /** The part of a clip-path that leaves a 4 px square open around (x, y). */+ const holeAt = (x: number, y: number) =>+ `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 a presentation's shield open where a finger or pen went down before it came up, follows that press, and closes the opening when it lifts", () => {+ const {layer} = mountLayer();+ const outside = document.createElement('button');+ document.body.append(outside);+ const stops = (+ ['pointerdown', 'pointermove', 'pointerup', 'pointercancel'] as const+ ).map(type => subscribeInput(window, type, () => {}));+ const press = (+ type: string,+ pointerType: string,+ id: number,+ x: number,+ y: number,+ ) =>+ outside.dispatchEvent(+ new PointerEvent(type, {+ ...INIT,+ pointerId: id,+ pointerType,+ clientX: x,+ clientY: y,+ }),+ );+ press('pointerdown', 'touch', 61, 20, 680);+ press('pointerdown', 'pen', 62, 300, 40);+ press('pointerdown', 'touch', 63, 90, 90);+ press('pointerup', 'touch', 63, 90, 90);+ hold(layer);+ const shield = layer.lastElementChild as HTMLElement;+ const clip = () => shield.style.getPropertyValue('clip-path');+ expect(clip()).toContain(holeAt(20, 680));+ expect(clip()).toContain(holeAt(300, 40));+ expect(clip()).not.toContain(holeAt(90, 90));+ // The opening follows the press, and closes when it lifts.+ press('pointermove', 'touch', 61, 24, 676);+ expect(clip()).toContain(holeAt(24, 676));+ expect(clip()).not.toContain(holeAt(20, 680));+ press('pointerup', 'touch', 61, 24, 676);+ expect(clip()).not.toContain(holeAt(24, 676));+ press('pointercancel', 'pen', 62, 300, 40);+ expect(clip()).not.toContain(holeAt(300, 40));+ for (const stop of stops) {+ stop();+ }+ outside.remove();+ });++ it('merges an earlier press opening that overlaps the still-mouse opening into one box', () => {+ const {layer} = mountLayer();+ mouseRestsAt(100, 100);+ lastPointerWas('touch');+ const stops = (['pointerdown', 'pointerup', 'pointercancel'] as const).map(+ type => subscribeInput(window, type, () => {}),+ );+ document.body.dispatchEvent(+ new PointerEvent('pointerdown', {+ ...INIT,+ pointerId: 81,+ pointerType: 'touch',+ clientX: 103,+ clientY: 101,+ }),+ );+ hold(layer);+ const shield = layer.lastElementChild as HTMLElement;+ expect(shield.style.getPropertyValue('clip-path')).toBe(+ 'polygon(0 0, 100% 0, 100% 100%, 0 100%, 0 98px, 98px 98px, 98px 103px, 105px 103px, 105px 98px, 0 98px)',+ );+ document.body.dispatchEvent(+ new PointerEvent('pointerup', {+ ...INIT,+ pointerId: 81,+ pointerType: 'touch',+ clientX: 103,+ clientY: 101,+ }),+ );+ expect(shield.style.getPropertyValue('clip-path')).toBe(+ openingAt(100, 100),+ );+ for (const stop of stops) {+ stop();+ }+ });++ it("gives no opening to a press the press hold holds, and none on a dismissal's shield", () => {+ const {layer, child} = mountLayer();+ const stops = (['pointerdown', 'pointerup', 'pointercancel'] as const).map(+ type => subscribeInput(window, type, () => {}),+ );+ const press = (+ target: Element,+ type: string,+ id: number,+ x: number,+ y: number,+ ) =>+ target.dispatchEvent(+ new PointerEvent(type, {+ ...INIT,+ pointerId: id,+ pointerType: 'touch',+ clientX: x,+ clientY: y,+ }),+ );+ press(document.body, 'pointerdown', 71, 10, 10);+ hold(layer, 'surface');+ // Held: it lands on the layer during the surface hold.+ press(child, 'pointerdown', 72, 50, 50);+ hold(layer, 'viewport');+ const shield = layer.lastElementChild as HTMLElement;+ expect(shield.style.getPropertyValue('clip-path')).toContain(+ holeAt(10, 10),+ );+ expect(shield.style.getPropertyValue('clip-path')).not.toContain(+ holeAt(50, 50),+ );+ // A dismissal's shield takes every hit.+ hold(layer, 'layer');+ expect(shield.style.getPropertyValue('clip-path')).toBe('');+ press(document.body, 'pointerup', 71, 10, 10);+ press(child, 'pointerup', 72, 50, 50);+ for (const stop of stops) {+ stop();+ }+ });+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);diff --git a/packages/core/src/Layer/inputHold.ts b/packages/core/src/Layer/inputHold.tsindex eb5438beec..acb6c25131 100644--- a/packages/core/src/Layer/inputHold.ts+++ b/packages/core/src/Layer/inputHold.ts@@ -21,9 +21,11 @@ import {parentAcrossShadow,} from '../utils/flatTree';import {+ getDownPresses,getLastMousePoint,getLastPointerType,subscribeInput,+ type MousePoint,} from '../utils/inputStream';import {isDocument, isElement, isHTMLElement, isNode} from '../utils/nodeType';@@ -58,6 +60,12 @@ interface HeldLayer {isShieldFull: boolean;/** The content a dismissal made inert, and whether each was already. */inertContent: Map<Element, boolean> | null;+ /**+ * The pointer ids of the finger and pen presses that were down when the+ * full shield came up. The shield leaves each one open where the stream+ * last heard it, until its press ends.+ */+ pressOpenings: Set<number>;}/** A document while any of its layers is held. */@@ -72,6 +80,8 @@ interface PressHold {hold: () => void;/** No new press is held; held ones are followed to their end. */release: () => void;+ /** Whether the press with this pointer id is held to its end. */+ holds: (pointerId: number) => boolean;}const held = new WeakMap<Element, HeldLayer>();@@ -266,6 +276,50 @@ function addShield(layer: Element, isFull: boolean): HTMLElement {const MOUSE_OPENING_HALF_SIDE_PX = 2;+interface Opening {+ left: number;+ right: number;+ top: number;+ bottom: number;+}++/**+ * The square opening around each point. Two openings that overlap become one+ * box that covers both: under the nonzero rule, the overlap of two openings+ * drawn the same way round would count twice and close again.+ */+function openings(points: ReadonlyArray<MousePoint>): Opening[] {+ const boxes: Opening[] = [];+ for (const {x, y} of points) {+ let box: Opening = {+ left: x - MOUSE_OPENING_HALF_SIDE_PX,+ right: x + MOUSE_OPENING_HALF_SIDE_PX,+ top: y - MOUSE_OPENING_HALF_SIDE_PX,+ bottom: y + MOUSE_OPENING_HALF_SIDE_PX,+ };+ for (let index = boxes.length - 1; index >= 0; index--) {+ const other = boxes[index];+ if (+ box.left < other.right &&+ other.left < box.right &&+ box.top < other.bottom &&+ other.top < box.bottom+ ) {+ box = {+ left: Math.min(box.left, other.left),+ right: Math.max(box.right, other.right),+ top: Math.min(box.top, other.top),+ bottom: Math.max(box.bottom, other.bottom),+ };+ boxes.splice(index, 1);+ index = boxes.length;+ }+ }+ boxes.push(box);+ }+ return boxes;+}+/*** 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@@ -274,7 +328,9 @@ const MOUSE_OPENING_HALF_SIDE_PX = 2;* 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.+ * full shield also leaves open each finger or pen press that was down before+ * it came up (see `openToEarlierPresses`). A dismissal's shield keeps every+ * hit.*/function openToMouse(state: HeldLayer, doc: Document): void {const {shield} = state;@@ -282,9 +338,19 @@ function openToMouse(state: HeldLayer, doc: Document): void {return;}const view = doc.defaultView;- const point = view == null ? null : getLastMousePoint(view);+ const mouse = view == null ? null : getLastMousePoint(view);+ const downPresses = view == null ? null : getDownPresses(view);+ const presses: MousePoint[] = [];+ if (state.isShieldFull && downPresses != null) {+ for (const pointerId of state.pressOpenings) {+ const point = downPresses.get(pointerId);+ if (point != null) {+ presses.push(point);+ }+ }+ }if (- point == null ||+ (mouse == null && presses.length === 0) ||state.layer > 0 ||shield.style.pointerEvents === 'none') {@@ -295,27 +361,46 @@ function openToMouse(state: HeldLayer, doc: Document): void {shield.style.pointerEvents = 'none';return;}- const left = point.x - MOUSE_OPENING_HALF_SIDE_PX;- const right = point.x + MOUSE_OPENING_HALF_SIDE_PX;- const top = point.y - MOUSE_OPENING_HALF_SIDE_PX;- const bottom = point.y + MOUSE_OPENING_HALF_SIDE_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`,- ];+ // The viewport, then each opening drawn the other way round. The nonzero+ // rule leaves the openings out, and a clipped-out area takes no hits.+ const outline = ['0 0', '100% 0', '100% 100%', '0 100%'];+ for (const {left, right, top, bottom} of openings(+ mouse == null ? presses : [mouse, ...presses],+ )) {+ outline.push(+ `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(', ')})`;}+/**+ * Leaves an opening in a presentation's full shield at each finger or pen+ * press that went down before the shield came up. WebKit hit-tests the lift+ * of such a press again, even with pointer capture, and the lift would land+ * on the shield, inside the layer: a light-dismissing popover would stay+ * open. Through the opening, the lift reaches what is under it, so light+ * dismiss runs as with no hold. A press the press hold holds started during+ * an earlier hold, and gets no opening.+ */+function openToEarlierPresses(state: HeldLayer, doc: Document): void {+ const view = doc.defaultView;+ if (view == null) {+ return;+ }+ const pressHold = pressHolds.get(doc);+ for (const pointerId of getDownPresses(view).keys()) {+ if (pressHold?.holds(pointerId) !== true) {+ state.pressOpenings.add(pointerId);+ }+ }+}+/*** Holds input away from `layer` until `until` settles. Returns the release* (idempotent), for a hold that ends early. Holds on one layer stack, and@@ -336,11 +421,14 @@ export function holdInput(shield: null,isShieldFull: false,inertContent: null,+ pressOpenings: new Set(),};held.set(layer, state);}startHolding(layer);const isFull = scope !== 'surface';+ const raisesFullShield =+ isFull && (state.shield == null || !state.isShieldFull);if (state.shield != null && isFull && !state.isShieldFull) {// A surface hold's shield gives way to one over the viewport.liveShields.delete(state.shield);@@ -368,6 +456,9 @@ export function holdInput(state.inertContent = makeContentInert(layer, state.shield);}}+ if (raisesFullShield) {+ openToEarlierPresses(state, layer.ownerDocument);+ }openToMouse(state, layer.ownerDocument);let isReleased = false;const release = () => {@@ -586,8 +677,33 @@ function createPressHold(doc: Document): PressHold | null {shield.setPointerCapture(event.pointerId);};+ /**+ * An earlier press's opening follows it, and closes when it ends. The+ * stream has already recorded the event's point when the hold hears it.+ */+ const followEarlierPress = (event: PointerEvent) => {+ const {pointerId, type} = event;+ for (const layer of liveLayers(doc)) {+ const state = held.get(layer);+ if (state == null || !state.pressOpenings.has(pointerId)) {+ continue;+ }+ if (type === 'pointerup' || type === 'pointercancel') {+ state.pressOpenings.delete(pointerId);+ }+ openToMouse(state, doc);+ }+ };+const onPointer = (event: Event): boolean => {const {pointerId, pointerType, type} = event as PointerEvent;+ if (+ HELD_POINTERS.has(pointerType) &&+ type !== 'pointerdown' &&+ !pointers.has(pointerId)+ ) {+ followEarlierPress(event as PointerEvent);+ }// A finger or a pen raises the shields. Only a mouse that moves with no// button down lowers them. So a mouse press that began on a shield stays// the shield's to its end. Otherwise its click would go to the two@@ -751,6 +867,7 @@ function createPressHold(doc: Document): PressHold | null {isHolding = false;settle();},+ holds: pointerId => pointers.has(pointerId),};return pressHold;}diff --git a/packages/core/src/utils/inputStream.test.ts b/packages/core/src/utils/inputStream.test.tsindex d1f72062ab..d4cb8ecb29 100644--- a/packages/core/src/utils/inputStream.test.ts+++ b/packages/core/src/utils/inputStream.test.ts@@ -22,6 +22,7 @@ import {afterEach, describe, expect, it, vi} from 'vitest';import {getGestureHost} from '../gestures/GestureHost';import {+ getDownPresses,getLastMousePoint,getLastPointerType,subscribeInput,@@ -259,6 +260,49 @@ describe('inputStream', () => {expect(getLastPointerType(view)).toBe('touch');});+ it("records where each finger's or pen's press that is down was last, while one hears pointer presses, and forgets them when the last listener for their end goes", () => {+ const view = document.defaultView as Window;+ const press = (+ type: string,+ pointerType: string,+ pointerId: number,+ x: number,+ y: number,+ ) =>+ document.body.dispatchEvent(+ new PointerEvent(type, {+ bubbles: true,+ pointerType,+ pointerId,+ clientX: x,+ clientY: y,+ }),+ );+ const stops = (+ ['pointerdown', 'pointermove', 'pointerup', 'pointercancel'] as const+ ).map(type => subscribeInput(view, type, () => {}));+ press('pointerdown', 'touch', 31, 10, 20);+ press('pointerdown', 'pen', 32, 30, 40);+ press('pointerdown', 'mouse', 33, 50, 60);+ // A move of a pointer that is not down records nothing.+ press('pointermove', 'touch', 34, 70, 80);+ press('pointermove', 'touch', 31, 12, 22);+ expect([...getDownPresses(view)]).toEqual([+ [31, {x: 12, y: 22}],+ [32, {x: 30, y: 40}],+ ]);+ press('pointerup', 'touch', 31, 12, 22);+ press('pointercancel', 'pen', 32, 30, 40);+ press('pointerup', 'mouse', 33, 50, 60);+ expect([...getDownPresses(view)]).toEqual([]);+ press('pointerdown', 'touch', 35, 1, 2);+ expect([...getDownPresses(view)]).toEqual([[35, {x: 1, y: 2}]]);+ for (const stop of stops) {+ stop();+ }+ expect([...getDownPresses(view)]).toEqual([]);+ });+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', () => {});diff --git a/packages/core/src/utils/inputStream.ts b/packages/core/src/utils/inputStream.tsindex 8891060767..e3232168f4 100644--- a/packages/core/src/utils/inputStream.ts+++ b/packages/core/src/utils/inputStream.ts@@ -4,9 +4,10 @@* @file inputStream.ts* @input A window, and subscribers to its input events (the press feedback* controller, the gesture host)- * @output subscribeInput, getLastPointerType and getLastMousePoint: the- * type of the latest pointer event the stream delivered, and where the- * latest mouse one was+ * @output subscribeInput, getLastPointerType, getLastMousePoint and+ * getDownPresses: the type of the latest pointer event the stream+ * delivered, where the latest mouse one was, and where each finger and pen+ * press that is down was last* @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@@ -72,6 +73,10 @@ const lastPointerTypes = new WeakMap<Window, string>();/** Where the latest mouse pointer event each window's stream delivered was. */const lastMousePoints = new WeakMap<Window, MousePoint>();+/** Each window's finger and pen presses that are down, by pointer id. */+const downPresses = new WeakMap<Window, Map<number, MousePoint>>();+const NO_PRESSES: ReadonlyMap<number, MousePoint> = new Map();+/** A point in the viewport, in CSS pixels. */export interface MousePoint {readonly x: number;@@ -97,6 +102,17 @@ export function getLastMousePoint(view: Window): MousePoint | null {return lastMousePoints.get(view) ?? null;}+/**+ * The finger and pen presses that are down in `view`, by pointer id, each at+ * the point where the stream last heard it. The stream hears them only while+ * subscribers listen to `pointerdown`, `pointerup` and `pointercancel` (and+ * `pointermove` for a moving press), as press feedback and the gesture host+ * do. Reading it adds no listener.+ */+export function getDownPresses(view: Window): ReadonlyMap<number, MousePoint> {+ return downPresses.get(view) ?? NO_PRESSES;+}+/*** Listen to `type` on `view`'s capture phase through its shared native* listener. Returns the unsubscribe; calling it again does nothing.@@ -148,6 +164,11 @@ export function subscribeInput<K extends keyof InputEventMap>(register(view, type, current);if (current.subscribers.length === 0) {streams.get(view)?.delete(type);+ // Without a listener the stream cannot hear a press end, so the+ // presses it recorded may already have ended.+ if (type === 'pointerup' || type === 'pointercancel') {+ downPresses.delete(view);+ }}};}@@ -190,6 +211,9 @@ function dispatch(view: Window, channel: Channel, event: Event): void {) {lastMousePoints.set(view, {x: clientX, y: clientY});}+ if (pointerType === 'touch' || pointerType === 'pen') {+ recordPress(view, event);+ }// 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]) {@@ -206,6 +230,31 @@ function dispatch(view: Window, channel: Channel, event: Event): void {}}+function recordPress(view: Window, event: Event): void {+ const {pointerId, clientX, clientY} = event as Partial<PointerEvent>;+ if (typeof pointerId !== 'number') {+ return;+ }+ let presses = downPresses.get(view);+ if (event.type === 'pointerup' || event.type === 'pointercancel') {+ presses?.delete(pointerId);+ return;+ }+ if (+ (event.type !== 'pointerdown' &&+ !(event.type === 'pointermove' && presses?.has(pointerId) === true)) ||+ typeof clientX !== 'number' ||+ typeof clientY !== 'number'+ ) {+ return;+ }+ if (presses == null) {+ presses = new Map();+ downPresses.set(view, presses);+ }+ presses.set(pointerId, {x: clientX, y: clientY});+}+function report(error: unknown): void {if (typeof globalThis.reportError === 'function') {globalThis.reportError(error);