Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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-public-default.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feature] `useMenuPress` with no `longPressDelayMs` now follows DropdownMenu's finger rules on every device (`spec:AST-094` FR25, FR29, FR43): a finger or a pen opens the menu as it presses (inside a scroll view, once the 150 ms content-touch delay has passed), and the finger that opened it picks a row only after moving 10 px. To keep the previous behavior, a 500 ms hold and a pick at the opening finger's lift, pass `longPressDelayMs: 500`.

@vjeux
7 changes: 7 additions & 0 deletions .changeset/menu-press-uimenu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[feature] Under a finger or a pen, on every device, a DropdownMenu or MoreMenu trigger now opens its menu as it is pressed, instead of after a 500 ms hold (`spec:AST-094` FR29, FR43). Inside a scroll view it opens once the 150 ms content-touch delay has passed with no scroll, so a scroll that starts on it still scrolls. To keep the previous behavior, a tap that opens at the lift and a 500 ms hold, pass `presentation="popover"`. A mouse is unchanged.
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved

@vjeux
71 changes: 35 additions & 36 deletions docs/specs/components/DropdownMenu/DropdownMenu.spec.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/core/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -6085,7 +6085,7 @@ interface UseMenuPressOptions
hitTest?: (x: number, y: number) => Element | null
isEnabled?: boolean = true
itemSelector: string
longPressDelayMs?: number = 500
longPressDelayMs?: number
menuRef: RefObject<HTMLElement | null>
onActivate?: (row: HTMLElement, release: PointerEvent) => void
onDismiss?: () => void
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Breadcrumbs/BreadcrumbItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import {focusOutlineProps} from '../utils/focusOutline.stylex';
import {Icon} from '../Icon';
import {usePopover} from '../Popover/usePopover';
import {useListFocus} from '../hooks/useListFocus';
import {useMenuPress} from '../hooks/useMenuPress';
import {useMenuSurfacePress} from '../hooks/useMenuPress';
import {pressOnItemsOnly} from '../hooks/menuPressOnItems';
import {menuTouchActionStyles} from '../DropdownMenu/menuTouchAction.stylex';
import {useMenuOverflow} from '../DropdownMenu/useMenuOverflow';
Expand Down Expand Up @@ -585,7 +585,7 @@ function BreadcrumbMenuTrigger({
// A press that slides across the menu's items acts on the item it lifts on,
// the highlight following the pointer, as DropdownMenu's does
// (`family:action-groups` FR1-FR3, FR5); a mouse released outside closes it.
const menuPress = useMenuPress({
const menuPress = useMenuSurfacePress({
menuRef: listRef,
itemSelector: MENU_ITEM_SELECTOR,
onDismiss: closeMenu,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/ContextMenu/ContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import {
import {useMenuOverflow} from '../DropdownMenu/useMenuOverflow';
import {useMenuDrillIn} from '../DropdownMenu/useMenuDrillIn';
import {useInternalListFocus} from '../hooks/useListFocus';
import {useMenuPress} from '../hooks/useMenuPress';
import {useMenuSurfacePress} from '../hooks/useMenuPress';
import {useTypeahead} from '../hooks/useTypeahead';
import {useLongPress} from '../hooks/useLongPress';
import {layerAnimations} from '../Layer/layerAnimations.stylex';
Expand Down Expand Up @@ -768,7 +768,7 @@ export function ContextMenu({
// The press model: the row under a release acts, the highlight follows a
// held pointer, a mouse released outside dismisses and a finger leaves the
// menu open.
const menuPress = useMenuPress({
const menuPress = useMenuSurfacePress({
menuRef: listRef,
itemSelector: MENU_ITEM_SELECTOR,
onDismiss: closeMenu,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/DropdownMenu/DropdownMenu.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export const docs = {
name: 'renderTrigger',
type: '(props: DropdownMenuTriggerProps) => ReactNode',
description:
'Render the control the menu hangs off — an IconButton, a chip, an avatar, a list row — instead of the built-in Button. Spread the given props onto it: they carry the press model (a mouse opens on press-down, a held finger opens with the finger down), the keyboard opens, the toggle click, and `aria-haspopup`/`aria-expanded`/`aria-controls`/`id`; the menu is then named by that control through `aria-labelledby`. Mutually exclusive with `button`.',
'Render the control the menu hangs off — an IconButton, a chip, an avatar, a list row — instead of the built-in Button. Spread the given props onto it: they carry the press model (a mouse opens on press-down, a finger or a pen opens as it presses), the keyboard opens, the toggle click, and `aria-haspopup`/`aria-expanded`/`aria-controls`/`id`; the menu is then named by that control through `aria-labelledby`. Mutually exclusive with `button`.',
},

{
Expand All @@ -201,7 +201,7 @@ export const docs = {
name: 'presentation',
type: "'popover' | 'bottom-sheet' | 'adaptive'",
description:
"Presentation surface for data-driven items. 'popover' stays anchored, 'bottom-sheet' always renders the actions in a modal BottomSheet, and 'adaptive' uses a BottomSheet on compact coarse-pointer layouts while remaining anchored elsewhere. Compound children currently support popover only. Omitted, the menu is anchored, and under a finger, on every device, the finger that opened it picks a row only after moving 10 px. Pass 'popover' to keep the pointer behavior on every device, the settle rule included.",
"Presentation surface for data-driven items. 'popover' stays anchored, 'bottom-sheet' always renders the actions in a modal BottomSheet, and 'adaptive' uses a BottomSheet on compact coarse-pointer layouts while remaining anchored elsewhere. Compound children currently support popover only. Omitted, the menu is anchored, and under a finger or a pen, on every device, its trigger opens as it is pressed (inside a scroll view, after the 150 ms content-touch delay), and the opening finger picks after moving 10 px. Pass 'popover' to keep the pointer behavior on every device, a 500 ms hold included.",
},
{
name: 'isMenuOpen',
Expand Down
228 changes: 216 additions & 12 deletions packages/core/src/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3379,27 +3379,30 @@ describe('DropdownMenu press model', () => {
expect(HTMLElement.prototype.showPopover).toHaveBeenCalledTimes(1);
});

it('a held touch on the trigger opens with the finger down, and a slide past the handoff picks (spec:AST-094 FR29, FR43)', () => {
it('under a coarse pointer a finger opens the menu as it presses the trigger (no scroll view), and a slide picks', () => {
stubCoarsePointer();
vi.useFakeTimers();
try {
const onPick = vi.fn();
const trigger = renderMenu(onPick);
// A trigger in no scroll view opens on the press.
fireEvent.pointerDown(trigger, {...touch, clientX: 0, clientY: 0});
expect(trigger).toHaveAttribute('aria-expanded', 'false');
act(() => {
vi.advanceTimersByTime(500);
});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
// The handoff counts from the first position the open menu sees.
// UIMenu's handoff: the finger picks once it travels from the first
// sample the menu saw.
fireEvent.pointerMove(item('Delete'), {...touch, clientX: 0, clientY: 3});
const past = {
expect(item('Delete')).not.toHaveFocus();
fireEvent.pointerMove(item('Delete'), {
...touch,
clientX: 0,
clientY: 3 + UIKIT_GESTURES.menuPress.handoff.value,
};
fireEvent.pointerMove(item('Delete'), past);
});
expect(item('Delete')).toHaveFocus();
fireEvent.pointerUp(item('Delete'), past);
fireEvent.pointerUp(item('Delete'), {
...touch,
clientX: 0,
clientY: 3 + UIKIT_GESTURES.menuPress.handoff.value,
});
expect(onPick).toHaveBeenCalledWith('Delete');
// The click the browser aims at the trigger for this gesture is spent.
fireEvent.click(trigger, {detail: 1});
Expand Down Expand Up @@ -3478,7 +3481,7 @@ describe('DropdownMenu press model', () => {
});

it('presentation="popover" pins the settle rule under a finger: once the opening press has settled, a still lift picks the row under it', () => {
// spec:AST-094 FR25: a moved touch default comes back through
// A moved touch default comes back through
// `presentation`, never through a public threshold.
stubCoarsePointer();
vi.useFakeTimers();
Expand All @@ -3504,6 +3507,37 @@ describe('DropdownMenu press model', () => {
}
});

it('presentation="popover" pins the released trigger under a finger: it opens at 500 ms, not on the press', () => {
// A moved touch default comes back through
// `presentation`, never through a public threshold.
stubCoarsePointer();
vi.useFakeTimers();
try {
const onPick = vi.fn<(label: string) => void>();
render(
<DropdownMenu button={{label: 'Pinned'}} presentation="popover">
<DropdownMenuItem label="Edit" onClick={() => onPick('Edit')} />
<DropdownMenuItem label="Delete" onClick={() => onPick('Delete')} />
</DropdownMenu>,
);
const trigger = screen.getByRole('button', {name: /Pinned/});
fireEvent.pointerDown(trigger, {...touch, clientX: 0, clientY: 0});
// Not on the press, as the touch modes would open it.
expect(trigger).toHaveAttribute('aria-expanded', 'false');
act(() => {
vi.advanceTimersByTime(MENU_PRESS_LONG_PRESS_MS - 1);
});
expect(trigger).toHaveAttribute('aria-expanded', 'false');
act(() => {
vi.advanceTimersByTime(1);
});
expect(MENU_PRESS_LONG_PRESS_MS).toBe(500);
expect(trigger).toHaveAttribute('aria-expanded', 'true');
} finally {
vi.useRealTimers();
}
});

it('a menu a held finger opens keeps its trigger as the invoker until the finger lifts', () => {
// A release on the trigger is outside the `popover="auto"` menu, so the
// browser's light dismiss would close it; holding the invoker through
Expand Down Expand Up @@ -3630,13 +3664,183 @@ describe('DropdownMenu press model', () => {
});
});

it('a tap on the trigger still opens through its click', () => {
it('a tap on the trigger opens the menu on the press, and its click does not close it', () => {
const trigger = renderMenu();
fireEvent.pointerDown(trigger, touch);
expect(trigger).toHaveAttribute('aria-expanded', 'true');
fireEvent.pointerUp(trigger, touch);
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
});

// A finger follows the menus' finger rules on every device: a touchscreen
// laptop (a fine primary pointer) as on a phone. Each gesture is still read
// as a finger or a mouse. `presentation="popover"` keeps the released
// finger behavior.
describe('a finger follows the same rules on every device; each gesture is still a finger or a mouse', () => {
function stubFinePointer() {
vi.stubGlobal(
'matchMedia',
vi.fn().mockImplementation((query: string) => ({
matches: false,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
);
expect(window.matchMedia(TOUCH_QUERY).matches).toBe(false);
}
const touchmoveIsPrevented = () => {
const event = new Event('touchmove', {bubbles: true, cancelable: true});
document.body.dispatchEvent(event);
return event.defaultPrevented;
};

it('fine primary pointer: a finger opens the menu as it presses the trigger, as on a phone (spec:AST-094 FR29, FR43)', () => {
stubFinePointer();
const trigger = renderMenu();
fireEvent.pointerDown(trigger, {...touch, clientX: 0, clientY: 0});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
fireEvent.pointerUp(trigger, {...touch, clientX: 0, clientY: 0});
// The tap's own click comes after the lift; it must not toggle.
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
});

it('fine primary pointer: a pen opens the menu as it presses the trigger, as a finger does (spec:AST-094 FR29, FR43; spec:AST-017 FR59)', () => {
stubFinePointer();
const trigger = renderMenu();
const pen = {pointerType: 'pen', pointerId: 7, clientX: 0, clientY: 0};
fireEvent.pointerDown(trigger, pen);
expect(trigger).toHaveAttribute('aria-expanded', 'true');
fireEvent.pointerUp(trigger, pen);
// The pen's own click comes after the lift; it must not toggle.
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
});

it('fine primary pointer, presentation="popover": a finger keeps the released behavior, its press opening nothing and its tap opening the menu through the click (spec:AST-094 FR25)', () => {
stubFinePointer();
render(
<DropdownMenu button={{label: 'Pinned'}} presentation="popover">
<DropdownMenuItem label="Edit" />
<DropdownMenuItem label="Delete" />
</DropdownMenu>,
);
const trigger = screen.getByRole('button', {name: /Pinned/});
fireEvent.pointerDown(trigger, {...touch, clientX: 0, clientY: 0});
expect(trigger).toHaveAttribute('aria-expanded', 'false');
fireEvent.pointerUp(trigger, {...touch, clientX: 0, clientY: 0});
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
});

it('fine primary pointer: the finger that opened the menu picks only once it travels the handoff, as on a phone (spec:AST-094 FR29, FR43)', () => {
stubFinePointer();
const onPick = vi.fn<(label: string) => void>();
const trigger = renderMenu(onPick);
fireEvent.pointerDown(trigger, {...touch, clientX: 0, clientY: 0});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
// The finger's later touchmoves are the menu's, not a scroll.
expect(touchmoveIsPrevented()).toBe(true);
// A finger that barely moved lights nothing, however long it rests.
fireEvent.pointerMove(item('Delete'), {...touch, clientX: 0, clientY: 3});
expect(item('Delete')).not.toHaveFocus();
// UIMenu's handoff: the finger picks once it travels from the first
// sample the menu saw.
const handoff = 3 + UIKIT_GESTURES.menuPress.handoff.value;
fireEvent.pointerMove(item('Delete'), {
...touch,
clientX: 0,
clientY: handoff,
});
expect(item('Delete')).toHaveFocus();
fireEvent.pointerUp(item('Delete'), {
...touch,
clientX: 0,
clientY: handoff,
});
expect(onPick).toHaveBeenCalledWith('Delete');
expect(touchmoveIsPrevented()).toBe(false);
});

it('fine primary pointer: a finger that lifts outside the menu leaves it open; a mouse released outside closes it', async () => {
stubFinePointer();
const user = userEvent.setup();
const trigger = renderMenu();
await user.click(trigger);
fireEvent.pointerDown(item('Edit'), touch);
fireEvent.pointerUp(document.body, touch);
expect(trigger).toHaveAttribute('aria-expanded', 'true');
fireEvent.pointerDown(item('Edit'), mouse);
fireEvent.pointerUp(document.body, mouse);
expect(trigger).toHaveAttribute('aria-expanded', 'false');
});

function renderCountingMenu() {
const onOpenChange = vi.fn<(isOpen: boolean) => void>();
const onPick = vi.fn<(label: string) => void>();
render(
<DropdownMenu button={{label: 'Actions'}} onOpenChange={onOpenChange}>
<DropdownMenuItem label="Edit" onClick={() => onPick('Edit')} />
<DropdownMenuItem label="Delete" onClick={() => onPick('Delete')} />
</DropdownMenu>,
);
return {
trigger: screen.getByRole('button', {name: /Actions/}),
onOpenChange,
onPick,
};
}

it('coarse primary pointer: a finger opens the menu once, as it presses, and it closes once at a pick (module AR5)', () => {
stubCoarsePointer();
const {trigger, onOpenChange, onPick} = renderCountingMenu();
fireEvent.pointerDown(trigger, {...touch, clientX: 0, clientY: 0});
fireEvent.pointerUp(trigger, {...touch, clientX: 0, clientY: 0});
// The tap's own click comes after the lift; it must not toggle.
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
expect(onOpenChange.mock.calls).toEqual([[true]]);
fireEvent.pointerDown(item('Delete'), touch);
fireEvent.pointerUp(item('Delete'), touch);
expect(onPick).toHaveBeenCalledWith('Delete');
expect(onOpenChange.mock.calls).toEqual([[true], [false]]);
});

it('fine primary pointer: a finger opens the menu once, as it presses, and it closes once at a pick, as on a phone (module AR5, spec:AST-094 FR43)', () => {
stubFinePointer();
const {trigger, onOpenChange, onPick} = renderCountingMenu();
fireEvent.pointerDown(trigger, {...touch, clientX: 0, clientY: 0});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
fireEvent.pointerUp(trigger, {...touch, clientX: 0, clientY: 0});
// The tap's own click comes after the lift; it must not toggle.
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
expect(onOpenChange.mock.calls).toEqual([[true]]);
fireEvent.pointerDown(item('Delete'), touch);
fireEvent.pointerUp(item('Delete'), touch);
expect(onPick).toHaveBeenCalledWith('Delete');
expect(onOpenChange.mock.calls).toEqual([[true], [false]]);
});

it('coarse primary pointer: a mouse keeps the mouse path: it opens on its press, and a press in the menu released outside closes it', () => {
stubCoarsePointer();
const trigger = renderMenu();
fireEvent.pointerDown(trigger, mouse);
expect(trigger).toHaveAttribute('aria-expanded', 'true');
fireEvent.pointerUp(trigger, mouse);
fireEvent.click(trigger, {detail: 1});
expect(trigger).toHaveAttribute('aria-expanded', 'true');
fireEvent.pointerDown(item('Edit'), mouse);
fireEvent.pointerUp(document.body, mouse);
expect(trigger).toHaveAttribute('aria-expanded', 'false');
});
});
});

describe('DropdownMenu trigger handler composition', () => {
Expand Down
Loading