Skip to content

Commit 71a6e06

Browse files
committed
feat(useMenuPress): a pointer that went down elsewhere is the menu's opening press
The internal press model gains continueGesture: a pointer that went down elsewhere (a context menu's long press, a trigger the hook does not own) continues into the menu that just opened under it, tracked as the opening press. With the 'uikit' touch model, a finger on every device picks only after the handoff distance from the first position the menu sees; otherwise, and for a mouse, the opening press follows the settle rule. A menu with no trigger takes the touch model through useMenuSurfacePress, so the finger it is handed keeps the handoff. No component hands a pointer over yet. - useMenuPress: continueGesture on the internal return, and the touch model on useMenuSurfacePress. - Tests: the hook's continuation cases, and a real-browser spec with its fixture and story (MenuPressContinuation). - A [feature] changeset.
1 parent 4ad7e96 commit 71a6e06

8 files changed

Lines changed: 573 additions & 12 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+
[chore] Internal: the menu press model takes a pointer that went down elsewhere, such as a context menu's long press, as the menu's opening press (`module:DropdownMenu/useMenuPress` FR11), and its trigger-less entry takes the touch model, so under the `'uikit'` touch model a finger handed to the menu, on every device, picks only past UIMenu's handoff distance. Not part of the public hooks API.
6+
7+
@vjeux
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file MenuPressContinuationA11y.stories.tsx
5+
* @input The menu-press continuation fixture from core's hook tests
6+
* @output The fixture that the menu-press continuation interaction spec
7+
* drives in a real browser. A button's finger long press opens a menu and
8+
* hands it the held finger. Each picked row prints outside the menu.
9+
* Excluded from the visual matrix.
10+
* @position The reproduction path for
11+
* `packages/core/src/hooks/__tests__/MenuPressContinuation.interaction.browser.spec.ts`.
12+
*
13+
* SYNC: The spec navigates to this story id by name.
14+
*/
15+
16+
import type {Meta, StoryObj} from '@storybook/react';
17+
import {ContinuedPressFixture} from '../../../packages/core/src/hooks/__tests__/menuPressContinuation.fixture';
18+
19+
const meta: Meta = {
20+
title: 'a11y/Menu press continuation',
21+
tags: ['no-visual'],
22+
};
23+
24+
export default meta;
25+
26+
export const ContinuedPress: StoryObj = {
27+
render: () => <ContinuedPressFixture />,
28+
};
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file MenuPressContinuation.interaction.browser.spec.ts
5+
* @input The built `a11y/Menu press continuation` story, real touch input
6+
* (Chromium CDP / WebKit's input command), the interaction harness, and the
7+
* menu press's handoff distance from the UIKit constants table
8+
* @output Interaction proofs for a pointer handed to a menu as it opens
9+
* (`module:DropdownMenu/useMenuPress` FR11), under a finger (the same on
10+
* every device, `spec:AST-094` FR43): held on a button until the menu opens
11+
* under it, the finger picks the row it lifts on only once it has travelled the
12+
* handoff distance from the first position the menu sees; a lift before
13+
* that acts on nothing and leaves the menu open
14+
* @position The real-browser case FR11's verification names, beside the
15+
* continuation suite in useMenuPress.test.tsx; the fixture is
16+
* menuPressContinuation.fixture.tsx
17+
*/
18+
19+
import {expect, test, type Page} from '@playwright/test';
20+
import {
21+
frames,
22+
openStory,
23+
touchDriver,
24+
withStorybook,
25+
type TouchDriver,
26+
type TouchFrame,
27+
} from '@astryxdesign/interaction-harness';
28+
import {UIKIT_MENU_PRESS} from '../../motion/uikit/gestures';
29+
import {
30+
CONTINUED_PRESS_HOLD_MS,
31+
CONTINUED_PRESS_ROW_PX,
32+
} from './menuPressContinuation.geometry';
33+
34+
const storybook = withStorybook(test);
35+
36+
interface OpenFixture {
37+
driver: TouchDriver;
38+
/** The button's center, where the finger lands. */
39+
center: {x: number; y: number};
40+
/** The center of a row of the menu that opens under the button. */
41+
rowCenter: (index: number) => {x: number; y: number};
42+
}
43+
44+
async function openFixture(page: Page): Promise<OpenFixture> {
45+
await openStory(
46+
page,
47+
storybook(),
48+
'a11y-menu-press-continuation--continued-press',
49+
);
50+
const driver = await touchDriver(page);
51+
const box = await page.getByRole('button', {name: 'Hold me'}).boundingBox();
52+
if (box == null) {
53+
throw new Error('no button');
54+
}
55+
const center = {x: box.x + box.width / 2, y: box.y + box.height / 2};
56+
// The menu renders right below the button, one 44 px row after another.
57+
const rowCenter = (index: number) => ({
58+
x: center.x,
59+
y: box.y + box.height + CONTINUED_PRESS_ROW_PX * (index + 0.5),
60+
});
61+
return {driver, center, rowCenter};
62+
}
63+
64+
const picked = (page: Page) => page.getByTestId('picked');
65+
66+
/** One finger at `point` at time `t`; no point lifts it. */
67+
const at = (t: number, point?: {x: number; y: number}): TouchFrame => ({
68+
t,
69+
fingers: point == null ? [] : [{id: 0, ...point}],
70+
});
71+
72+
/** When the finger first moves: after the menu has opened under it. */
73+
const FIRST_MOVE_MS = CONTINUED_PRESS_HOLD_MS + 250;
74+
75+
test('a finger handed to the menu as it opens picks the row it lifts on, past the handoff distance (module FR11)', async ({
76+
page,
77+
}) => {
78+
const fixture = await openFixture(page);
79+
// Held until the menu opens under it; the first position the menu sees is
80+
// over the first row, and the finger then slides a whole row, past the
81+
// handoff distance, onto the second and lifts there.
82+
const alpha = fixture.rowCenter(0);
83+
const bravo = fixture.rowCenter(1);
84+
await fixture.driver.play([
85+
at(0, fixture.center),
86+
at(FIRST_MOVE_MS, alpha),
87+
at(FIRST_MOVE_MS + 50, {x: alpha.x, y: (alpha.y + bravo.y) / 2}),
88+
at(FIRST_MOVE_MS + 100, bravo),
89+
at(FIRST_MOVE_MS + 160, bravo),
90+
at(FIRST_MOVE_MS + 220),
91+
]);
92+
await expect(picked(page)).toHaveText('Bravo');
93+
await expect(page.getByRole('menu')).toHaveCount(0);
94+
});
95+
96+
test('a lift before the handoff distance acts on nothing and leaves the menu open', async ({
97+
page,
98+
}) => {
99+
const fixture = await openFixture(page);
100+
const handoff = UIKIT_MENU_PRESS.handoff.value;
101+
// Held until the menu opens; the first position the menu sees is already
102+
// over the first row, and the finger moves less than the handoff distance
103+
// there before it lifts. Under the settle rule this lift would pick the row.
104+
const alpha = fixture.rowCenter(0);
105+
await fixture.driver.play([
106+
at(0, fixture.center),
107+
at(FIRST_MOVE_MS, alpha),
108+
at(FIRST_MOVE_MS + 50, {x: alpha.x, y: alpha.y + handoff / 2}),
109+
at(FIRST_MOVE_MS + 160, {x: alpha.x, y: alpha.y + handoff / 2}),
110+
at(FIRST_MOVE_MS + 220),
111+
]);
112+
await frames(page, 2);
113+
await expect(page.getByRole('menu')).toBeVisible();
114+
await expect(picked(page)).toHaveText('');
115+
});
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
'use client';
4+
5+
/**
6+
* @file menuPressContinuation.fixture.tsx
7+
* @input useMenuSurfacePress (its continueGesture)
8+
* @output ContinuedPressFixture: a plain button whose finger long press opens
9+
* a list menu under the held finger and hands that finger to the menu's
10+
* press model (`module:DropdownMenu/useMenuPress` FR11), with each picked
11+
* row printed outside the menu
12+
* @position The fixture the menu-press continuation interaction spec drives
13+
* (hooks/__tests__/MenuPressContinuation.interaction.browser.spec.ts),
14+
* through apps/storybook/stories/MenuPressContinuationA11y.stories.tsx.
15+
* No component hands a pointer over yet; this stands in for the context
16+
* menu's long press FR11 names.
17+
*
18+
* SYNC: The spec aims at rows from menuPressContinuation.geometry.ts.
19+
*/
20+
21+
import {useCallback, useEffect, useRef, useState} from 'react';
22+
import {useMenuSurfacePress, type MenuPressContinuation} from '../useMenuPress';
23+
import {
24+
CONTINUED_PRESS_HOLD_MS,
25+
CONTINUED_PRESS_ROWS,
26+
CONTINUED_PRESS_ROW_PX,
27+
} from './menuPressContinuation.geometry';
28+
29+
export function ContinuedPressFixture() {
30+
const menuRef = useRef<HTMLDivElement | null>(null);
31+
const [isOpen, setIsOpen] = useState(false);
32+
const [picked, setPicked] = useState<ReadonlyArray<string>>([]);
33+
// The touch model the press model needs for FR11's handoff: 'uikit', as
34+
// DropdownMenu's default presentation runs it for a finger on every device
35+
// (`spec:AST-094` FR43).
36+
const press = useMenuSurfacePress({
37+
menuRef,
38+
itemSelector: '[role="menuitem"]',
39+
onDismiss: () => setIsOpen(false),
40+
touchModel: 'uikit',
41+
});
42+
const holdTimerRef = useRef<number | null>(null);
43+
const handOffRef = useRef<MenuPressContinuation | null>(null);
44+
const continueGesture = press.continueGesture;
45+
46+
// Hands the held pointer to the menu as its element attaches, so the press
47+
// model listens in the menu's document.
48+
const attachMenu = useCallback(
49+
(node: HTMLDivElement | null) => {
50+
menuRef.current = node;
51+
if (node != null && handOffRef.current != null) {
52+
continueGesture(handOffRef.current);
53+
handOffRef.current = null;
54+
}
55+
},
56+
[continueGesture],
57+
);
58+
59+
const clearHold = () => {
60+
if (holdTimerRef.current != null) {
61+
window.clearTimeout(holdTimerRef.current);
62+
holdTimerRef.current = null;
63+
}
64+
};
65+
// Clears a pending hold on unmount.
66+
useEffect(() => clearHold, []);
67+
68+
return (
69+
<div style={{padding: 24, width: 240}}>
70+
<button
71+
type="button"
72+
style={{
73+
display: 'block',
74+
width: 200,
75+
height: CONTINUED_PRESS_ROW_PX,
76+
touchAction: 'none',
77+
userSelect: 'none',
78+
WebkitUserSelect: 'none',
79+
WebkitTouchCallout: 'none',
80+
}}
81+
onContextMenu={event => event.preventDefault()}
82+
onPointerDown={event => {
83+
if (event.pointerType === 'mouse' || isOpen) {
84+
return;
85+
}
86+
const {pointerId} = event;
87+
const pointerType = event.pointerType === 'pen' ? 'pen' : 'touch';
88+
clearHold();
89+
holdTimerRef.current = window.setTimeout(() => {
90+
holdTimerRef.current = null;
91+
handOffRef.current = {pointerId, pointerType, time: Date.now()};
92+
setIsOpen(true);
93+
}, CONTINUED_PRESS_HOLD_MS);
94+
}}
95+
onPointerUp={clearHold}
96+
onPointerCancel={clearHold}>
97+
Hold me
98+
</button>
99+
{isOpen ? (
100+
<div
101+
ref={attachMenu}
102+
role="menu"
103+
aria-label="Actions"
104+
tabIndex={-1}
105+
{...press.menuProps}
106+
style={{display: 'flex', flexDirection: 'column', width: 200}}>
107+
{CONTINUED_PRESS_ROWS.map(label => (
108+
<div
109+
key={label}
110+
role="menuitem"
111+
tabIndex={-1}
112+
style={{
113+
height: CONTINUED_PRESS_ROW_PX,
114+
boxSizing: 'border-box',
115+
padding: 12,
116+
}}
117+
onClick={() => {
118+
setPicked(previous => [...previous, label]);
119+
setIsOpen(false);
120+
}}>
121+
{label}
122+
</div>
123+
))}
124+
</div>
125+
) : null}
126+
<output data-testid="picked">{picked.join(',')}</output>
127+
</div>
128+
);
129+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file menuPressContinuation.geometry.ts
5+
* @input Nothing; data only
6+
* @output The menu-press continuation fixture's timing and layout: the hold
7+
* before the menu opens, the rows' height and labels
8+
* @position Shared by menuPressContinuation.fixture.tsx and the interaction
9+
* spec, which aims at row centers from it without loading the fixture's
10+
* component code
11+
*/
12+
13+
/** How long the finger rests on the button before the menu opens, ms. */
14+
export const CONTINUED_PRESS_HOLD_MS = 500;
15+
/** The button's and each row's height, px. */
16+
export const CONTINUED_PRESS_ROW_PX = 44;
17+
export const CONTINUED_PRESS_ROWS: ReadonlyArray<string> = [
18+
'Alpha',
19+
'Bravo',
20+
'Charlie',
21+
];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import {MENU_PRESS_HANDOFF_PX as UIKIT_MENU_PRESS_HANDOFF_PX} from './menuPressValues';
1717

18-
/** How long a mouse's opening press must last before its release may act. */
18+
/** How long an opening press with no handoff must last before its release may act. */
1919
export const MENU_PRESS_SETTLE_MS = 300;
2020

2121
/**

0 commit comments

Comments
 (0)