From d565d4c7955c13f9431598f8d3fe84c74a668a6d Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Fri, 23 Oct 2026 23:31:57 +0200 Subject: [PATCH 1/4] DropdownMenu: Focus items according to the opening interaction --- packages/components/CHANGELOG.md | 1 + .../components/src/dropdown-menu/README.md | 2 + .../components/src/dropdown-menu/index.tsx | 57 ++++++++++++++++++- .../src/navigable-container/container.tsx | 34 +++++++---- .../specs/editor/various/list-view.spec.js | 4 +- test/e2e/specs/site-editor/patterns.spec.js | 4 ++ 6 files changed, 88 insertions(+), 14 deletions(-) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 80805a97897120..e6946c79252f58 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -8,6 +8,7 @@ ### Enhancements +- `DropdownMenu`: Focus the menu on pointer opening and enter its items with arrow keys ([#84583](https://github.com/WordPress/gutenberg/pull/84583)). - `RangeControl`: Use the design system outline for the slider thumb focus ring ([#85536](https://github.com/WordPress/gutenberg/pull/85536)). - `UnitControl`: Use the design system outline for the unit select focus ring ([#85444](https://github.com/WordPress/gutenberg/pull/85444)). - `CheckboxControl`, `RadioControl`: Use the design system outline for focus rings ([#85379](https://github.com/WordPress/gutenberg/pull/85379)). diff --git a/packages/components/src/dropdown-menu/README.md b/packages/components/src/dropdown-menu/README.md index 5de6740f35fdd9..39183469583307 100644 --- a/packages/components/src/dropdown-menu/README.md +++ b/packages/components/src/dropdown-menu/README.md @@ -43,6 +43,8 @@ Generally, the parent button should have a triangular icon to the right of the i The parent button should retain the same visual styling regardless of whether the DropdownMenu is displayed or not. +Opening the menu with a pointer focuses the menu container without highlighting an item. Arrow keys then move focus into the menu. Opening it with Enter, Space, or ArrowDown focuses the first item. Set `popoverProps.focusOnMount` to override this default focus behavior. + #### Placement The DropdownMenu should typically appear directly below, or below and to the left of, the parent button. If there isn’t enough space below to display the full DropdownMenu, it can be displayed instead above the parent button. diff --git a/packages/components/src/dropdown-menu/index.tsx b/packages/components/src/dropdown-menu/index.tsx index 5ab0f4b866f9d0..1be5ef8605f301 100644 --- a/packages/components/src/dropdown-menu/index.tsx +++ b/packages/components/src/dropdown-menu/index.tsx @@ -1,5 +1,6 @@ import clsx from 'clsx'; import { menu } from '@wordpress/icons'; +import { useEffect, useState } from '@wordpress/element'; import { contextConnectWithoutRef, useContextSystem } from '../context'; import Button from '../button'; import Dropdown from '../dropdown'; @@ -53,6 +54,12 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { dropdownMenuProps, 'DropdownMenu' ); + const [ focusFirstItem, setFocusFirstItem ] = useState( true ); + useEffect( () => { + if ( open === false ) { + setFocusFirstItem( true ); + } + }, [ open ] ); if ( ! controls?.length && ! isFunction( children ) ) { return null; @@ -78,11 +85,35 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { }, popoverProps ); + const onPopoverFocusCapture = ( + event: React.FocusEvent< HTMLDivElement > + ) => { + popoverProps?.onFocusCapture?.( event ); + + if ( + ! focusFirstItem && + popoverProps?.focusOnMount === undefined && + event.target === event.currentTarget && + event.currentTarget.ownerDocument.activeElement === + event.currentTarget + ) { + // Keep pointer-opened menus focused without highlighting an item. + event.currentTarget + .querySelector< HTMLElement >( '[role="menu"]' ) + ?.focus( { preventScroll: true } ); + } + }; return ( { const openOnArrowDown = ( event: React.KeyboardEvent ) => { // A toggle disabled with `accessibleWhenDisabled` can still be @@ -93,6 +124,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { if ( ! isOpen && event.code === 'ArrowDown' ) { event.preventDefault(); + setFocusFirstItem( true ); onToggle(); } }; @@ -114,6 +146,16 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { icon={ icon } onClick={ ( ( event ) => { + if ( ! isOpen ) { + const nativeEvent = event.nativeEvent; + // Assistive technology can send trusted clicks with + // nonzero detail and an empty pointer type. + setFocusFirstItem( + event.detail === 0 || + ( 'pointerType' in nativeEvent && + nativeEvent.pointerType === '' ) + ); + } onToggle(); if ( mergedToggleProps.onClick ) { mergedToggleProps.onClick( event ); @@ -150,7 +192,11 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { ); return ( - + { isFunction( children ) ? children( props ) : null } { controlSets?.flatMap( ( controlSet, indexOfSet ) => controlSet.map( ( control, indexOfControl ) => ( @@ -205,7 +251,12 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { } } open={ open } defaultOpen={ defaultOpen } - onToggle={ onToggleProp } + onToggle={ ( isOpen ) => { + if ( ! isOpen ) { + setFocusFirstItem( true ); + } + onToggleProp?.( isOpen ); + } } /> ); } diff --git a/packages/components/src/navigable-container/container.tsx b/packages/components/src/navigable-container/container.tsx index b93296cd142acb..f8f31e9eb719a0 100644 --- a/packages/components/src/navigable-container/container.tsx +++ b/packages/components/src/navigable-container/container.tsx @@ -21,13 +21,14 @@ function cycleValue( value: number, total: number, offset: number ) { function getFocusableContext( container: HTMLElement, target: Element, - tabbableOnly: boolean + tabbableOnly: boolean, + allowContainer: boolean ) { const finder = tabbableOnly ? focus.tabbable : focus.focusable; const focusables = finder.find( container ); const index = focusables.indexOf( target as HTMLElement ); - if ( index > -1 ) { + if ( index > -1 || ( allowContainer && target === container ) ) { return { index, target, focusables }; } return null; @@ -76,7 +77,10 @@ function UnforwardedNavigableContainer( const targetHasMenuItemRole = !! targetRole && MENU_ITEM_ROLES.includes( targetRole ); - if ( targetHasMenuItemRole ) { + if ( + targetHasMenuItemRole || + ( ! onlyBrowserTabstops && event.target === container ) + ) { event.preventDefault(); } } @@ -94,24 +98,34 @@ function UnforwardedNavigableContainer( const context = getFocusableContext( container!, activeElement, - !! onlyBrowserTabstops + !! onlyBrowserTabstops, + ! onlyBrowserTabstops && + [ + 'ArrowDown', + 'ArrowUp', + 'ArrowLeft', + 'ArrowRight', + ].includes( event.code ) ); if ( ! context ) { return; } const { index, focusables } = context; - const nextIndex = cycle - ? cycleValue( index, focusables.length, offset ) - : index + offset; + let nextIndex = index + offset; + if ( index === -1 ) { + nextIndex = offset > 0 ? 0 : focusables.length - 1; + } else if ( cycle ) { + nextIndex = cycleValue( index, focusables.length, offset ); + } if ( nextIndex >= 0 && nextIndex < focusables.length ) { focusables[ nextIndex ].focus(); onNavigate( nextIndex, focusables[ nextIndex ] as HTMLElement ); - // `preventDefault()` on tab to avoid having the browser move the focus - // after this component has already moved it. - if ( event.code === 'Tab' ) { + // Prevent scrolling when entering from the container, and keep + // Tab from moving focus again after navigation has handled it. + if ( index === -1 || event.code === 'Tab' ) { event.preventDefault(); } } diff --git a/test/e2e/specs/editor/various/list-view.spec.js b/test/e2e/specs/editor/various/list-view.spec.js index 843c79d721581a..837db23d972e28 100644 --- a/test/e2e/specs/editor/various/list-view.spec.js +++ b/test/e2e/specs/editor/various/list-view.spec.js @@ -1462,8 +1462,10 @@ test.describe( 'List View', () => { await optionsForFileToggle.click(); await expect( optionsForFileMenu, - 'Pressing Space should also open the menu dropdown' + 'Clicking should also open the menu dropdown' ).toBeVisible(); + await expect( optionsForFileMenu ).toBeFocused(); + await page.keyboard.press( 'ArrowDown' ); await expect( firstMenuItem ).toBeFocused(); await pageUtils.pressKeys( 'access+z' ); // Keyboard shortcut for Delete. await expect diff --git a/test/e2e/specs/site-editor/patterns.spec.js b/test/e2e/specs/site-editor/patterns.spec.js index ac69a6af5b3029..96f43a3ce61a91 100644 --- a/test/e2e/specs/site-editor/patterns.spec.js +++ b/test/e2e/specs/site-editor/patterns.spec.js @@ -65,6 +65,10 @@ test.describe( 'Patterns', () => { .getByRole( 'menuitem', { name: 'add pattern', } ); + await expect( + page.getByRole( 'menu', { name: 'add pattern' } ) + ).toBeFocused(); + await page.keyboard.press( 'ArrowDown' ); await expect( addNewMenuItem ).toBeFocused(); await addNewMenuItem.click(); } From cb0d295294d4befa8338b6fd9228403fd2fb79e7 Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Fri, 23 Oct 2026 23:34:21 +0200 Subject: [PATCH 2/4] DropdownMenu: Link focus changelog to its dedicated PR --- packages/components/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index e6946c79252f58..2654884b0ff253 100644 --- a/packages/components/CHANGELOG.md +++ b/packages/components/CHANGELOG.md @@ -8,7 +8,7 @@ ### Enhancements -- `DropdownMenu`: Focus the menu on pointer opening and enter its items with arrow keys ([#84583](https://github.com/WordPress/gutenberg/pull/84583)). +- `DropdownMenu`: Focus the menu on pointer opening and enter its items with arrow keys ([#85486](https://github.com/WordPress/gutenberg/pull/85486)). - `RangeControl`: Use the design system outline for the slider thumb focus ring ([#85536](https://github.com/WordPress/gutenberg/pull/85536)). - `UnitControl`: Use the design system outline for the unit select focus ring ([#85444](https://github.com/WordPress/gutenberg/pull/85444)). - `CheckboxControl`, `RadioControl`: Use the design system outline for focus rings ([#85379](https://github.com/WordPress/gutenberg/pull/85379)). From 65b25e457d198f0b88e8717f59f1a53650c0e0eb Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Sat, 24 Oct 2026 00:21:01 +0200 Subject: [PATCH 3/4] Update locked block menu focus expectation --- test/e2e/specs/editor/various/list-view.spec.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/e2e/specs/editor/various/list-view.spec.js b/test/e2e/specs/editor/various/list-view.spec.js index 837db23d972e28..6401170453ea7c 100644 --- a/test/e2e/specs/editor/various/list-view.spec.js +++ b/test/e2e/specs/editor/various/list-view.spec.js @@ -1487,6 +1487,8 @@ test.describe( 'List View', () => { optionsForFileMenu.getByRole( 'menuitem', { name: 'Delete' } ), 'The delete menu item should be hidden for locked blocks' ).toBeHidden(); + await expect( optionsForFileMenu ).toBeFocused(); + await page.keyboard.press( 'ArrowDown' ); await expect( firstMenuItem ).toBeFocused(); await pageUtils.pressKeys( 'access+z' ); await expect From 59cc7b01935a2d41bb46d5e2fb3e0cd82fef64ec Mon Sep 17 00:00:00 2001 From: Marco Ciampini Date: Tue, 27 Oct 2026 11:50:36 +0000 Subject: [PATCH 4/4] DropdownMenu: Simplify opening focus and cover navigation --- .../components/src/dropdown-menu/index.tsx | 8 +- .../dropdown-menu/test/focus.browser.test.tsx | 87 +++++++++++++++++++ .../src/navigable-container/container.tsx | 20 ++--- 3 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 packages/components/src/dropdown-menu/test/focus.browser.test.tsx diff --git a/packages/components/src/dropdown-menu/index.tsx b/packages/components/src/dropdown-menu/index.tsx index 1be5ef8605f301..d543ba6d5c389c 100644 --- a/packages/components/src/dropdown-menu/index.tsx +++ b/packages/components/src/dropdown-menu/index.tsx @@ -90,6 +90,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { ) => { popoverProps?.onFocusCapture?.( event ); + // A consumer's focus handler may already have moved focus elsewhere. if ( ! focusFirstItem && popoverProps?.focusOnMount === undefined && @@ -251,12 +252,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { } } open={ open } defaultOpen={ defaultOpen } - onToggle={ ( isOpen ) => { - if ( ! isOpen ) { - setFocusFirstItem( true ); - } - onToggleProp?.( isOpen ); - } } + onToggle={ onToggleProp } /> ); } diff --git a/packages/components/src/dropdown-menu/test/focus.browser.test.tsx b/packages/components/src/dropdown-menu/test/focus.browser.test.tsx new file mode 100644 index 00000000000000..97efaa8204ed94 --- /dev/null +++ b/packages/components/src/dropdown-menu/test/focus.browser.test.tsx @@ -0,0 +1,87 @@ +import { describe, expect, it } from 'vitest'; +import { userEvent } from 'vitest/browser'; +import { render } from 'vitest-browser-react'; +import DropdownMenu from '..'; +import MenuItem from '../../menu-item'; +import '../../button/style.scss'; +import '../../dropdown/style.scss'; +import '../../popover/style.scss'; +import '../style.scss'; + +const menuItems = () => ( + <> + First item + Last item + +); + +describe( 'DropdownMenu opening focus', () => { + it( 'focuses the menu on pointer opening and enters its first or last item with arrow keys', async () => { + const user = userEvent.setup(); + const screen = await render( + { menuItems } + ); + const toggle = screen.getByRole( 'button', { name: 'Open menu' } ); + const menu = screen.getByRole( 'menu', { name: 'Open menu' } ); + + await user.click( toggle ); + await expect.element( menu ).toHaveFocus(); + await user.keyboard( '[ArrowDown]' ); + await expect + .element( screen.getByRole( 'menuitem', { name: 'First item' } ) ) + .toHaveFocus(); + + await user.keyboard( '[Escape]' ); + await expect.element( toggle ).toHaveFocus(); + await user.click( toggle ); + await expect.element( menu ).toHaveFocus(); + await user.keyboard( '[ArrowUp]' ); + await expect + .element( screen.getByRole( 'menuitem', { name: 'Last item' } ) ) + .toHaveFocus(); + } ); + + it.each( [ '[Enter]', '[Space]', '[ArrowDown]' ] )( + 'focuses the first item on keyboard opening with %s', + async ( key ) => { + const user = userEvent.setup(); + const screen = await render( + { menuItems } + ); + + await user.tab(); + await user.keyboard( key ); + await expect + .element( + screen.getByRole( 'menuitem', { name: 'First item' } ) + ) + .toHaveFocus(); + } + ); + + it( "preserves a consumer's custom focus target", async () => { + const user = userEvent.setup(); + const screen = await render( + { + if ( event.target === event.currentTarget ) { + screen + .getByRole( 'menuitem', { name: 'Last item' } ) + .element() + .focus(); + } + }, + } } + > + { menuItems } + + ); + + await user.click( screen.getByRole( 'button', { name: 'Open menu' } ) ); + await expect + .element( screen.getByRole( 'menuitem', { name: 'Last item' } ) ) + .toHaveFocus(); + } ); +} ); diff --git a/packages/components/src/navigable-container/container.tsx b/packages/components/src/navigable-container/container.tsx index f8f31e9eb719a0..9c6f482532d905 100644 --- a/packages/components/src/navigable-container/container.tsx +++ b/packages/components/src/navigable-container/container.tsx @@ -21,14 +21,13 @@ function cycleValue( value: number, total: number, offset: number ) { function getFocusableContext( container: HTMLElement, target: Element, - tabbableOnly: boolean, - allowContainer: boolean + tabbableOnly: boolean ) { const finder = tabbableOnly ? focus.tabbable : focus.focusable; const focusables = finder.find( container ); const index = focusables.indexOf( target as HTMLElement ); - if ( index > -1 || ( allowContainer && target === container ) ) { + if ( index > -1 || ( ! tabbableOnly && target === container ) ) { return { index, target, focusables }; } return null; @@ -98,14 +97,7 @@ function UnforwardedNavigableContainer( const context = getFocusableContext( container!, activeElement, - !! onlyBrowserTabstops, - ! onlyBrowserTabstops && - [ - 'ArrowDown', - 'ArrowUp', - 'ArrowLeft', - 'ArrowRight', - ].includes( event.code ) + !! onlyBrowserTabstops ); if ( ! context ) { return; @@ -123,9 +115,9 @@ function UnforwardedNavigableContainer( focusables[ nextIndex ].focus(); onNavigate( nextIndex, focusables[ nextIndex ] as HTMLElement ); - // Prevent scrolling when entering from the container, and keep - // Tab from moving focus again after navigation has handled it. - if ( index === -1 || event.code === 'Tab' ) { + // `preventDefault()` on tab to avoid having the browser move the focus + // after this component has already moved it. + if ( event.code === 'Tab' ) { event.preventDefault(); } }