diff --git a/packages/components/CHANGELOG.md b/packages/components/CHANGELOG.md index 80805a97897120..2654884b0ff253 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 ([#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)). 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..d543ba6d5c389c 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,36 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { }, popoverProps ); + const onPopoverFocusCapture = ( + event: React.FocusEvent< HTMLDivElement > + ) => { + popoverProps?.onFocusCapture?.( event ); + + // A consumer's focus handler may already have moved focus elsewhere. + 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 +125,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { if ( ! isOpen && event.code === 'ArrowDown' ) { event.preventDefault(); + setFocusFirstItem( true ); onToggle(); } }; @@ -114,6 +147,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 +193,11 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { ); return ( - + { isFunction( children ) ? children( props ) : null } { controlSets?.flatMap( ( controlSet, indexOfSet ) => controlSet.map( ( control, indexOfControl ) => ( 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 b93296cd142acb..9c6f482532d905 100644 --- a/packages/components/src/navigable-container/container.tsx +++ b/packages/components/src/navigable-container/container.tsx @@ -27,7 +27,7 @@ function getFocusableContext( const focusables = finder.find( container ); const index = focusables.indexOf( target as HTMLElement ); - if ( index > -1 ) { + if ( index > -1 || ( ! tabbableOnly && target === container ) ) { return { index, target, focusables }; } return null; @@ -76,7 +76,10 @@ function UnforwardedNavigableContainer( const targetHasMenuItemRole = !! targetRole && MENU_ITEM_ROLES.includes( targetRole ); - if ( targetHasMenuItemRole ) { + if ( + targetHasMenuItemRole || + ( ! onlyBrowserTabstops && event.target === container ) + ) { event.preventDefault(); } } @@ -101,9 +104,12 @@ function UnforwardedNavigableContainer( } 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(); diff --git a/test/e2e/specs/editor/various/list-view.spec.js b/test/e2e/specs/editor/various/list-view.spec.js index 843c79d721581a..6401170453ea7c 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 @@ -1485,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 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(); }