Skip to content

Commit abc8dbf

Browse files
committed
fix(DropdownMenu): a cancelled touch clears the row a finger lit
Under a finger, on every device and every menu surface, a press that the browser takes (pointercancel, a second finger, a scroll) now clears the row the finger lit, as UIKit clears it when the list starts scrolling. That holds under any presentation, in sub-menus and in the bottom sheet's action list. When the window loses focus under a finger, its gesture is withdrawn: it stays tracked to its release, which acts on nothing and whose click is swallowed, and any new press ends it. A mouse keeps its highlight and its click. - menuPressGesture: the withdraw input and withdrawn phase, and the clear flag on a finger's cancel. - useMenuPress: both clears follow the machine's clear flag, which only a finger or pen sets. - MenuBottomSheetActionList: its list takes focus (tabIndex -1) when no row is lit, as DropdownMenu's own menu does, so a cleared row loses focus. - Tests: the machine, hook, sub-menu and sheet-list cases, and the accessibility case in real Chromium. A [fix] changeset.
1 parent 7419b9d commit abc8dbf

10 files changed

Lines changed: 627 additions & 22 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+
[fix] After the window loses focus during a finger's press, lifting that finger no longer acts on any menu (DropdownMenu, MoreMenu, ContextMenu, Selector, and menus built on the public `useMenuPress`), and a tap on a menu's trigger still opens it. Under a finger, a row lit by a press that is cancelled (a scroll, a second finger, the window losing focus) now clears, as on iOS (`spec:AST-094` FR28). It clears in DropdownMenu and its sub-menus under any `presentation`, MoreMenu, ContextMenu, Selector's list and the bottom sheet's action list, and in a menu built on the public `useMenuPress` whose root can take focus. A mouse keeps its highlight.
6+
7+
@vjeux

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,22 @@ describe('DropdownMenu press model', () => {
34803480
}
34813481
});
34823482

