Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(DropdownMenu): a pointer open keeps the row a drag already lit (#10590)

Christopher Chedeau73eb8cb64dbc
diff --git a/.changeset/menu-open-focus-keeps-lit-row.md b/.changeset/menu-open-focus-keeps-lit-row.md
new file mode 100644
index 0000000000..483326eb18
--- /dev/null
+++ b/.changeset/menu-open-focus-keeps-lit-row.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] DropdownMenu: a held mouse dragged from the trigger onto a row keeps that row lit. On a loaded page the menu's open focus could land after the drag reached the row and take the highlight back to the menu container until the pointer moved again.
+
+@vjeux
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
index 14d7c67763..66404e3073 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -2674,6 +2674,10 @@ describe('DropdownMenu keyboard access for menuitemradio/menuitemcheckbox (#3829
describe('DropdownMenu open focus follows input modality (#4477)', () => {
const items = [{label: 'Edit'}, {label: 'Duplicate'}, {label: 'Delete'}];
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
it('pointer open focuses the menu container, not the first item (items mode)', async () => {
const user = userEvent.setup();
render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
@@ -2706,6 +2710,71 @@ describe('DropdownMenu open focus follows input modality (#4477)', () => {
).not.toHaveFocus();
});
+ it('a row the pointer lit before the open frame keeps focus after a pointer open', async () => {
+ // The open's frame waits until the test runs it, as on a loaded page
+ // where the pointer reaches a row first.
+ const frames: FrameRequestCallback[] = [];
+ vi.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => {
+ frames.push(callback);
+ return frames.length;
+ });
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+ const edit = screen.getByRole('menuitem', {name: 'Edit', hidden: true});
+ fireEvent.pointerMove(edit, {pointerType: 'mouse'});
+ expect(edit).toHaveFocus();
+ act(() => {
+ for (const callback of frames.splice(0)) {
+ callback(performance.now());
+ }
+ });
+
+ expect(edit).toHaveFocus();
+ });
+
+ it('a pointer open with no row lit before the open frame still focuses the menu container', async () => {
+ const frames: FrameRequestCallback[] = [];
+ vi.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => {
+ frames.push(callback);
+ return frames.length;
+ });
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ await user.click(screen.getByRole('button', {name: /Actions/}));
+ act(() => {
+ for (const callback of frames.splice(0)) {
+ callback(performance.now());
+ }
+ });
+
+ expect(screen.getByRole('menu', {hidden: true})).toHaveFocus();
+ });
+
+ it('a keyboard open via Space still focuses the first enabled item when its frame runs', async () => {
+ const frames: FrameRequestCallback[] = [];
+ vi.spyOn(window, 'requestAnimationFrame').mockImplementation(callback => {
+ frames.push(callback);
+ return frames.length;
+ });
+ const user = userEvent.setup();
+ render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
+
+ screen.getByRole('button', {name: /Actions/}).focus();
+ await user.keyboard(' ');
+ act(() => {
+ for (const callback of frames.splice(0)) {
+ callback(performance.now());
+ }
+ });
+
+ expect(
+ screen.getByRole('menuitem', {name: 'Edit', hidden: true}),
+ ).toHaveFocus();
+ });
+
it('first ArrowDown after a pointer open moves focus to the first enabled item', async () => {
const user = userEvent.setup();
render(<DropdownMenu button={{label: 'Actions'}} items={items} />);
diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx
index a0c1b8e460..e6aefd7b1a 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx
@@ -1209,8 +1209,18 @@ function DropdownMenuPopover({
requestAnimationFrame(() => {
const focusEnd = focusLastOnOpenRef.current ? focusLast : focusFirst;
focusLastOnOpenRef.current = false;
- if (openModalityRef.current === 'pointer' || !focusEnd()) {
- listRef.current?.focus();
+ const isPointerOpen = openModalityRef.current === 'pointer';
+ // A pointer that lit a row before this frame keeps that row's
+ // highlight: the row under the pointer stays the focused row.
+ const list = listRef.current;
+ const active = list?.ownerDocument.activeElement;
+ const isRowLit =
+ isPointerOpen &&
+ active != null &&
+ active !== list &&
+ list?.contains(active) === true;
+ if (!isRowLit && (isPointerOpen || !focusEnd())) {
+ list?.focus();
}
openModalityRef.current = 'keyboard';
});