Skip to content

Commit c190837

Browse files
committed
feat(DropdownMenu): a finger in the open menu is tracked as UIMenu's list tracks it
Measured on the iOS 26.2 simulator (a pull-down button's 3-row and 30-row menus): the row under a finger that lands in the menu lights about 160 ms after the touch (the list's content-touch delay), in a short menu as in a long one; a tap picks at the lift; a quick drag scrolls a long menu (nothing lights or picks) and scrubs a short one, picking the row it lifts on. - MenuSelectionGestureRecognizer (on the gesture system): begins after UIKit's content-touch delay for a still finger, at the lift for a tap, and at once from where it landed when it travels past the press slop in a menu that fits; it fails on travel in a menu that scrolls, and there it never claims the finger, so a drag after the highlight still scrolls the list. - useMenuPress takes it as touchTracking: 'uikit': the recognizer drives the shared action-group tracker (rects read once, highlight = focus, the row runs through the existing activation inside the lift); a mouse keeps the pointer model. DropdownMenu and its sub-menus opt in.
1 parent 47a1734 commit c190837

16 files changed

Lines changed: 1329 additions & 43 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[feature] Under a finger, on every touch device, DropdownMenu tracks a finger that lands in its open menu as the list of an iOS menu does: the row under a resting finger lights after UIKit's content-touch delay (150 ms), a tap picks at the lift, and a quick drag scrolls a menu that scrolls and slides the highlight in one that fits. A mouse keeps the pointer model. useMenuPress takes the behavior as `touchTracking: 'uikit'`, running it on the gesture system and the shared action-group tracker.
6+
7+
@vjeux

