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/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
80 changes: 79 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,84 @@ 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('takes no touch while it enters, when the commit that opens it also unpins it from a popover', () => {
stubPointer('coarse');
stubEntrance();
function Screen({
presentation,
isMenuOpen,
}: {
presentation?: 'popover';
isMenuOpen: boolean;
}) {
return (
<DropdownMenu
button={{label: 'Actions'}}
presentation={presentation}
isMenuOpen={isMenuOpen}
onOpenChange={() => {}}
items={[
{label: 'Edit', onClick: () => {}},
{label: 'Delete', onClick: () => {}},
]}
/>
);
}
const {rerender} = render(<Screen presentation="popover" isMenuOpen />);
rerender(<Screen presentation="popover" isMenuOpen={false} />);

// One commit unpins the menu and opens it. A layout effect shows it in
// that commit, and the show starts this commit's entrance hold.
rerender(<Screen isMenuOpen />);

expect(isInHeldLayer(item('Edit'))).toBe(true);
});

it('lets go at once when it closes during its entrance', async () => {
stubPointer('coarse');
stubEntrance();
Expand Down
13 changes: 10 additions & 3 deletions packages/core/src/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import React, {
useState,
type ReactNode,
} from 'react';
import {useCommittedRefInto} from '../hooks/useCommittedRef';
import {readAccessibleName} from '../utils/accessibleName';
import * as stylex from '@stylexjs/stylex';
import {usePopoverInternal} from '../Popover/usePopover';
Expand Down Expand Up @@ -1259,7 +1260,9 @@ function DropdownMenuPopover({
entrance: popover.contentRef.current ?? layer,
});
}, [listRef, pressTouchModel, popover.contentRef]);
holdEntranceRef.current = holdEntrance;
// The show's entrance hold, written at commit before any layout effect. The
// controlled-open layout effect above can show the menu then.
Comment on lines +1263 to +1264

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional (non-blocking): the first sentence restates what useCommittedRefInto does. Keeping only the reason reads shorter, and the same applies to the comments above the other two writes (:1424-1425, :1496-1497, with "hide" for the cancel):

  // The controlled-open layout effect above can show the menu in this commit.
  // The show reads this ref.

useCommittedRefInto(holdEntranceRef, holdEntrance);
// The layer runtime's input hold: an unmount during the entrance releases it.
useEffect(
() => () => {
Expand Down Expand Up @@ -1418,7 +1421,9 @@ function DropdownMenuPopover({
holdInvokerThroughPress(buttonRef.current, popover.id);
}
}, [popover.id]);
holdOpeningPressRef.current = holdOpeningPress;
// The show's trigger hold, written at commit before any layout effect. The
// controlled-open layout effect above can show the menu then.
useCommittedRefInto(holdOpeningPressRef, 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 +1493,9 @@ function DropdownMenuPopover({
longPressDelayMs:
pressTouchModel === 'pointer' ? MENU_PRESS_LONG_PRESS_MS : undefined,
});
cancelMenuPressRef.current = menuPress.cancel;
// The hide's gesture cancel, written at commit before any layout effect.
// The controlled-open layout effect above can hide the menu then.
useCommittedRefInto(cancelMenuPressRef, menuPress.cancel);

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