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

[fix] DropdownMenu syncs the refs its show and hide call at commit. After a render React discards, as a suspended transition does, its entrance under a finger holds input as the committed menu says.

@vjeux
47 changes: 46 additions & 1 deletion packages/core/src/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import {readFileSync} from 'node:fs';
import {render, screen, fireEvent, waitFor, act} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {createRef, useState} from 'react';
import {Suspense, createRef, startTransition, use, useState} from 'react';
import * as stylex from '@stylexjs/stylex';
import {DropdownMenu, type DropdownMenuTriggerProps} from './DropdownMenu';
import {DropdownMenuItem} from './DropdownMenuItem';
Expand Down Expand Up @@ -3160,6 +3160,51 @@ describe('DropdownMenu entrance under a finger', () => {
expect(isInHeldLayer(item('Edit'))).toBe(false);
});

it('takes no touch while it enters, after React discards a render that pins it to a popover', async () => {
// A transition that suspends for good: React renders it, then keeps the
// committed tree and never commits that render.
const forever = new Promise<never>(() => {});
function Forever(): null {
use(forever);
return null;
}
function Screen({
presentation,
isPending = false,
}: {
presentation?: 'popover';
isPending?: boolean;
}) {
return (
<Suspense fallback={null}>
<DropdownMenu
button={{label: 'Actions'}}
presentation={presentation}
items={[
{label: 'Edit', onClick: () => {}},
{label: 'Delete', onClick: () => {}},
]}
/>
{isPending ? <Forever /> : null}
</Suspense>
);
}
stubPointer('coarse');
stubEntrance();
const {rerender} = render(<Screen />);
await act(async () => {
startTransition(() => {
rerender(<Screen presentation="popover" isPending />);
});
});
const user = userEvent.setup();

await user.click(screen.getByRole('button', {name: /Actions/}));

// The committed menu is not pinned, so its entrance holds the layer.
expect(isInHeldLayer(item('Edit'))).toBe(true);
});

it('lets go at once when it closes during its entrance', async () => {
stubPointer('coarse');
stubEntrance();
Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import React, {
useCallback,
useEffect,
useId,
useInsertionEffect,
useMemo,
useRef,
useState,
Expand Down Expand Up @@ -1259,7 +1260,11 @@ function DropdownMenuPopover({
entrance: popover.contentRef.current ?? layer,
});
}, [listRef, pressTouchModel, popover.contentRef]);
holdEntranceRef.current = holdEntrance;
// Syncs the show's entrance hold with the committed one, ahead of the
// controlled-open layout effect above, which can show the menu then.
useInsertionEffect(() => {
holdEntranceRef.current = holdEntrance;
});
// The layer runtime's input hold: an unmount during the entrance releases it.
useEffect(
() => () => {
Expand Down Expand Up @@ -1418,7 +1423,11 @@ function DropdownMenuPopover({
holdInvokerThroughPress(buttonRef.current, popover.id);
}
}, [popover.id]);
holdOpeningPressRef.current = holdOpeningPress;
// Syncs the show's trigger hold with the committed one, ahead of the
// controlled-open layout effect above, which can show the menu then.
useInsertionEffect(() => {
holdOpeningPressRef.current = holdOpeningPress;
});

// A mouse opens on press-down; a finger held on the trigger opens after
// the long-press delay. Pressing the trigger of an open menu closes it, and
Expand Down Expand Up @@ -1488,7 +1497,11 @@ function DropdownMenuPopover({
longPressDelayMs:
pressTouchModel === 'pointer' ? MENU_PRESS_LONG_PRESS_MS : undefined,
});
cancelMenuPressRef.current = menuPress.cancel;
// Syncs the hide's gesture cancel with the committed one, ahead of the
// controlled-open layout effect above, which can hide the menu then.
useInsertionEffect(() => {
cancelMenuPressRef.current = menuPress.cancel;
});

const handleButtonClick = useCallback(
(e: React.MouseEvent<HTMLElement>) => {
Expand Down