An open source design system that's fully customizable and agent ready
TypeScript
fix(dropdownmenu): close on Tab per APG menu-button pattern (menus-5) (#3368)
Cindy Zhanged78e111db82
diff --git a/.changeset/dropdownmenu-tab-close.md b/.changeset/dropdownmenu-tab-close.mdnew file mode 100644index 0000000000..6aacd18d34--- /dev/null+++ b/.changeset/dropdownmenu-tab-close.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] DropdownMenu: pressing Tab in an open menu now closes it (APG menu-button pattern) and returns focus to the trigger, instead of leaking focus into the page while the menu stayed open (#3343).+@cixzhangdiff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsxindex 8093964182..468c48775a 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx@@ -45,20 +45,14 @@ beforeEach(() => {describe('DropdownMenu', () => {it('renders trigger button with label', () => {render(- <DropdownMenu- button={{label: 'Actions'}}- items={[{label: 'Item 1'}]}- />,+ <DropdownMenu button={{label: 'Actions'}} items={[{label: 'Item 1'}]} />,);expect(screen.getByRole('button', {name: /Actions/})).toBeInTheDocument();});it('renders menu with role="menu"', () => {render(- <DropdownMenu- button={{label: 'Actions'}}- items={[{label: 'Item 1'}]}- />,+ <DropdownMenu button={{label: 'Actions'}} items={[{label: 'Item 1'}]} />,);expect(screen.getByRole('menu', {hidden: true})).toBeInTheDocument();});@@ -81,10 +75,7 @@ describe('DropdownMenu', () => {it('defaults menu placement below', () => {render(- <DropdownMenu- button={{label: 'Actions'}}- items={[{label: 'Item 1'}]}- />,+ <DropdownMenu button={{label: 'Actions'}} items={[{label: 'Item 1'}]} />,);const popover = screen.getByRole('menu', {hidden: true})@@ -112,10 +103,7 @@ describe('DropdownMenu', () => {it('has aria-haspopup and aria-expanded attributes', () => {render(- <DropdownMenu- button={{label: 'Actions'}}- items={[{label: 'Item 1'}]}- />,+ <DropdownMenu button={{label: 'Actions'}} items={[{label: 'Item 1'}]} />,);const button = screen.getByRole('button', {name: /Actions/});expect(button).toHaveAttribute('aria-haspopup', 'menu');@@ -125,14 +113,25 @@ describe('DropdownMenu', () => {it('opens menu when button is clicked', async () => {const user = userEvent.setup();render(- <DropdownMenu- button={{label: 'Actions'}}- items={[{label: 'Item 1'}]}- />,+ <DropdownMenu button={{label: 'Actions'}} items={[{label: 'Item 1'}]} />,+ );++ await user.click(screen.getByRole('button', {name: /Actions/}));+ expect(HTMLElement.prototype.showPopover).toHaveBeenCalled();+ });++ it('closes the menu when Tab is pressed inside it (APG menu-button)', async () => {+ const user = userEvent.setup();+ render(+ <DropdownMenu button={{label: 'Actions'}} items={[{label: 'Item 1'}]} />,);await user.click(screen.getByRole('button', {name: /Actions/}));expect(HTMLElement.prototype.showPopover).toHaveBeenCalled();++ const menu = screen.getByRole('menu', {hidden: true});+ fireEvent.keyDown(menu, {key: 'Tab'});+ expect(HTMLElement.prototype.hidePopover).toHaveBeenCalled();});it('calls onClick callback when button is clicked', async () => {diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsxindex d26eb8c080..995f772069 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx@@ -294,9 +294,18 @@ export function DropdownMenu({}return;}+ // APG menu-button pattern: Tab closes the menu. Menu items are+ // tabIndex={-1} so the focus trap has nothing trappable and Tab would+ // otherwise leak into the page while the menu stayed open (menus-5).+ // Do NOT preventDefault — closing restores focus to the trigger, and the+ // browser's default Tab then continues from there to the next element.+ if (e.key === 'Tab') {+ closeMenu();+ return;+ }listNavKeyDown(e);},- [listNavKeyDown],+ [listNavKeyDown, closeMenu],);const openAndFocus = useCallback(() => {