‎packages/core/src/DropdownMenu/DropdownMenu.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export const docs = {
276276
usage: {
277277
anatomy,
278278
description:
279-
'A dropdown menu that displays a list of actionable items in a popup triggered by a button. Use to present action options as a next step in a process, or to offer contextual actions without cluttering the interface. Like the menus of macOS and iOS, the row under the pointer when it is released is the row that acts, the highlight follows a held mouse or finger, a mouse opens it on press and can drag straight into it, and a finger held on the trigger opens it with the finger down. Under a finger it behaves like UIMenu on iOS: a finger resting on the trigger for 150 ms opens the menu, the finger that opened it picks a row only once it has moved 10 px (so the row that appears under it is never picked by accident), the menu grows out of its trigger and collapses back into it on UIKit\u2019s springs, and a pan that starts outside it dismisses it as the page scrolls. On a phone the popover menu sits over its trigger as UIMenu does: centered on it, top-aligned with it (bottom-aligned when the trigger is in the lower half of the viewport), 8 px from the viewport\u2019s sides.',
279+
'A dropdown menu that displays a list of actionable items in a popup triggered by a button. Use to present action options as a next step in a process, or to offer contextual actions without cluttering the interface. Like the menus of macOS and iOS, the row under the pointer when it is released is the row that acts, the highlight follows a held mouse or finger, a mouse opens it on press and can drag straight into it, and a finger held on the trigger opens it with the finger down. Under a finger it behaves like UIMenu on iOS: a finger resting on the trigger for 150 ms opens the menu, the finger that opened it picks a row only once it has moved 10 px (so the row that appears under it is never picked by accident), the menu grows out of its trigger and collapses back into it on UIKit\u2019s springs, and a pan that starts outside it dismisses it as the page scrolls. A finger that lands in the open menu lights its row after UIKit\u2019s content-touch delay (150 ms) and a tap picks at the lift; a quick drag scrolls a menu that scrolls and slides the highlight in one that fits. On a phone the popover menu sits over its trigger as UIMenu does: centered on it, top-aligned with it (bottom-aligned when the trigger is in the lower half of the viewport), 8 px from the viewport\u2019s sides.',
280280
bestPractices: [
281281
{
282282
guidance: true,

‎packages/core/src/DropdownMenu/DropdownMenu.test.tsx‎

Lines changed: 194 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {DropdownMenuItem} from './DropdownMenuItem';
2828
import {DropdownMenuDivider} from './DropdownMenuDivider';
2929
import {DropdownMenuGroup} from './DropdownMenuGroup';
3030
import {warmMenuBottomSheet} from './__tests__/warmMenuBottomSheet';
31+
import {finger, placeRows, rowCenter} from './__tests__/fingerTouch';
3132
import {DropdownMenuSubMenu} from './DropdownMenuSubMenu';
3233
import {Divider} from '../Divider';
3334
import {rtlStyles} from '../utils';
@@ -3019,21 +3020,211 @@ describe('DropdownMenu press model', () => {
30193020
expect(window.matchMedia(TOUCH_QUERY).matches).toBe(true);
30203021
}
30213022

3023+
it('a closed menu adds no listener: its finger tracking joins the gesture system only while it shows', async () => {
3024+
const add = vi.spyOn(document, 'addEventListener');
3025+
const remove = vi.spyOn(document, 'removeEventListener');
3026+
const touches = (spy: typeof add) =>
3027+
spy.mock.calls.filter(([type]) =>
3028+
['pointercancel', 'touchstart'].includes(type),
3029+
).length;
3030+
try {
3031+
const user = userEvent.setup();
3032+
const trigger = renderMenu();
3033+
expect(touches(add)).toBe(0);
3034+
await user.click(trigger);
3035+
expect(touches(add)).toBeGreaterThan(0);
3036+
await user.keyboard('{Escape}');
3037+
expect(trigger).toHaveAttribute('aria-expanded', 'false');
3038+
expect(touches(remove)).toBe(touches(add));
3039+
} finally {
3040+
add.mockRestore();
3041+
remove.mockRestore();
3042+
}
3043+
});
3044+
30223045
it('a finger that lands on one row and lifts on another acts on the second, once', async () => {
3046+
stubCoarsePointer();
30233047
const onPick = vi.fn();
30243048
const user = userEvent.setup();
30253049
const trigger = renderMenu(onPick);
30263050
await user.click(trigger);
3027-
fireEvent.pointerDown(item('Edit'), touch);
3028-
fireEvent.pointerMove(item('Delete'), touch);
3051+
// The finger is tracked by the gesture system (as UIMenu's list tracks
3052+
// it): touch events over rows with a layout box.
3053+
placeRows([item('Edit'), item('Duplicate'), item('Delete')]);
3054+
const contact = finger();
3055+
contact.down(item('Edit'), rowCenter(0));
3056+
contact.move(item('Edit'), rowCenter(2));
30293057
expect(item('Delete')).toHaveFocus();
3030-
fireEvent.pointerUp(item('Delete'), touch);
3058+
contact.up(item('Edit'), rowCenter(2));
30313059
// The WebKit tail: a click aimed at the row the touch began on.
30323060
fireEvent.click(item('Edit'), {detail: 1});
30333061
expect(onPick).toHaveBeenCalledTimes(1);
30343062
expect(onPick).toHaveBeenCalledWith('Delete');
30353063
});
30363064

3065+
describe('a finger in the menu, tracked as UIMenu\u2019s list tracks it', () => {
3066+
// iOS 26.2: a row highlights about 160 ms after the finger lands (the
3067+
// list's content-touch delay), a tap picks at the lift, and a quick drag
3068+
// scrolls a long menu but scrubs a short one. One model on every touch
3069+
// device.
3070+
const stubFinger = () => {
3071+
vi.stubGlobal(
3072+
'matchMedia',
3073+
vi.fn().mockImplementation((query: string) => ({
3074+
matches: query === '(pointer: coarse)',
3075+
media: query,
3076+
onchange: null,
3077+
addEventListener: vi.fn(),
3078+
removeEventListener: vi.fn(),
3079+
addListener: vi.fn(),
3080+
removeListener: vi.fn(),
3081+
dispatchEvent: vi.fn(),
3082+
})),
3083+
);
3084+
// jsdom runs no animations (UIMenu's open under a finger).
3085+
HTMLElement.prototype.animate = vi.fn(function () {
3086+
return {
3087+
cancel: vi.fn(),
3088+
finished: new Promise(() => {}),
3089+
startTime: null,
3090+
} as unknown as Animation;
3091+
});
3092+
};
3093+
beforeEach(() => {
3094+
stubFinger();
3095+
});
3096+
afterEach(() => {
3097+
vi.unstubAllGlobals();
3098+
Reflect.deleteProperty(HTMLElement.prototype, 'animate');
3099+
});
3100+
async function openPlaced(onPick: (label: string) => void) {
3101+
const user = userEvent.setup();
3102+
const trigger = renderMenu(onPick);
3103+
await user.click(trigger);
3104+
const rows = [item('Edit'), item('Duplicate'), item('Delete')];
3105+
placeRows(rows);
3106+
return {trigger, rows};
3107+
}
3108+
const ONSET = 150;
3109+
3110+
it('a resting finger highlights its row only after the content-touch delay, and the lift picks it once', async () => {
3111+
const onPick = vi.fn();
3112+
const {rows} = await openPlaced(onPick);
3113+
const contact = finger();
3114+
contact.down(rows[0], rowCenter(0));
3115+
contact.move(rows[0], {...rowCenter(0), y: rowCenter(0).y + 2}, 100);
3116+
expect(rows[0]).not.toHaveFocus();
3117+
contact.move(
3118+
rows[0],
3119+
{...rowCenter(0), y: rowCenter(0).y + 3},
3120+
ONSET - 90,
3121+
);
3122+
expect(rows[0]).toHaveFocus();
3123+
contact.up(rows[0], rowCenter(0));
3124+
fireEvent.click(rows[0], {detail: 1});
3125+
expect(onPick).toHaveBeenCalledTimes(1);
3126+
expect(onPick).toHaveBeenCalledWith('Edit');
3127+
});
3128+
3129+
it('a tap picks the row at the lift', async () => {
3130+
const onPick = vi.fn();
3131+
const {rows} = await openPlaced(onPick);
3132+
const contact = finger();
3133+
contact.down(rows[1], rowCenter(1));
3134+
contact.up(rows[1], rowCenter(1), 60);
3135+
expect(onPick).toHaveBeenCalledTimes(1);
3136+
expect(onPick).toHaveBeenCalledWith('Duplicate');
3137+
});
3138+
3139+
it('a second finger that lands before the row lights ends the press with nothing acting (FR4)', async () => {
3140+
const onPick = vi.fn();
3141+
const {rows} = await openPlaced(onPick);
3142+
const first = finger();
3143+
const second = finger();
3144+
first.down(rows[0], rowCenter(0));
3145+
second.down(rows[2], rowCenter(2), {after: 40});
3146+
first.up(rows[0], rowCenter(0), 40);
3147+
second.up(rows[2], rowCenter(2));
3148+
expect(onPick).not.toHaveBeenCalled();
3149+
expect(rows[0]).not.toHaveFocus();
3150+
});
3151+
3152+
it('a second finger that lands once the row is lit ends the press with nothing acting (FR4)', async () => {
3153+
const onPick = vi.fn();
3154+
const {rows} = await openPlaced(onPick);
3155+
const first = finger();
3156+
const second = finger();
3157+
first.down(rows[0], rowCenter(0));
3158+
first.move(rows[0], {...rowCenter(0), y: rowCenter(0).y + 2}, ONSET + 30);
3159+
expect(rows[0]).toHaveFocus();
3160+
second.down(rows[2], rowCenter(2), {after: 20});
3161+
first.up(rows[0], rowCenter(0));
3162+
second.up(rows[2], rowCenter(2));
3163+
fireEvent.click(rows[0], {detail: 1});
3164+
expect(onPick).not.toHaveBeenCalled();
3165+
});
3166+
3167+
it('a finger that lands on a row and lifts outside the menu acts on nothing and leaves it open (FR3)', async () => {
3168+
const onPick = vi.fn();
3169+
const {trigger, rows} = await openPlaced(onPick);
3170+
const contact = finger();
3171+
contact.down(rows[0], rowCenter(0));
3172+
contact.move(rows[0], {x: rowCenter(0).x, y: 900}, ONSET + 30);
3173+
contact.up(rows[0], {x: rowCenter(0).x, y: 900});
3174+
fireEvent.click(rows[0], {detail: 1});
3175+
expect(onPick).not.toHaveBeenCalled();
3176+
expect(trigger).toHaveAttribute('aria-expanded', 'true');
3177+
});
3178+
3179+
it('in a menu that fits, a quick drag scrubs from where it landed and picks the row it lifts on', async () => {
3180+
const onPick = vi.fn();
3181+
const {rows} = await openPlaced(onPick);
3182+
const contact = finger();
3183+
contact.down(rows[0], rowCenter(0));
3184+
contact.move(rows[0], rowCenter(2), 30);
3185+
expect(rows[2]).toHaveFocus();
3186+
contact.up(rows[0], rowCenter(2), 30);
3187+
expect(onPick).toHaveBeenCalledTimes(1);
3188+
expect(onPick).toHaveBeenCalledWith('Delete');
3189+
});
3190+
3191+
it('in a menu that scrolls, a quick drag is the list\u2019s scroll: nothing highlights, nothing is picked', async () => {
3192+
const onPick = vi.fn();
3193+
const {rows} = await openPlaced(onPick);
3194+
const menu = screen.getByRole('menu', {hidden: true});
3195+
Object.defineProperty(menu, 'scrollHeight', {value: 600});
3196+
Object.defineProperty(menu, 'clientHeight', {value: 300});
3197+
const contact = finger();
3198+
contact.down(rows[0], rowCenter(0));
3199+
contact.move(rows[0], rowCenter(2), 30);
3200+
expect(rows[2]).not.toHaveFocus();
3201+
expect(rows[0]).not.toHaveFocus();
3202+
contact.up(rows[0], rowCenter(2), 30);
3203+
expect(onPick).not.toHaveBeenCalled();
3204+
});
3205+
3206+
describe('an Android finger (no iOS engine) is tracked the same way', () => {
3207+
it('its row lights only after the content-touch delay, and the lift picks it once', async () => {
3208+
const onPick = vi.fn();
3209+
const {rows} = await openPlaced(onPick);
3210+
const contact = finger();
3211+
contact.down(rows[0], rowCenter(0));
3212+
contact.move(rows[0], {...rowCenter(0), y: rowCenter(0).y + 2}, 100);
3213+
expect(rows[0]).not.toHaveFocus();
3214+
contact.move(
3215+
rows[0],
3216+
{...rowCenter(0), y: rowCenter(0).y + 3},
3217+
ONSET - 90,
3218+
);
3219+
expect(rows[0]).toHaveFocus();
3220+
contact.up(rows[0], rowCenter(0));
3221+
fireEvent.click(rows[0], {detail: 1});
3222+
expect(onPick).toHaveBeenCalledTimes(1);
3223+
expect(onPick).toHaveBeenCalledWith('Edit');
3224+
});
3225+
});
3226+
});
3227+
30373228
it('a mouse released outside closes the menu; a finger released outside leaves it open', async () => {
30383229
const user = userEvent.setup();
30393230
const trigger = renderMenu();

‎packages/core/src/DropdownMenu/DropdownMenu.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,9 +1569,11 @@ function DropdownMenuPopover({
15691569
itemSelector: MENU_ITEM_SELECTOR,
15701570
onTriggerPress: handleTriggerPress,
15711571
onDismiss: closeMenu,
1572-
// Under a finger the trigger is UIMenu's pull-down button (spec:AST-094);
1572+
// Under a finger the trigger opens as spec:AST-094 FR29 states and a
1573+
// finger in the menu is tracked as UIMenu's list tracks it (FR26–FR30);
15731574
// `presentation="popover"` pins the pointer model, today's included.
15741575
touchModel: touchModes ? 'uikit' : 'pointer',
1576+
isOpen,
15751577
});
15761578
cancelMenuPressRef.current = menuPress.cancel;
15771579

@@ -1711,8 +1713,9 @@ function DropdownMenuPopover({
17111713
host: surfaceHost,
17121714
ownerId: menuId,
17131715
menuMaxHeight: resolvedMaxHeight ?? undefined,
1716+
touchModes: touchModes === true,
17141717
}),
1715-
[surfaceHost, menuId, resolvedMaxHeight],
1718+
[surfaceHost, menuId, resolvedMaxHeight, touchModes],
17161719
);
17171720

17181721
// Resolve menu content: data-driven items become components

‎packages/core/src/DropdownMenu/DropdownMenuContext.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ export interface DropdownMenuSurfaceValue {
9393
ownerId: string;
9494
/** The root's resolved `menuMaxHeight`, which a drilled-in view caps at. */
9595
menuMaxHeight?: string;
96+
/**
97+
* Whether the root's touch modes apply (its resolved presentation): a
98+
* sub-menu tracks a finger as the root does.
99+
*/
100+
touchModes: boolean;
96101
}
97102

98103
/**

0 commit comments

Comments
 (0)