An open source design system that's fully customizable and agent ready
TypeScript
fix(DropdownMenu): a press-opened menu returns focus to its trigger, not to the control focused before the press (#7160)
Christopher Chedeau6ea1fb7eb96d
diff --git a/.changeset/dropdown-menu-press-open-focus-return.md b/.changeset/dropdown-menu-press-open-focus-return.mdnew file mode 100644index 0000000000..f92e40e71c--- /dev/null+++ b/.changeset/dropdown-menu-press-open-focus-return.md@@ -0,0 +1,9 @@+---+'@astryxdesign/core': patch+---++[fix] A DropdownMenu opened by a mouse press returns focus to its trigger when it closes, instead of to the control that was focused before the press.++A mouse opens the menu on press-down, before the browser's own mousedown has focused the trigger, so the popover remembered the previously focused control and handed focus back there on Escape or after a pick. The trigger now takes focus as the press opens the menu, the state a click-open already had; keyboard opens, taps and the menu's own behavior are unchanged.++@vjeuxdiff --git a/packages/core/src/DropdownMenu/DropdownMenu.spec.md b/packages/core/src/DropdownMenu/DropdownMenu.spec.mdindex 69fd62a182..203f3499ce 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.spec.md+++ b/packages/core/src/DropdownMenu/DropdownMenu.spec.md@@ -16,6 +16,7 @@ verified_by:packages/core/src/DropdownMenu/DropdownMenuSelectable.test.tsx,packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx,packages/core/src/DropdownMenu/__tests__/DropdownMenuDrillIn.a11y.chromium.spec.ts,+ packages/core/src/DropdownMenu/__tests__/MenuPress.a11y.chromium.spec.ts,packages/core/src/BottomSheet/BottomSheet.test.tsx,packages/core/src/List/List.test.tsx,packages/core/src/Indicator/Indicator.test.tsx,@@ -267,7 +268,7 @@ than adding a DropdownMenu-owned heading target.| FR3 | `DropdownMenuSelectable.test.tsx` plus BottomSheet, List, Indicator, Icon, and Divider owner tests | Trigger, touch actions, icons, checkbox, radio | A composed part loses its owner target or is documented as a new local target. | `audit:DropdownMenu/theming` || Layer relationships | `DropdownMenu.test.tsx`, `DropdownMenuSubMenu.test.tsx`, and current layer/dismissal architecture records | Light dismiss, nested flyout, sheet dismissal | Documentation claims a shared owner where current source retains local behavior, or the reverse. | `audit:DropdownMenu/behavior` || FR5 | `DropdownMenu.test.tsx` press model suite; the module record's own map | Finger slide across rows, outside release by mouse and finger, `touch-action`, root marker | A row acting on the press or the stray click, a lost `touch-action`, or a finger release that closes fails. | `audit:DropdownMenu/behavior` |-| FR6 | `DropdownMenu.test.tsx` dismissal and closing suites | keyboard dismissal, pointer dismissal, press outside on a control, pointer pick, closed menu | Focus left on the body after a pointer pick, or a ring after pointer input, fails. | `audit:DropdownMenu/behavior` |+| FR6 | `DropdownMenu.test.tsx` dismissal, closing and press model suites; `__tests__/MenuPress.a11y.chromium.spec.ts` in real Chromium | keyboard/pointer dismissal, Escape after press-open, press outside on a control, pointer pick, closed menu | Focus left on the body after a pointer pick, on the control focused before a press-open, or a ring after pointer input, fails. | `audit:DropdownMenu/behavior` || FR7 | `menuMaxHeight` MUST replace the 300px term of the menu's and its popover viewport's block-size cap while the viewport gutters still bound both; the touch sheet ignores it. | `component:DropdownMenu/DEC-3` | Proposed; verified in jsdom; owner to confirm DEC-3 || Theming anatomy map | `scripts/check-knowledge.mjs` | Canonical anatomy and current target inventory | Missing, extra, prefixed, stale, or unclassified mappings fail repository validation. | `audit:DropdownMenu/theming` || FR8 | `DropdownMenu.test.tsx` press model suite (trigger cases) | Press-open + drag-release, unsettled opening release, trigger toggle, held touch, tap | A menu that does not open on a mouse press, an unsettled release that acts, or a trigger that reopens in the same gesture fails. | `audit:DropdownMenu/behavior` |diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsxindex e0fe4a3043..82580af4d5 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx@@ -2557,6 +2557,59 @@ describe('DropdownMenu press model', () => {expect(HTMLElement.prototype.hidePopover).toHaveBeenCalled();});+ it('a press-opened menu returns focus to its trigger, not to the control focused before the press', async () => {+ // A browser's popover remembers the focused element when it is shown+ // and, when it hides with focus inside it, hands focus back to that+ // element before the layer's own hide handler runs. jsdom has no popover,+ // so the mocks carry that one rule here. A mouse press opens the menu+ // before the browser's mousedown would have focused the trigger: the+ // popover must still remember the trigger, or Escape lands focus on+ // whatever control was focused before the press.+ let rememberedFocus: Element | null = null;+ const showPopover = HTMLElement.prototype.showPopover;+ const hidePopover = HTMLElement.prototype.hidePopover;+ HTMLElement.prototype.showPopover = function (this: HTMLElement) {+ rememberedFocus = document.activeElement;+ showPopover.call(this);+ };+ HTMLElement.prototype.hidePopover = function (this: HTMLElement) {+ if (+ rememberedFocus instanceof HTMLElement &&+ this.contains(document.activeElement)+ ) {+ rememberedFocus.focus();+ }+ hidePopover.call(this);+ };++ render(+ <>+ <button type="button">Before</button>+ <DropdownMenu+ button={{label: 'Actions'}}+ items={[{label: 'Edit'}, {label: 'Delete'}]}+ />+ </>,+ );+ const before = screen.getByRole('button', {name: 'Before'});+ const trigger = screen.getByRole('button', {name: /Actions/});+ before.focus();++ fireEvent.pointerDown(trigger, mouse);+ expect(trigger).toHaveAttribute('aria-expanded', 'true');+ // The popover recorded the trigger, not the control focused before.+ expect(rememberedFocus).toBe(trigger);+ fireEvent.pointerUp(trigger, mouse);+ fireEvent.click(trigger, {detail: 1});+ const menu = screen.getByRole('menu', {hidden: true});+ await waitFor(() => expect(menu).toHaveFocus());++ fireEvent.keyDown(menu, {key: 'Escape'});+ expect(trigger).toHaveAttribute('aria-expanded', 'false');+ expect(trigger).toHaveFocus();+ expect(before).not.toHaveFocus();+ });+it('the opening release before the settle time acts on nothing and the menu stays', () => {vi.useFakeTimers();try {diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsxindex 3a433cae13..9fe1d2486e 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx@@ -1001,6 +1001,13 @@ function DropdownMenuPopover({}return false;}+ // The press opens the menu before the browser's own mousedown has+ // focused the trigger (a held finger never does), so the popover would+ // remember whatever was focused BEFORE the press as the element to hand+ // focus back to when it closes. Focusing the trigger first gives the+ // popover the state a click-open already has: the trigger is where+ // Escape or a pick returns focus (the APG menu-button pattern).+ buttonRef.current?.focus({preventScroll: true});const didOpen = openAndFocus('pointer', true);if (didOpen && pointerType === 'mouse') {// The menu is `popover="auto"`: the browser's light dismiss woulddiff --git a/packages/core/src/DropdownMenu/__tests__/MenuPress.a11y.chromium.spec.ts b/packages/core/src/DropdownMenu/__tests__/MenuPress.a11y.chromium.spec.tsindex 340b8ab0fe..36801f3e83 100644--- a/packages/core/src/DropdownMenu/__tests__/MenuPress.a11y.chromium.spec.ts+++ b/packages/core/src/DropdownMenu/__tests__/MenuPress.a11y.chromium.spec.ts@@ -7,7 +7,8 @@* @output Browser evidence for the menu press model: the row under the* release acts once, the highlight follows the pointer, a release outside* closes under a mouse and not under a finger, a touch pan in an- * overflowing menu cancels the gesture, and a pen behaves like a finger+ * overflowing menu cancels the gesture, a pen behaves like a finger, and+ * Escape after a press-open returns focus to the trigger* @position Real-engine proof for module:DropdownMenu/useMenuPress — the* paths jsdom cannot exercise (hit testing, touch synthesis, scrolling)*/@@ -206,6 +207,39 @@ test.describe('DropdownMenu press model (Chromium)', () => {expect(activations).toEqual([]);});+ test('mouse: Escape after a press-open returns focus to the trigger, not to the control focused before the press', async ({+ page,+ }) => {+ await mount(page, MENU_STORY);+ // The control a user had focused before reaching for the menu — a field,+ // a toolbar button. The story renders none, so one is added to the page.+ await page.evaluate(() => {+ const before = document.createElement('button');+ before.id = 'focused-before-the-press';+ before.textContent = 'Before';+ document.body.prepend(before);+ before.focus();+ });+ const before = page.locator('#focused-before-the-press');+ await expect(before).toBeFocused();++ const trigger = page.locator(TRIGGER);+ const point = await center(page, TRIGGER);+ await page.mouse.move(point.x, point.y);+ await page.mouse.down();+ const menu = page.getByRole('menu');+ await expect(menu).toBeVisible();+ await page.mouse.up();+ // The opening release leaves the menu open with focus on the menu, where+ // Escape is read.+ await expect(menu).toBeFocused();++ await page.keyboard.press('Escape');+ await expect(menu).toBeHidden();+ await expect(trigger).toBeFocused();+ await expect(before).not.toBeFocused();+ });+test('touch: a finger that lands on Edit and lifts on Delete acts on Delete, once; the highlight followed it', async ({page,}) => {