Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/menu-press-cancel-clear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[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.

@vjeux
16 changes: 16 additions & 0 deletions packages/core/src/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3480,6 +3480,22 @@ describe('DropdownMenu press model', () => {
}
});

it('presentation="popover": a blur during a finger\u2019s tap on the trigger still lets the tap\u2019s click open the menu', () => {
render(
<DropdownMenu button={{label: 'Pinned'}} presentation="popover">
<DropdownMenuItem label="Edit" />
</DropdownMenu>,
);
const trigger = screen.getByRole('button', {name: /Pinned/});
fireEvent.pointerDown(trigger, touch);
act(() => {
window.dispatchEvent(new FocusEvent('blur'));
});
fireEvent.pointerUp(trigger, touch);
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
});

it('presentation="popover" pins the settle rule under a finger: once the opening press has settled, a still lift picks the row under it', () => {
// A moved touch default comes back through
// `presentation`, never through a public threshold.
Expand Down
70 changes: 70 additions & 0 deletions packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {RadioList, RadioListItem} from '../RadioList';
import {COMPACT_TOUCH_PRESENTATION_QUERY} from '../hooks/useAdaptivePresentation';
import {Theme, defineTheme} from '../theme';
import {rtlStyles} from '../utils';
import {TOUCH_QUERY} from '../utils/touchPresentation';

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

describe('DropdownMenuSubMenu press model', () => {
const finger = (pointerId = 1) => ({pointerType: 'touch', pointerId});

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 () => {
const originalMatchMedia = window.matchMedia;
// A large screen, its primary pointer coarse (a tablet) or fine (a
// touchscreen laptop): the root is an anchored popover and its sub-menu
// flies out.
const stubPrimaryPointer = (coarse: boolean) =>
vi.stubGlobal(
'matchMedia',
vi.fn().mockImplementation((query: string) => ({
matches: coarse && query === TOUCH_QUERY,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
);
try {
const litAfterCancel = async (presentation?: 'popover') => {
const user = userEvent.setup();
const {unmount} = render(
<DropdownMenu button={{label: 'Actions'}} presentation={presentation}>
<DropdownMenuItem label="Rename" onClick={() => {}} />
<DropdownMenuSubMenu label="Move to">
<DropdownMenuItem label="Folder A" onClick={() => {}} />
<DropdownMenuItem label="Folder B" onClick={() => {}} />
</DropdownMenuSubMenu>
</DropdownMenu>,
);
await user.click(screen.getByRole('button', {name: /Actions/}));
const trigger = screen.getByRole('menuitem', {
name: /Move to/,
hidden: true,
});
fireEvent.pointerDown(trigger, finger());
fireEvent.pointerUp(trigger, finger());
expect(trigger).toHaveAttribute('aria-expanded', 'true');
const folderA = screen.getByRole('menuitem', {
name: 'Folder A',
hidden: true,
});
const folderB = screen.getByRole('menuitem', {
name: 'Folder B',
hidden: true,
});
// A finger lands in the flyout and slides onto Folder B, which lights;
// then the browser takes the touch (a scroll, a system gesture).
fireEvent.pointerDown(folderA, finger(2));
fireEvent.pointerMove(folderB, finger(2));
expect(folderB).toHaveFocus();
fireEvent.pointerCancel(folderB, finger(2));
const lit = document.activeElement === folderB;
unmount();
return lit;
};
for (const coarse of [true, false]) {
stubPrimaryPointer(coarse);
expect(await litAfterCancel(), `coarse=${coarse}`).toBe(false);
expect(await litAfterCancel('popover'), `coarse=${coarse}`).toBe(false);
}
} finally {
vi.stubGlobal('matchMedia', originalMatchMedia);
}
});

it('a press released on the sub-menu row opens it and keeps the menu', async () => {
const user = userEvent.setup();
render(<MoveMenu />);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

import {fireEvent, render, screen} from '@testing-library/react';
import {afterEach, describe, expect, it} from 'vitest';
import {__resetMenuPressForTest} from '../hooks/useMenuPress';
import {MenuBottomSheetActionList} from './MenuBottomSheetActionList';

afterEach(() => {
__resetMenuPressForTest();
});

const touch = (pointerId = 1) => ({pointerType: 'touch', pointerId});
const mouse = (pointerId = 1) => ({
pointerType: 'mouse',
pointerId,
button: 0,
});

function renderList() {
return render(
<MenuBottomSheetActionList
items={[
{label: 'Rename', onClick: () => {}},
{label: 'Delete', onClick: () => {}},
]}
onSelect={() => {}}
onOpenSubmenu={() => {}}
/>,
);
}

describe('MenuBottomSheetActionList', () => {
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)', () => {
const {unmount} = renderList();
fireEvent.pointerDown(
screen.getByRole('button', {name: 'Rename'}),
touch(),
);
fireEvent.pointerMove(
screen.getByRole('button', {name: 'Delete'}),
touch(),
);
expect(screen.getByRole('button', {name: 'Delete'})).toHaveFocus();
// The browser takes the touch: a scroll of the sheet, say.
fireEvent.pointerCancel(
screen.getByRole('button', {name: 'Delete'}),
touch(),
);
expect(screen.getByRole('button', {name: 'Delete'})).not.toHaveFocus();
unmount();

renderList();
fireEvent.pointerDown(
screen.getByRole('button', {name: 'Rename'}),
mouse(),
);
fireEvent.pointerMove(
screen.getByRole('button', {name: 'Delete'}),
mouse(),
);
fireEvent.pointerCancel(
screen.getByRole('button', {name: 'Delete'}),
mouse(),
);
expect(screen.getByRole('button', {name: 'Delete'})).toHaveFocus();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ export function MenuBottomSheetActionList({
<List
ref={listComposedRef}
density="spacious"
// Focus parks on the list when no row is lit, as in DropdownMenu's own
// menu, so a row a cancelled finger lit loses its highlight.
tabIndex={-1}
xstyle={styles.list}
{...menuPress.menuProps}>
{items.map((option, index) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
* @file MenuPress.a11y.chromium.spec.ts
* @input Built DropdownMenu and Selector stories and real Chromium mouse,
* touch (CDP) and pen (CDP) input
* @output Browser evidence for the menu press model: the row under the
* release acts once (a link row included: no native link drag cancels a
* mouse press), the highlight follows the pointer, a release outside
* closes under a mouse and not under a finger, a touch pan in an
* overflowing menu cancels the gesture, a pen behaves like a finger, and
* Escape after a press-open returns focus to the trigger
* @output Browser evidence for the menu press model. The row under the
* release acts once, a link row included: no native link drag cancels a
* mouse press. The highlight follows the pointer. A release outside closes
* under a mouse and not under a finger. A touch pan in an overflowing menu
* cancels the gesture. A cancelled touch and the window losing focus end a
* finger's gesture with nothing acting and its lit row cleared. A pen
* behaves like a finger, and Escape after a press-open returns focus to
* the trigger.
* @position Real-engine proof for module:DropdownMenu/useMenuPress — the
* paths jsdom cannot exercise (hit testing, touch synthesis, scrolling)
*/
Expand Down Expand Up @@ -373,6 +375,77 @@ test.describe('DropdownMenu press model (Chromium)', () => {
expect(activations).toEqual([]);
});

test('touch: a cancelled touch ends the gesture: nothing acts and the lit row clears (spec:AST-094 FR28)', async ({
page,
}) => {
const activations = collectActivations(page);
const cdp = await enableTouch(page);
await mount(page, MENU_STORY);
await touch.tap(cdp, await center(page, TRIGGER));
const menu = page.getByRole('menu');
await expect(menu).toBeVisible();
// Measure the rows once the menu has landed: under a finger it may
// still be growing out of its trigger.
await expect
.poll(async () =>
menu.evaluate(
element =>
(element.closest('[popover]') ?? element)
.getAnimations({subtree: true})
.filter(animation => animation.playState === 'running').length,
),
)
.toBe(0);
const edit = await center(page, 'menuitem', 'Edit');
await touch.start(cdp, edit);
await expect.poll(async () => activeRowName(page)).toBe('Edit');
// The browser takes the touch.
await cdp.send('Input.dispatchTouchEvent', {
type: 'touchCancel',
touchPoints: [],
});
await expect.poll(async () => activeRowName(page)).toBeNull();
await page.waitForTimeout(300);
await expect(menu).toBeVisible();
expect(activations).toEqual([]);
});

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 ({
page,
}) => {
const activations = collectActivations(page);
const cdp = await enableTouch(page);
await mount(page, MENU_STORY);
await touch.tap(cdp, await center(page, TRIGGER));
const menu = page.getByRole('menu');
await expect(menu).toBeVisible();
// Measure the rows once the menu has landed: under a finger it may
// still be growing out of its trigger.
await expect
.poll(async () =>
menu.evaluate(
element =>
(element.closest('[popover]') ?? element)
.getAnimations({subtree: true})
.filter(animation => animation.playState === 'running').length,
),
)
.toBe(0);
const edit = await center(page, 'menuitem', 'Edit');
await touch.start(cdp, edit);
await expect.poll(async () => activeRowName(page)).toBe('Edit');
// The window loses focus with the finger down (a headless page keeps
// focus, so the blur is dispatched to the window).
await page.evaluate(() => {
window.dispatchEvent(new FocusEvent('blur'));
});
await expect.poll(async () => activeRowName(page)).toBeNull();
await touch.end(cdp);
await page.waitForTimeout(300);
await expect(menu).toBeVisible();
expect(activations).toEqual([]);
});

test('touch: a menu a held finger opens stays open when that finger lifts on the trigger', async ({
page,
}) => {
Expand Down
81 changes: 79 additions & 2 deletions packages/core/src/hooks/menuPressGesture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,24 @@ describe('menuPressStep — inside the menu', () => {
expect(last).toEqual({type: 'settle', stray: true, dismiss: false});
});

it('Tracking → Idle: a cancel (scroll, second pointer, browser) acts on nothing', () => {
it('Tracking → Idle: a cancel (scroll, second pointer, browser) acts on nothing and clears the lit row', () => {
const {gesture, last} = run([downInMenu('A'), move('B'), {type: 'cancel'}]);
expect(gesture.phase).toBe('idle');
expect(last).toEqual({
type: 'settle',
stray: false,
dismiss: false,
clear: true,
});
});

it('Tracking → Idle: a cancelled MOUSE press keeps its lit row', () => {
const {last} = run([downInMenu('A', 'mouse'), move('B'), {type: 'cancel'}]);
expect(last).toEqual({type: 'settle', stray: false, dismiss: false});
});

it('Tracking → Idle: a cancel with nothing lit has nothing to clear', () => {
const {last} = run([downInMenu('A'), move(null), {type: 'cancel'}]);
expect(last).toEqual({type: 'settle', stray: false, dismiss: false});
});

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

describe('menuPressStep — the window loses focus mid-press (spec:AST-094 FR28)', () => {
it('a finger\u2019s gesture is withdrawn, not ended: its lit row clears, and nothing it does highlights', () => {
const {gesture, effects} = run([
downInMenu('A'),
{type: 'withdraw'},
move('B'),
]);
expect(gesture).toEqual({phase: 'withdrawn', pointerType: 'touch'});
expect(effects.slice(1)).toEqual([
{type: 'withdraw', clear: true},
{type: 'none'},
]);
});

it('the withdrawn finger\u2019s release acts on nothing and swallows its click; a cancel ends it quietly', () => {
expect(run([downInMenu('A'), {type: 'withdraw'}, up('B')])).toMatchObject({
gesture: {phase: 'idle'},
last: {type: 'settle', stray: true, dismiss: false},
});
expect(
run([downInMenu('A'), {type: 'withdraw'}, {type: 'cancel'}]),
).toMatchObject({
gesture: {phase: 'idle'},
last: {type: 'settle', stray: false, dismiss: false},
});
});

it('a finger holding the trigger, or one that opened the menu, is withdrawn with nothing to clear', () => {
expect(run([downOnTrigger('touch'), {type: 'withdraw'}])).toMatchObject({
gesture: {phase: 'withdrawn'},
last: {type: 'withdraw'},
});
expect(
run([
downOnTrigger('pen'),
{type: 'opened', time: 500},
{type: 'withdraw'},
]),
).toMatchObject({gesture: {phase: 'withdrawn'}, last: {type: 'withdraw'}});
});

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', () => {
expect(
run([
downOnTrigger('touch'),
{type: 'withdraw'},
up(null, {isInMenu: false, isOnTrigger: true}),
]),
).toMatchObject({
gesture: {phase: 'idle'},
last: {type: 'settle', stray: false, dismiss: false},
});
});

it('a mouse\u2019s gesture ends as a cancel, keeping its click (spec:AST-017 FR59)', () => {
expect(run([downInMenu('A', 'mouse'), {type: 'withdraw'}])).toMatchObject({
gesture: {phase: 'idle'},
last: {type: 'settle', stray: false, dismiss: false},
});
});
});

describe('menuPressStep, the released public machine', () => {
it('keeps the settle rule when a caller\u2019s events carry positions: the opening finger picks the row it lifts on', () => {
type Positioned = MenuPressEvent<Row> & {x: number; y: number};
Expand Down Expand Up @@ -434,7 +511,7 @@ describe('menuPressStep, the released public machine', () => {
expect(menuPressStep(gesture, up).effect).toEqual({type: 'act', row: 'B'});
});

it('keeps the released shapes: no handoff state', () => {
it('keeps the released shapes: no handoff state, no settle clear', () => {
let gesture = menuPressStep(IDLE_MENU_PRESS, {
type: 'down',
target: 'menu',
Expand Down
Loading
Loading