Skip to content

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

TypeScript

fix(AlertDialog): a mouse press on the actions acts natively again (#9771)

Christopher Chedeaubb3838b6fd13
diff --git a/.changeset/alertdialog-touch-action-tracking.md b/.changeset/alertdialog-touch-action-tracking.md
index 6b8bc346fb..a83a3ff4cd 100644
--- a/.changeset/alertdialog-touch-action-tracking.md
+++ b/.changeset/alertdialog-touch-action-tracking.md
@@ -2,6 +2,6 @@
'@astryxdesign/core': patch
---
-[fix] AlertDialog: its two actions are tracked as one group under every pointer, the way iOS tracks an alert's actions. A press that lands on one action and slides to the other runs the action it lifts on, and lifting off both runs nothing. The pressed action highlights at once, and the highlight follows the pointer between the actions. A second finger or the window losing focus ends the press with nothing run. A press on the title or the message is not tracked. The keyboard and assistive technology keep each button's own activation.
+[fix] AlertDialog: under the touch presentation, its two actions are tracked as one group for a finger and a pen, the way iOS tracks an alert's actions. A press that lands on one action and slides to the other runs the action it lifts on, and lifting off both runs nothing. The pressed action highlights at once, and the highlight follows the finger between the actions. A second finger or the window losing focus ends the press with nothing run. A press on the title or the message is not tracked. A mouse press stays the native button's, and the keyboard and assistive technology keep each button's own activation.
@vjeux
diff --git a/docs/families/action-groups.md b/docs/families/action-groups.md
index b8f07fdfa5..857d950aeb 100644
--- a/docs/families/action-groups.md
+++ b/docs/families/action-groups.md
@@ -131,11 +131,13 @@ Selector's and MultiSelector's options FR1–FR5
## Cross-component invariants
-- **FR1 — The lift decides.** In a member, a press on an action that slides
- across its siblings acts on the action the pointer lifts on, through that
- action's own activation; a press that never leaves its action acts on it.
- One gesture acts at most once (`spec:AST-094` FR26). After a slide, focus does what
- a click on the lifted action does.
+- **FR1 — The lift decides.** Under the touch presentation (`spec:AST-094`
+ FR13), in a member, a press on an action that slides across its siblings
+ acts on the action the pointer lifts on, through that action's own
+ activation; a press that never leaves its action acts on it. One gesture
+ acts at most once (`spec:AST-094` FR26). After a slide, focus does what a
+ click on the lifted action does. A mouse press acts as the platform's
+ native button does: on the action it began on, released over that action.
- **FR2 — The highlight follows the pointer.** While the pointer is held, the
enabled action under it is highlighted, at most one at a time, and nothing
is highlighted over a disabled action, a gap, or outside the group
@@ -148,13 +150,13 @@ Selector's and MultiSelector's options FR1–FR5
- **FR4 — A toggle group toggles only the lifted action.** In a toggle group,
only the action lifted on toggles; sliding across never toggles the actions
passed over.
-- **FR5 — Every pointer, one target rule.** The behavior applies to every web
- pointer, mouse and pen as well as a finger. Under a fine pointer a group
- keeps one `pointerdown` listener on its element, cancelable where the member
- needs it, adds document listeners only during a press and removes them at
- the release, and may arm one per-document click swallower from the release
- until that gesture's click; nothing else is idle (`spec:AST-094` FR19,
- FR24). The 44 px target is each member's hit
+- **FR5 — A finger and a pen, one target rule.** Under the touch presentation
+ the behavior applies to a finger and a pen; a mouse press acts as FR1 says.
+ While it tracks, a group keeps one `pointerdown` listener on its element,
+ cancelable where the member needs it, adds document listeners only during a
+ press and removes them at the release, and may arm one per-document click
+ swallower from the release until that gesture's click; nothing else is idle
+ (`spec:AST-094` FR19, FR24). The 44 px target is each member's hit
expansion under a coarse pointer only. Keyboard and assistive-technology
behavior is unchanged.
@@ -162,8 +164,12 @@ Selector's and MultiSelector's options FR1–FR5
- What an action does on activation, and how a member shows its highlight,
stay with the member.
-- Menus keep their own opening and handoff (`spec:AST-094` FR29) and their
- scrolling rules.
+- Menus keep their own opening and handoff (`spec:AST-094` FR29), their
+ press and drag under every pointer, and their scrolling rules; FR1's touch
+ scope and FR5 do not change them.
+- Selector's and MultiSelector's options keep the menu press model's press
+ and drag under every pointer (`component:Selector` FR7); FR1's touch scope
+ and FR5 do not change them.
- A wheel is a scroll container: a drag scrolls it, as it does today, and a
tap acts on the option under it; only a press that does not scroll tracks.
@@ -205,6 +211,16 @@ they are outside the family.
- `spec:AST-094/DEC-17` — every group of sibling actions tracks a held
pointer the same way.
+### DEC-1 — The slide across actions is for touch only
+
+**Decider:** `cixzhang`, `2026-11-03`
+
+The slide-across press runs under the touch presentation only. A desktop
+mouse press behaves as a native button does: only the pressed action
+activates, with no drag across the others.
+
+Rejected: every pointer.
+
## Open questions
None.
diff --git a/docs/specs/AST-094-touch-presentations/spec.md b/docs/specs/AST-094-touch-presentations/spec.md
index 4c83a8680e..0cf34e5135 100644
--- a/docs/specs/AST-094-touch-presentations/spec.md
+++ b/docs/specs/AST-094-touch-presentations/spec.md
@@ -322,7 +322,7 @@ a measured, sourced difference exists.
exceptions. This rule governs
this record's behaviors only; `spec:AST-057` keeps its per-event pointer
read for the row swipe, as its record states. A `family:action-groups` member
- tracks its own group as FR24 allows: under a fine pointer it keeps one
+ that tracks its group (`family:action-groups` FR1) keeps, as FR24 allows, one
`pointerdown` listener on the group element, cancelable where the member
needs it, adds document listeners only during a press, removed at the
release, and may arm one per-document click swallower from the release until
@@ -599,7 +599,8 @@ under a finger, and a mouse follows `module:DropdownMenu/useMenuPress` FR3 and
selection and native drag, while a trackpad's horizontal scroll swipes a
row (`spec:AST-057` AR2, DEC-7); a
pinch under a pen; the screen-edge pan under a mouse, a trackpad, or a pen;
- and the 44 px coarse-pointer touch target, which stays coarse-only and never overlaps a
+ the slide across an action group under a mouse, and under any pointer
+ outside the touch presentation (`family:action-groups` FR1); and the 44 px coarse-pointer touch target, which stays coarse-only and never overlaps a
neighbor's target. On the web, a pan takes a trackpad's two-finger scroll
where its record allows it, and never a mouse wheel's notches. A scroll is a
trackpad's only when it carries Chromium's and WebKit's trackpad signature:
@@ -726,9 +727,9 @@ under a finger, and a mouse follows `module:DropdownMenu/useMenuPress` FR3 and
pointer, within FR24's listener budget.
- `design:user-states` DR1, DR3 and `family:buttons`: press timing follows
FR11's profiles under every pointer, the fine-pointer profile included.
-- Every `family:action-groups` member, as its membership rule lists them:
- a press that slides across sibling actions acts on the action it lifts on
- (FR26–FR28), under every pointer.
+- Every `family:action-groups` member outside the menu press model: a press
+ that slides across sibling actions acts on the action it lifts on
+ (FR26–FR28), under the touch presentation (`family:action-groups` FR1).
- Every component under a coarse pointer: no box grows for touch, and a
touch target reaches 44 px only through an invisible hit area clamped by
its neighbors, unless a component or module record sets its own (FR42).
@@ -951,8 +952,9 @@ A person who presses one action and slides to another expects the same result
in a menu, an alert's buttons, a button group, or a toolbar. Every member
group of sibling actions behaves the same way, decided once and built in
two owners: the menu press model and the action-group tracker
-(`family:action-groups`; `architecture:interaction-modality` INV13). The behavior works with a mouse
-as well, so it stays on for every pointer, while the 44 px target stays
+(`family:action-groups`; `architecture:interaction-modality` INV13). Menus keep the behavior for every
+pointer; an action group's slide runs for a finger and a pen under the touch
+presentation (`family:action-groups` DEC-1). The 44 px target stays
touch-only.
Rejected: a tracking rule for menus only.
diff --git a/docs/specs/components/AlertDialog/AlertDialog.spec.md b/docs/specs/components/AlertDialog/AlertDialog.spec.md
index 1f67f08357..c2f309fb3d 100644
--- a/docs/specs/components/AlertDialog/AlertDialog.spec.md
+++ b/docs/specs/components/AlertDialog/AlertDialog.spec.md
@@ -50,7 +50,7 @@ system_specs:
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Public contract | `AlertDialog`, `AlertDialogProps`, and `useImperativeAlertDialog` are exported from `@astryxdesign/core/AlertDialog`; `title`, `description`, `actionLabel`, `onAction`, and `onOpenChange` are required with `isOpen`, plus cancel label, action variant, loading, width, and an inline preview mode. |
| Behavior | A modal confirmation that interrupts: it opens through the native dialog, cannot be dismissed by a click outside, closes on Escape or Cancel through `onOpenChange(false)`, and runs `onAction` on the action button. |
-| End-user impact | A keyboard or screen-reader user is told a decision is needed (the title and description are announced), lands on Cancel, and cannot lose the dialog by clicking past it. On a touch screen the alert scales in and fades out as iOS's does and ignores taps while it enters (FR6, FR7), and on a phone it is 320 px wide (FR9); under every pointer, mouse included, a press that slides from Cancel to the action runs the action it lifts on (FR8). |
+| End-user impact | A keyboard or screen-reader user is told a decision is needed (the title and description are announced), lands on Cancel, and cannot lose the dialog by clicking past it. On a touch screen the alert scales in and fades out as iOS's does and ignores taps while it enters (FR6, FR7), and on a phone it is 320 px wide (FR9); on a touch screen a press slid from Cancel to the action runs the lifted action; a mouse's press acts natively (FR8). |
| Builder impact | None. |
| Compatibility/readiness | FR1–FR5, FR8 and AR1 record shipped behavior, apart from AR1's Tab wrap. FR6, FR7 and FR9 are requirements not shipped yet. Each gap is listed under Current-state impact. |
| Review checks | Reject a modal AlertDialog without `role="alertdialog"`, a title or description not linked by id, initial focus on the destructive action, or a backdrop click that dismisses it. |
@@ -67,8 +67,9 @@ confirmation without assembling it.
## Compatibility and migration
-- Released default preserved: `no` — FR8 changes action activation under a
- slide; shipping FR6, FR7 and FR9 changes the touch presentation and the
+- Released default preserved: `no` — under the touch presentation FR8
+ changes action activation under a slide; a mouse press keeps the native
+ button's; shipping FR6, FR7 and FR9 changes the touch presentation and the
phone width
- Compatibility class: nonbreaking behavior change; no public API, DOM, or
target change
@@ -130,7 +131,7 @@ Consumer migration instructions belong in consumer docs and release notes.
| FR5 | `useImperativeAlertDialog` returns `show`, `hide`, and the element to render, and owns `isOpen`/`onOpenChange` for its caller. | shipped source and focused tests |
| FR6 | Under a touch presentation (`spec:AST-094` FR13), a modal AlertDialog presents by scaling from 1.2 to 1 with its opacity and its backdrop's dimming rising from 0 to 1 on the motion role `springs.alertPresent`, started at the browser's commit, with the larger start kept inside the viewport; it dismisses by a fade alone on `springs.alertDismiss` and closes at the fade's end; a close during the presentation, or a reopen during the dismissal, springs from where it is. The roles are internal, with UIKit's measured values at `tempo` 1 and `'standard'`, and follow the theme's motion knobs (`spec:AST-094` FR4, DEC-15). Under Reduce Motion both are the shared cross-fade (`design:reduced-motion` DR1–DR3). A fine primary pointer keeps today's motion (`spec:AST-094` FR13 and its Non-goals). Implementation gap: not shipped yet (Current-state impact). | proposed from measured iOS 26.2 behavior |
| FR7 | Under a touch presentation, pointer input to the alert is held while it enters (`spec:AST-094` FR35); keyboard and assistive-technology input are never held, and the entrance never makes the content inert. While the alert leaves, its content is inert for the dismissal fade. The hold's timing stays internal (`spec:AST-002` FR3). Implementation gap: not shipped yet (Current-state impact). | `spec:AST-094` FR35; owner decision (DEC-1) |
-| FR8 | AlertDialog's actions are an action group (`family:action-groups`): a press that slides across them runs the action it lifts on, and a lift outside them runs nothing, under every pointer; keyboard and assistive-technology behavior is unchanged. | `family:action-groups`; `spec:AST-094` DEC-17; DEC-3 |
+| FR8 | AlertDialog's actions are an action group (`family:action-groups`): under the touch presentation (`spec:AST-094` FR13), a press that slides across them runs the action it lifts on, and a lift outside them runs nothing; a mouse press acts as the platform's native button does; keyboard and assistive-technology behavior is unchanged. | `family:action-groups`; `spec:AST-094` DEC-17; DEC-3 |
| FR9 | On a phone, a compact window (`spec:AST-012` FR9: narrower than `md` or shorter than 500 px) under a coarse primary pointer, the alert is 320 px wide in every theme, as iOS's is; no theme role sets it. A caller's explicit `width` still wins there (`architecture:public-component-api` INV5), and Dialog's viewport clamp still applies on a screen narrower than the alert. Elsewhere it keeps today's width. Implementation gap: not shipped yet (Current-state impact). | owner decision (DEC-2) |
### Allowed variation
@@ -262,8 +263,8 @@ Rejected: a general compact-dialog width theme role.
**Decider:** `cixzhang`, `2026-10-26`
A press that slides from Cancel to the action runs the action it lifts on, as
-in every group of sibling actions (`spec:AST-094` DEC-17), so an alert's two
-buttons behave like a button group under every pointer.
+in every group of sibling actions (`spec:AST-094` DEC-17), so under the touch
+presentation an alert's two buttons behave like a button group.
Rejected: keeping each action's activation on the browser's click alone.
diff --git a/packages/core/src/AlertDialog/AlertDialog.test.tsx b/packages/core/src/AlertDialog/AlertDialog.test.tsx
index b37767874c..5d7b4d007a 100644
--- a/packages/core/src/AlertDialog/AlertDialog.test.tsx
+++ b/packages/core/src/AlertDialog/AlertDialog.test.tsx
@@ -547,6 +547,27 @@ describe('AlertDialog action tracking', () => {
element.getBoundingClientRect = () => rect;
}
+ function mouse(
+ type: 'pointerdown' | 'pointermove' | 'pointerup',
+ target: Element,
+ point: {x: number; y: number},
+ ): void {
+ touchClock += 16;
+ const event = new PointerEvent(type, {
+ bubbles: true,
+ cancelable: true,
+ pointerId: 1,
+ pointerType: 'mouse',
+ isPrimary: true,
+ button: 0,
+ buttons: type === 'pointerup' ? 0 : 1,
+ clientX: point.x,
+ clientY: point.y,
+ });
+ Object.defineProperty(event, 'timeStamp', {value: touchClock});
+ target.dispatchEvent(event);
+ }
+
function renderOpen(props: {isInline?: boolean; coarse?: boolean} = {}) {
const isCoarse = props.coarse ?? true;
stubAlertDialogMedia({
@@ -598,6 +619,26 @@ describe('AlertDialog action tracking', () => {
}
});
+ it("tracks nothing under a fine primary pointer: a desktop press stays the native button's (family:action-groups FR1)", () => {
+ const {cancel, action} = renderOpen({coarse: false});
+ for (const button of [cancel, action]) {
+ expect(isTrackedByGroup(button)).toBe(false);
+ expect(button).toHaveAttribute('data-astryx-pressable');
+ }
+ });
+
+ it('leaves a mouse press to the native button under the touch presentation: one slid from the action to Cancel runs nothing (family:action-groups FR1)', () => {
+ const {onOpenChange, onAction, cancel, action} = renderOpen();
+ mouse('pointerdown', action, {x: 60, y: 116});
+ mouse('pointermove', action, {x: 60, y: 156});
+ // The press paint stays on the pressed action, as `:active` does.
+ expect(action).toHaveAttribute('data-astryx-press', 'on');
+ expect(cancel).not.toHaveAttribute('data-astryx-press');
+ mouse('pointerup', cancel, {x: 60, y: 156});
+ expect(onOpenChange).not.toHaveBeenCalled();
+ expect(onAction).not.toHaveBeenCalled();
+ });
+
it('runs the action a finger lifts on: from the action to Cancel cancels, from Cancel to the action runs it', () => {
const {onOpenChange, onAction, cancel, action} = renderOpen();
let id = touchId++;
diff --git a/packages/core/src/AlertDialog/AlertDialog.tsx b/packages/core/src/AlertDialog/AlertDialog.tsx
index 83b542e42c..9c518a4f5d 100644
--- a/packages/core/src/AlertDialog/AlertDialog.tsx
+++ b/packages/core/src/AlertDialog/AlertDialog.tsx
@@ -34,6 +34,7 @@ import type {BaseProps} from '../BaseProps';
import {mergeProps} from '../utils/mergeProps';
import {themeProps} from '../utils/themeProps';
import {useMediaQuery} from '../hooks/useMediaQuery';
+import {useTouchPresentation} from '../utils/touchPresentation';
import {
registerEnglishFallback,
useComponentTranslator,
@@ -46,6 +47,9 @@ import {pressGroupItemProps} from '../privateAttributes/pressGroupItem.stylex';
registerEnglishFallback(english);
+const SLIDING_POINTERS = ['touch', 'pen'] as const;
+const ACTION_TRACKING_PROPS = pressGroupItemProps(SLIDING_POINTERS);
+
const SMALL_SCREEN_QUERY = '(max-width: 640px)';
const styles = stylex.create({
@@ -193,15 +197,20 @@ export function AlertDialog({
// Each action carries the press-group marker from its first render, so the
// tracker is its only painter.
const actionsRef = useRef<HTMLElement>(null);
- const isTracking = isOpen && isInline !== true;
+ // Only under the touch presentation does a press slide across the
+ // actions. A desktop press stays the native button's.
+ const {isTouch} = useTouchPresentation();
+ const isTracking = isOpen && isInline !== true && isTouch;
useActionGroupTracking(actionsRef, {
// The actions row holds only the two buttons.
itemSelector: 'button',
highlightOnsetMs: alertController.actions.highlightOnsetMs,
highlightExpansion: alertController.actions.highlightExpansion,
+ // A mouse press acts as a native button's under any presentation.
+ pointerTypes: SLIDING_POINTERS,
isEnabled: isTracking,
});
- const actionTrackingProps = isTracking ? pressGroupItemProps : undefined;
+ const actionTrackingProps = isTracking ? ACTION_TRACKING_PROPS : undefined;
const buttonActionStyle = [
styles.action,
diff --git a/packages/core/src/AlertDialog/__tests__/AlertDialog.interaction.browser.spec.ts b/packages/core/src/AlertDialog/__tests__/AlertDialog.interaction.browser.spec.ts
index 380749c6e0..81fb58dfcb 100644
--- a/packages/core/src/AlertDialog/__tests__/AlertDialog.interaction.browser.spec.ts
+++ b/packages/core/src/AlertDialog/__tests__/AlertDialog.interaction.browser.spec.ts
@@ -305,7 +305,7 @@ test('a tap runs its action exactly once', async ({page}) => {
test.describe('with a mouse', () => {
test.use({hasTouch: false, isMobile: false});
- test('a press on Delete slid to Cancel runs Cancel once, and a click on Delete runs Delete once (family:action-groups FR5)', async ({
+ test('a mouse press on Delete slid to Cancel runs nothing, as a native button does, and a click on Delete runs Delete once (family:action-groups FR1)', async ({
page,
}) => {
await openStory(page, storybook(), 'core-alertdialog--delete');
@@ -313,12 +313,6 @@ test.describe('with a mouse', () => {
expect(
await page.evaluate(() => matchMedia('(pointer: coarse)').matches),
).toBe(false);
- const open = async () => {
- await page.getByRole('button', {name: 'Delete item'}).click();
- const dialog = page.getByRole('alertdialog', {name: 'Delete item?'});
- await expect(dialog).toBeVisible();
- return dialog;
- };
const clicks: string[] = [];
await page.exposeFunction('__alertClick', (label: string) => {
clicks.push(label);
@@ -337,80 +331,28 @@ test.describe('with a mouse', () => {
true,
);
});
- let dialog = await open();
- const del = await centerOf(dialog.getByRole('button', {name: 'Delete'}));
- const cancel = await centerOf(dialog.getByRole('button', {name: 'Cancel'}));
- await page.mouse.move(del.x, del.y);
- await page.mouse.down();
- await page.mouse.move(cancel.x, cancel.y, {steps: 8});
- await page.mouse.up();
- await expect(dialog).toBeHidden();
- expect(clicks).toEqual(['Cancel']);
- // A plain click: the group's click runs it, and the browser's own click
- // for the same press is swallowed.
- clicks.length = 0;
- dialog = await open();
- await dialog.getByRole('button', {name: 'Delete'}).click();
- await expect.poll(() => clicks.length).toBe(1);
- await frames(page, 4);
- expect(clicks).toEqual(['Delete']);
- });
-
- test('a press held on Delete and slid onto Cancel lights only Cancel: Delete keeps no pressed look (family:action-groups FR2)', async ({
- page,
- }) => {
- await openStory(page, storybook(), 'core-alertdialog--delete');
- expect(
- await page.evaluate(() => matchMedia('(pointer: coarse)').matches),
- ).toBe(false);
await page.getByRole('button', {name: 'Delete item'}).click();
const dialog = page.getByRole('alertdialog', {name: 'Delete item?'});
await expect(dialog).toBeVisible();
await waitForRest(dialog);
const delButton = dialog.getByRole('button', {name: 'Delete'});
- const cancelButton = dialog.getByRole('button', {name: 'Cancel'});
- // Whether an action looks pressed: its press paint, its press scale, or
- // a pressed overlay image of nonzero size.
- const looksPressed = async (locator: Locator) =>
- locator.evaluate(element => {
- const style = getComputedStyle(element);
- const scale =
- style.transform !== 'none' &&
- !/^matrix\(1, 0, 0, 1, 0, 0\)$/.test(style.transform);
- return {
- paint: element.getAttribute('data-astryx-press'),
- scaled: scale,
- image:
- style.backgroundImage !== 'none' &&
- style.backgroundSize !== '0px 0px',
- };
- });
- // Both painted and at rest before the press.
- await expect(delButton).toBeVisible();
- await expect(cancelButton).toBeVisible();
- expect(await looksPressed(delButton)).toEqual({
- paint: null,
- scaled: false,
- image: false,
- });
const del = await centerOf(delButton);
- const cancel = await centerOf(cancelButton);
+ const cancel = await centerOf(dialog.getByRole('button', {name: 'Cancel'}));
await page.mouse.move(del.x, del.y);
await page.mouse.down();
- await expect
- .poll(async () => (await looksPressed(delButton)).paint)
- .toBe('on');
await page.mouse.move(cancel.x, cancel.y, {steps: 8});
- await expect
- .poll(async () => (await looksPressed(cancelButton)).paint)
- .toBe('on');
- await expect
- .poll(async () => looksPressed(delButton))
- .toEqual({paint: null, scaled: false, image: false});
- const lit = await looksPressed(cancelButton);
+ // Delete, where the press began, keeps `:active`; no tracker paints.
+ expect(await delButton.evaluate(el => el.matches(':active'))).toBe(true);
+ await expect(dialog.locator('[data-astryx-press]')).toHaveCount(0);
await page.mouse.up();
- expect(lit.paint).toBe('on');
- expect(lit.image).toBe(true);
+ // Past the release's click and the frames after it.
+ await frames(page, 4);
+ expect(clicks).toEqual([]);
+ await expect(dialog).toHaveJSProperty('open', true);
+ // A plain click on Delete runs it, which closes the alert.
+ await delButton.click();
+ await expect.poll(() => clicks).toEqual(['Delete']);
+ await expect(dialog).toBeHidden();
});
test('the dialog keeps its base styles: its text stays selectable', async ({
diff --git a/packages/core/src/privateAttributes/pressGroupItem.stylex.ts b/packages/core/src/privateAttributes/pressGroupItem.stylex.ts
index aa938f26bf..98ce94f4c4 100644
--- a/packages/core/src/privateAttributes/pressGroupItem.stylex.ts
+++ b/packages/core/src/privateAttributes/pressGroupItem.stylex.ts
@@ -3,21 +3,42 @@
* @file privateAttributes/pressGroupItem.stylex.ts
* @input Nothing: the private press-group-item attribute
* @output pressGroupItemProps, which marks an action a group's tracker
- * follows, and isTrackedByGroup, which reads the mark
+ * follows with the pointers it follows, and isTrackedByGroup, which reads
+ * the mark
* @position Owner module of the private press-group-item attribute: the only
* file that writes its name. AlertDialog marks its actions, and the press
- * controller (utils/pressFeedback.ts) leaves their press to the group's
- * tracker. No package entry exports it.
+ * controller (utils/pressFeedback.ts) leaves a press of those pointers to
+ * the group's tracker. No package entry exports it.
*/
const PRESS_GROUP_ITEM = 'data-astryx-press-group-item';
-/** Spread on each action that a group's tracker follows. */
-export const pressGroupItemProps: Readonly<Record<string, string>> = {
- [PRESS_GROUP_ITEM]: '',
-};
+/**
+ * Spread on each action that a group's tracker follows. It names the pointer
+ * types the tracker follows, such as `['touch', 'pen']`.
+ */
+export function pressGroupItemProps(
+ pointerTypes: ReadonlyArray<string>,
+): Readonly<Record<string, string>> {
+ return {[PRESS_GROUP_ITEM]: pointerTypes.join(' ')};
+}
-/** Whether a group's tracker paints the press on `element` or an ancestor. */
-export function isTrackedByGroup(element: Element): boolean {
- return element.closest(`[${PRESS_GROUP_ITEM}]`) != null;
+/**
+ * Whether a group's tracker paints the press on `element` or an ancestor: a
+ * press of `pointerType`, or any press when it is null.
+ */
+export function isTrackedByGroup(
+ element: Element,
+ pointerType: string | null = null,
+): boolean {
+ const marked = element.closest(`[${PRESS_GROUP_ITEM}]`);
+ if (marked == null) {
+ return false;
+ }
+ return (
+ pointerType == null ||
+ (marked.getAttribute(PRESS_GROUP_ITEM) ?? '')
+ .split(' ')
+ .includes(pointerType)
+ );
}
diff --git a/packages/core/src/utils/pressFeedback.test.ts b/packages/core/src/utils/pressFeedback.test.ts
index 940b2dc4ae..e63c4fc4e3 100644
--- a/packages/core/src/utils/pressFeedback.test.ts
+++ b/packages/core/src/utils/pressFeedback.test.ts
@@ -256,7 +256,9 @@ describe('the press reaches the surface the finger landed on', () => {
it('paints a Space press on a tracked action where the primary pointer is fine: a tracked action takes no `:active` paint', () => {
expect(window.matchMedia('(pointer: coarse)').matches).toBe(false);
const button = pressable('button');
- for (const [name, value] of Object.entries(pressGroupItemProps)) {
+ for (const [name, value] of Object.entries(
+ pressGroupItemProps(['touch', 'pen']),
+ )) {
button.setAttribute(name, value);
}
rows[0].appendChild(button);
@@ -312,7 +314,9 @@ describe('an action group whose own tracker paints its actions', () => {
const root = document.createElement('div');
const actions = [pressable('button'), pressable('button')];
for (const action of actions) {
- for (const [name, value] of Object.entries(pressGroupItemProps)) {
+ for (const [name, value] of Object.entries(
+ pressGroupItemProps(['touch', 'pen']),
+ )) {
action.setAttribute(name, value);
}
root.appendChild(action);
@@ -759,22 +763,33 @@ describe('a phone or tablet paints a mouse or Space press itself', () => {
}
});
- function groupAction(row: HTMLElement): HTMLButtonElement {
+ function groupAction(
+ row: HTMLElement,
+ pointerTypes: ReadonlyArray<string> = ['touch', 'pen'],
+ ): HTMLButtonElement {
const action = focusedButton(row);
- for (const [name, value] of Object.entries(pressGroupItemProps)) {
+ for (const [name, value] of Object.entries(
+ pressGroupItemProps(pointerTypes),
+ )) {
action.setAttribute(name, value);
}
return action;
}
- it("leaves a mouse press on an action its group tracks to the group's tracker", () => {
- const action = groupAction(rows[1]);
+ it('leaves a mouse press to a tracker that follows a mouse: it paints its own action', () => {
+ const action = groupAction(rows[1], ['mouse', 'pen', 'touch']);
press(action, 'pointerdown', 10, 100, 'mouse');
expect(action.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
- // The tracker's own paint is left alone through the release.
- action.setAttribute(PRESSED_ATTRIBUTE, 'on');
press(action, 'pointerup', 10, 100, 'mouse');
+ expect(action.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
+ });
+
+ it('paints a mouse press on an action whose tracker follows a finger and a pen only: that tracker takes no mouse press', () => {
+ const action = groupAction(rows[1]);
+ press(action, 'pointerdown', 10, 100, 'mouse');
expect(action.getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
+ press(action, 'pointerup', 10, 100, 'mouse');
+ expect(action.getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
});
it('still paints a Space press on a focused action its group tracks: the group tracks pointers only', () => {
diff --git a/packages/core/src/utils/pressFeedback.ts b/packages/core/src/utils/pressFeedback.ts
index f1de60107b..b580c56ca6 100644
--- a/packages/core/src/utils/pressFeedback.ts
+++ b/packages/core/src/utils/pressFeedback.ts
@@ -338,8 +338,8 @@ function buttonSurfaceAt(
if (element == null || element.closest(MENU_PRESS_ROOT) != null) {
return null;
}
- // A group's tracker follows pointers only, so a key's press stays here.
- if (pointerId != null && isTrackedByGroup(element)) {
+ // A tracker that follows a mouse paints a mouse press on its own actions.
+ if (pointerId != null && isTrackedByGroup(element, 'mouse')) {
return null;
}
if (pointerId == null && element !== target) {
@@ -420,9 +420,14 @@ function onPointerDown(event: PointerEvent): void {
const braking = element != null && scrollerIsBraking(element);
lastTouchAt = Date.now();
lastTouchTarget = event.target instanceof Node ? event.target : null;
- // A group's tracker paints a tracked action, so two writers would erase each
- // other's paint. The touch still counts for the brake above.
- if (element == null || braking || isTrackedByGroup(element)) {
+ // A group's tracker that follows this pointer paints its action, so two
+ // writers would erase each other's paint. The touch still counts for the
+ // brake above.
+ if (
+ element == null ||
+ braking ||
+ isTrackedByGroup(element, event.pointerType)
+ ) {
return;
}
takeOver(button, element);