3483+
it('presentation="popover": a blur during a finger\u2019s tap on the trigger still lets the tap\u2019s click open the menu', () => {
3484+
render(
3485+
<DropdownMenu button={{label: 'Pinned'}} presentation="popover">
3486+
<DropdownMenuItem label="Edit" />
3487+
</DropdownMenu>,
3488+
);
3489+
const trigger = screen.getByRole('button', {name: /Pinned/});
3490+
fireEvent.pointerDown(trigger, touch);
3491+
act(() => {
3492+
window.dispatchEvent(new FocusEvent('blur'));
3493+
});
3494+
fireEvent.pointerUp(trigger, touch);
3495+
fireEvent.click(trigger, {detail: 1});
3496+
expect(trigger).toHaveAttribute('aria-expanded', 'true');
3497+
});
3498+
34833499
it('presentation="popover" pins the settle rule under a finger: once the opening press has settled, a still lift picks the row under it', () => {
34843500
// A moved touch default comes back through
34853501
// `presentation`, never through a public threshold.

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {RadioList, RadioListItem} from '../RadioList';
2828
import {COMPACT_TOUCH_PRESENTATION_QUERY} from '../hooks/useAdaptivePresentation';
2929
import {Theme, defineTheme} from '../theme';
3030
import {rtlStyles} from '../utils';
31+
import {TOUCH_QUERY} from '../utils/touchPresentation';
3132

3233
beforeEach(() => {
3334
HTMLElement.prototype.showPopover = vi.fn(function (this: HTMLElement) {
@@ -1317,6 +1318,75 @@ describe('DropdownMenuSubMenu hover/click guard', () => {
13171318
});
13181319

13191320
describe('DropdownMenuSubMenu press model', () => {
1321+
const finger = (pointerId = 1) => ({pointerType: 'touch', pointerId});
1322+
1323+
it('a cancelled finger in a flyout clears its lit row as the root\u2019s does, on every device (spec:AST-094 FR28, FR43), presentation="popover" too', async () => {
1324+
const originalMatchMedia = window.matchMedia;
1325+
// A large screen, its primary pointer coarse (a tablet) or fine (a
1326+
// touchscreen laptop): the root is an anchored popover and its sub-menu
1327+
// flies out.
1328+
const stubPrimaryPointer = (coarse: boolean) =>
1329+
vi.stubGlobal(
1330+
'matchMedia',
1331+
vi.fn().mockImplementation((query: string) => ({
1332+
matches: coarse && query === TOUCH_QUERY,
1333+
media: query,
1334+
onchange: null,
1335+
addEventListener: vi.fn(),
1336+
removeEventListener: vi.fn(),
1337+
addListener: vi.fn(),
1338+
removeListener: vi.fn(),
1339+
dispatchEvent: vi.fn(),
1340+
})),
1341+
);
1342+
try {
1343+
const litAfterCancel = async (presentation?: 'popover') => {
1344+
const user = userEvent.setup();
1345+
const {unmount} = render(
1346+
<DropdownMenu button={{label: 'Actions'}} presentation={presentation}>
1347+
<DropdownMenuItem label="Rename" onClick={() => {}} />
1348+
<DropdownMenuSubMenu label="Move to">
1349+
<DropdownMenuItem label="Folder A" onClick={() => {}} />
1350+
<DropdownMenuItem label="Folder B" onClick={() => {}} />
1351+
</DropdownMenuSubMenu>
1352+
</DropdownMenu>,
1353+
);
1354+
await user.click(screen.getByRole('button', {name: /Actions/}));
1355+
const trigger = screen.getByRole('menuitem', {
1356+
name: /Move to/,
1357+
hidden: true,
1358+
});
1359+
fireEvent.pointerDown(trigger, finger());
1360+
fireEvent.pointerUp(trigger, finger());
1361+
expect(trigger).toHaveAttribute('aria-expanded', 'true');
1362+
const folderA = screen.getByRole('menuitem', {
1363+
name: 'Folder A',
1364+
hidden: true,
1365+
});
1366+
const folderB = screen.getByRole('menuitem', {
1367+
name: 'Folder B',
1368+
hidden: true,
1369+
});
1370+
// A finger lands in the flyout and slides onto Folder B, which lights;
1371+
// then the browser takes the touch (a scroll, a system gesture).
1372+
fireEvent.pointerDown(folderA, finger(2));
1373+
fireEvent.pointerMove(folderB, finger(2));
1374+
expect(folderB).toHaveFocus();
1375+
fireEvent.pointerCancel(folderB, finger(2));
1376+
const lit = document.activeElement === folderB;
1377+
unmount();
1378+
return lit;
1379+
};
1380+
for (const coarse of [true, false]) {
1381+
stubPrimaryPointer(coarse);
1382+
expect(await litAfterCancel(), `coarse=${coarse}`).toBe(false);
1383+
expect(await litAfterCancel('popover'), `coarse=${coarse}`).toBe(false);
1384+
}
1385+
} finally {
1386+
vi.stubGlobal('matchMedia', originalMatchMedia);
1387+
}
1388+
});
1389+
13201390
it('a press released on the sub-menu row opens it and keeps the menu', async () => {
13211391
const user = userEvent.setup();
13221392
render(<MoveMenu />);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
import {fireEvent, render, screen} from '@testing-library/react';
4+
import {afterEach, describe, expect, it} from 'vitest';
5+
import {__resetMenuPressForTest} from '../hooks/useMenuPress';
6+
import {MenuBottomSheetActionList} from './MenuBottomSheetActionList';
7+
8+
afterEach(() => {
9+
__resetMenuPressForTest();
10+
});
11+
12+
const touch = (pointerId = 1) => ({pointerType: 'touch', pointerId});
13+
const mouse = (pointerId = 1) => ({
14+
pointerType: 'mouse',
15+
pointerId,
16+
button: 0,
17+
});
18+
19+
function renderList() {
20+
return render(
21+
<MenuBottomSheetActionList
22+
items={[
23+
{label: 'Rename', onClick: () => {}},
24+
{label: 'Delete', onClick: () => {}},
25+
]}
26+
onSelect={() => {}}
27+
onOpenSubmenu={() => {}}
28+
/>,
29+
);
30+
}
31+
32+
describe('MenuBottomSheetActionList', () => {
33+
it('a cancelled finger clears the row it lit in the sheet\u2019s action list; a mouse keeps its highlight (spec:AST-094 FR28, spec:AST-017 FR59)', () => {
34+
const {unmount} = renderList();
35+
fireEvent.pointerDown(
36+
screen.getByRole('button', {name: 'Rename'}),
37+
touch(),
38+
);
39+
fireEvent.pointerMove(
40+
screen.getByRole('button', {name: 'Delete'}),
41+
touch(),
42+
);
43+
expect(screen.getByRole('button', {name: 'Delete'})).toHaveFocus();
44+
// The browser takes the touch: a scroll of the sheet, say.
45+
fireEvent.pointerCancel(
46+
screen.getByRole('button', {name: 'Delete'}),
47+
touch(),
48+
);
49+
expect(screen.getByRole('button', {name: 'Delete'})).not.toHaveFocus();
50+
unmount();
51+
52+
renderList();
53+
fireEvent.pointerDown(
54+
screen.getByRole('button', {name: 'Rename'}),
55+
mouse(),
56+
);
57+
fireEvent.pointerMove(
58+
screen.getByRole('button', {name: 'Delete'}),
59+
mouse(),
60+
);
61+
fireEvent.pointerCancel(
62+
screen.getByRole('button', {name: 'Delete'}),
63+
mouse(),
64+
);
65+
expect(screen.getByRole('button', {name: 'Delete'})).toHaveFocus();
66+
});
67+
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ export function MenuBottomSheetActionList({
151151
<List
152152
ref={listComposedRef}
153153
density="spacious"
154+
// Focus parks on the list when no row is lit, as in DropdownMenu's own
155+
// menu, so a row a cancelled finger lit loses its highlight.
156+
tabIndex={-1}
154157
xstyle={styles.list}
155158
{...menuPress.menuProps}>
156159
{items.map((option, index) => {

‎packages/core/src/DropdownMenu/__tests__/MenuPress.a11y.chromium.spec.ts‎

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
* @file MenuPress.a11y.chromium.spec.ts
66
* @input Built DropdownMenu and Selector stories and real Chromium mouse,
77
* touch (CDP) and pen (CDP) input
8-
* @output Browser evidence for the menu press model: the row under the
9-
* release acts once (a link row included: no native link drag cancels a
10-
* mouse press), the highlight follows the pointer, a release outside
11-
* closes under a mouse and not under a finger, a touch pan in an
12-
* overflowing menu cancels the gesture, a pen behaves like a finger, and
13-
* Escape after a press-open returns focus to the trigger
8+
* @output Browser evidence for the menu press model. The row under the
9+
* release acts once, a link row included: no native link drag cancels a
10+
* mouse press. The highlight follows the pointer. A release outside closes
11+
* under a mouse and not under a finger. A touch pan in an overflowing menu
12+
* cancels the gesture. A cancelled touch and the window losing focus end a
13+
* finger's gesture with nothing acting and its lit row cleared. A pen
14+
* behaves like a finger, and Escape after a press-open returns focus to
15+
* the trigger.
1416
* @position Real-engine proof for module:DropdownMenu/useMenuPress — the
1517
* paths jsdom cannot exercise (hit testing, touch synthesis, scrolling)
1618
*/
@@ -373,6 +375,77 @@ test.describe('DropdownMenu press model (Chromium)', () => {
373375
expect(activations).toEqual([]);
374376
});
375377

378+
test('touch: a cancelled touch ends the gesture: nothing acts and the lit row clears (spec:AST-094 FR28)', async ({
379+
page,
380+
}) => {
381+
const activations = collectActivations(page);
382+
const cdp = await enableTouch(page);
383+
await mount(page, MENU_STORY);
384+
await touch.tap(cdp, await center(page, TRIGGER));
385+
const menu = page.getByRole('menu');
386+
await expect(menu).toBeVisible();
387+
// Measure the rows once the menu has landed: under a finger it may
388+
// still be growing out of its trigger.
389+
await expect
390+
.poll(async () =>
391+
menu.evaluate(
392+
element =>
393+
(element.closest('[popover]') ?? element)
394+
.getAnimations({subtree: true})
395+
.filter(animation => animation.playState === 'running').length,
396+
),
397+
)
398+
.toBe(0);
399+
const edit = await center(page, 'menuitem', 'Edit');
400+
await touch.start(cdp, edit);
401+
await expect.poll(async () => activeRowName(page)).toBe('Edit');
402+
// The browser takes the touch.
403+
await cdp.send('Input.dispatchTouchEvent', {
404+
type: 'touchCancel',
405+
touchPoints: [],
406+
});
407+
await expect.poll(async () => activeRowName(page)).toBeNull();
408+
await page.waitForTimeout(300);
409+
await expect(menu).toBeVisible();
410+
expect(activations).toEqual([]);
411+
});
412+
413+
test('touch: the window losing focus ends the gesture: nothing acts, the lit row clears, and the lift acts on nothing (spec:AST-094 FR28)', async ({
414+
page,
415+
}) => {
416+
const activations = collectActivations(page);
417+
const cdp = await enableTouch(page);
418+
await mount(page, MENU_STORY);
419+
await touch.tap(cdp, await center(page, TRIGGER));
420+
const menu = page.getByRole('menu');
421+
await expect(menu).toBeVisible();
422+
// Measure the rows once the menu has landed: under a finger it may
423+
// still be growing out of its trigger.
424+
await expect
425+
.poll(async () =>
426+
menu.evaluate(
427+
element =>
428+
(element.closest('[popover]') ?? element)
429+
.getAnimations({subtree: true})
430+
.filter(animation => animation.playState === 'running').length,
431+
),
432+
)
433+
.toBe(0);
434+
const edit = await center(page, 'menuitem', 'Edit');
435+
await touch.start(cdp, edit);
436+
await expect.poll(async () => activeRowName(page)).toBe('Edit');
437+
// The window loses focus with the finger down (a headless page keeps
438+
// focus, so the blur is dispatched to the window).
439+
await page.evaluate(() => {
440+
window.dispatchEvent(new FocusEvent('blur'));
441+
});
442+
await expect.poll(async () => activeRowName(page)).toBeNull();
443+
await touch.end(cdp);
444+
await page.waitForTimeout(300);
445+
await expect(menu).toBeVisible();
446+
expect(activations).toEqual([]);
447+
});
448+
376449
test('touch: a menu a held finger opens stays open when that finger lifts on the trigger', async ({
377450
page,
378451
}) => {

‎packages/core/src/hooks/menuPressGesture.test.ts‎

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,24 @@ describe('menuPressStep — inside the menu', () => {
115115
expect(last).toEqual({type: 'settle', stray: true, dismiss: false});
116116
});
117117

118-
it('Tracking → Idle: a cancel (scroll, second pointer, browser) acts on nothing', () => {
118+
it('Tracking → Idle: a cancel (scroll, second pointer, browser) acts on nothing and clears the lit row', () => {
119119
const {gesture, last} = run([downInMenu('A'), move('B'), {type: 'cancel'}]);
120120
expect(gesture.phase).toBe('idle');
121+
expect(last).toEqual({
122+
type: 'settle',
123+
stray: false,
124+
dismiss: false,
125+
clear: true,
126+
});
127+
});
128+
129+
it('Tracking → Idle: a cancelled MOUSE press keeps its lit row', () => {
130+
const {last} = run([downInMenu('A', 'mouse'), move('B'), {type: 'cancel'}]);
131+
expect(last).toEqual({type: 'settle', stray: false, dismiss: false});
132+
});
133+
134+
it('Tracking → Idle: a cancel with nothing lit has nothing to clear', () => {
135+
const {last} = run([downInMenu('A'), move(null), {type: 'cancel'}]);
121136
expect(last).toEqual({type: 'settle', stray: false, dismiss: false});
122137
});
123138

@@ -402,6 +417,68 @@ describe('menuPressStep — a finger that opened the menu hands off after travel
402417
});
403418
});
404419

420+
describe('menuPressStep — the window loses focus mid-press (spec:AST-094 FR28)', () => {
421+
it('a finger\u2019s gesture is withdrawn, not ended: its lit row clears, and nothing it does highlights', () => {
422+
const {gesture, effects} = run([
423+
downInMenu('A'),
424+
{type: 'withdraw'},
425+
move('B'),
426+
]);
427+
expect(gesture).toEqual({phase: 'withdrawn', pointerType: 'touch'});
428+
expect(effects.slice(1)).toEqual([
429+
{type: 'withdraw', clear: true},
430+
{type: 'none'},
431+
]);
432+
});
433+
434+
it('the withdrawn finger\u2019s release acts on nothing and swallows its click; a cancel ends it quietly', () => {
435+
expect(run([downInMenu('A'), {type: 'withdraw'}, up('B')])).toMatchObject({
436+
gesture: {phase: 'idle'},
437+
last: {type: 'settle', stray: true, dismiss: false},
438+
});
439+
expect(
440+
run([downInMenu('A'), {type: 'withdraw'}, {type: 'cancel'}]),
441+
).toMatchObject({
442+
gesture: {phase: 'idle'},
443+
last: {type: 'settle', stray: false, dismiss: false},
444+
});
445+
});
446+
447+
it('a finger holding the trigger, or one that opened the menu, is withdrawn with nothing to clear', () => {
448+
expect(run([downOnTrigger('touch'), {type: 'withdraw'}])).toMatchObject({
449+
gesture: {phase: 'withdrawn'},
450+
last: {type: 'withdraw'},
451+
});
452+
expect(
453+
run([
454+
downOnTrigger('pen'),
455+
{type: 'opened', time: 500},
456+
{type: 'withdraw'},
457+
]),
458+
).toMatchObject({gesture: {phase: 'withdrawn'}, last: {type: 'withdraw'}});
459+
});
460+
461+
it('a finger\u2019s tap on the trigger that the blur withdrew lets its click through at the lift, so the click still opens the menu', () => {
462+
expect(
463+
run([
464+
downOnTrigger('touch'),
465+
{type: 'withdraw'},
466+
up(null, {isInMenu: false, isOnTrigger: true}),
467+
]),
468+
).toMatchObject({
469+
gesture: {phase: 'idle'},
470+
last: {type: 'settle', stray: false, dismiss: false},
471+
});
472+
});
473+
474+
it('a mouse\u2019s gesture ends as a cancel, keeping its click (spec:AST-017 FR59)', () => {
475+
expect(run([downInMenu('A', 'mouse'), {type: 'withdraw'}])).toMatchObject({
476+
gesture: {phase: 'idle'},
477+
last: {type: 'settle', stray: false, dismiss: false},
478+
});
479+
});
480+
});
481+
405482
describe('menuPressStep, the released public machine', () => {
406483
it('keeps the settle rule when a caller\u2019s events carry positions: the opening finger picks the row it lifts on', () => {
407484
type Positioned = MenuPressEvent<Row> & {x: number; y: number};
@@ -434,7 +511,7 @@ describe('menuPressStep, the released public machine', () => {
434511
expect(menuPressStep(gesture, up).effect).toEqual({type: 'act', row: 'B'});
435512
});
436513

437-
it('keeps the released shapes: no handoff state', () => {
514+
it('keeps the released shapes: no handoff state, no settle clear', () => {
438515
let gesture = menuPressStep(IDLE_MENU_PRESS, {
439516
type: 'down',
440517
target: 'menu',

0 commit comments

Comments
 (0)