From 023b4ff00d601cf859beb3aa78618e2446abe624 Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Sun, 8 Feb 2026 14:58:48 -0800 Subject: [PATCH 1/3] Support callback argument type for `focusOnMount` --- .../src/hooks/use-focus-on-mount/index.ts | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/compose/src/hooks/use-focus-on-mount/index.ts b/packages/compose/src/hooks/use-focus-on-mount/index.ts index ecf348f7e741a2..62c8a9e762e67e 100644 --- a/packages/compose/src/hooks/use-focus-on-mount/index.ts +++ b/packages/compose/src/hooks/use-focus-on-mount/index.ts @@ -5,10 +5,12 @@ import useRefEffect from '../use-ref-effect'; /** * Determines focus behavior when the element mounts. * - * @param focusOnMount Behavioral mode. Defaults to `"firstElement"` which focuses the + * @param focusOnMount Focus options. Defaults to `"firstElement"` which focuses the * first tabbable element within; `"firstInputElement"` focuses the * first value control within; `true` focuses the element itself; - * `false` does nothing. + * `false` does nothing. For dynamic resolution of the focus option + * pass a callback whose return value is one of the preceding + * options, `undefined` to default or an `HTMLElement` to focus. * @return Ref callback. * * @example @@ -27,7 +29,7 @@ import useRefEffect from '../use-ref-effect'; * ``` */ export function useFocusOnMount( - focusOnMount: useFocusOnMount.Mode = 'firstElement' + focusOnMount: useFocusOnMount.FocusOptions = 'firstElement' ) { const focusOnMountRef = useRef( focusOnMount ); @@ -50,7 +52,8 @@ export function useFocusOnMount( }, [ focusOnMount ] ); return useRefEffect< HTMLElement >( ( node ) => { - if ( focusOnMountRef.current === false ) { + const currentFocusOnMount = focusOnMountRef.current; + if ( currentFocusOnMount === false ) { return; } @@ -58,17 +61,31 @@ export function useFocusOnMount( return; } - if ( - focusOnMountRef.current !== 'firstElement' && - focusOnMountRef.current !== 'firstInputElement' - ) { + if ( currentFocusOnMount === true ) { setFocus( node ); return; } const timerId = setTimeout( () => { + let usedFocusOnMount; + if ( typeof currentFocusOnMount === 'function' ) { + usedFocusOnMount = + currentFocusOnMount( node ) ?? 'firstElement'; + if ( typeof usedFocusOnMount === 'boolean' ) { + if ( usedFocusOnMount ) { + setFocus( node ); + } + return; + } + // Focus the element returned. + if ( ! ( typeof usedFocusOnMount === 'string' ) ) { + setFocus( usedFocusOnMount ); + return; + } + } + usedFocusOnMount ??= 'firstElement'; // For 'firstInputElement' mode, try to find a form input element first - if ( focusOnMountRef.current === 'firstInputElement' ) { + if ( currentFocusOnMount === 'firstInputElement' ) { const formInput = node.querySelector< HTMLElement >( 'input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled])' ); @@ -78,11 +95,12 @@ export function useFocusOnMount( return; } } - // Fallback to the first tabbable element - const firstTabbable = focus.tabbable.find( node )[ 0 ]; - if ( firstTabbable ) { - setFocus( firstTabbable ); + if ( usedFocusOnMount === 'firstElement' ) { + const firstTabbable = focus.tabbable.find( node )[ 0 ]; + if ( firstTabbable ) { + setFocus( firstTabbable ); + } } }, 0 ); @@ -94,4 +112,8 @@ export function useFocusOnMount( export namespace useFocusOnMount { export type Mode = boolean | 'firstElement' | 'firstInputElement'; + export type ModeResolver = ( + node: HTMLElement + ) => Mode | HTMLElement | undefined; + export type FocusOptions = Mode | ModeResolver; } From 25efb3990d3a9084c058f2c378725bd010aaf659 Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Wed, 11 Feb 2026 14:19:57 -0800 Subject: [PATCH 2/3] Update types and docs --- packages/components/src/dropdown/types.ts | 22 ++++++++++++++++--- packages/components/src/modal/types.ts | 6 ++++- packages/components/src/popover/types.ts | 6 ++++- packages/compose/README.md | 2 +- .../compose/src/hooks/use-dialog/index.ts | 6 ++++- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/packages/components/src/dropdown/types.ts b/packages/components/src/dropdown/types.ts index bf6b50cfb001d7..106cd2605e0153 100644 --- a/packages/components/src/dropdown/types.ts +++ b/packages/components/src/dropdown/types.ts @@ -1,11 +1,23 @@ import type { useFocusOnMount } from '@wordpress/compose'; -import type { ComponentPropsWithoutRef, CSSProperties, ReactNode } from 'react'; +import type { + ComponentPropsWithoutRef, + CSSProperties, + KeyboardEvent, + MouseEvent, + PointerEvent, + ReactNode, +} from 'react'; import type Popover from '../popover'; import type { PopoverProps } from '../popover/types'; type CallbackProps = { isOpen: boolean; - onToggle: () => void; + /** + * The optional event parameter is only applicable when the component’s + * `focusOnMount` prop is unspecified. In that case it’s recommended and + * used for input device specific focus behavior when the dropdown opens. + */ + onToggle: ( event?: KeyboardEvent | MouseEvent | PointerEvent ) => void; onClose: () => void; }; @@ -48,9 +60,13 @@ export type DropdownProps = { * - `false` does nothing and _should not be used unless an accessible * substitute behavior is implemented_. * + * For dynamic resolution of the focus option pass a callback whose return + * value is one of the preceding options, `undefined` to default or an + * `HTMLElement` to focus. + * * @default 'firstElement' */ - focusOnMount?: useFocusOnMount.Mode; + focusOnMount?: Parameters< typeof useFocusOnMount >[ 0 ]; /** * Set this to customize the text that is shown in the dropdown's header * when it is fullscreen on mobile. diff --git a/packages/components/src/modal/types.ts b/packages/components/src/modal/types.ts index a5810b2063cbfb..6933f2018c2d11 100644 --- a/packages/components/src/modal/types.ts +++ b/packages/components/src/modal/types.ts @@ -59,9 +59,13 @@ export type ModalProps = { * - `false` does nothing and _should not be used unless an accessible * substitute behavior is implemented_. * + * For dynamic resolution of the focus option pass a callback whose return + * value is one of the preceding options, `undefined` to default or an + * `HTMLElement` to focus. + * * @default true */ - focusOnMount?: useFocusOnMount.Mode | 'firstContentElement'; + focusOnMount?: useFocusOnMount.FocusOptions | 'firstContentElement'; /** * Elements that are injected into the modal header to the left of the close button (if rendered). * Hidden if `__experimentalHideHeader` is `true`. diff --git a/packages/components/src/popover/types.ts b/packages/components/src/popover/types.ts index 54d95d3097cf9e..385ce3fd116d28 100644 --- a/packages/components/src/popover/types.ts +++ b/packages/components/src/popover/types.ts @@ -79,9 +79,13 @@ export type PopoverProps = { * - `false` does nothing and _should not be used unless an accessible * substitute behavior is implemented_. * + * For dynamic resolution of the focus option pass a callback whose return + * value is one of the preceding options, `undefined` to default or an + * `HTMLElement` to focus. + * * @default 'firstElement' */ - focusOnMount?: useFocusOnMount.Mode; + focusOnMount?: useFocusOnMount.FocusOptions; /** * A callback invoked when the focus leaves the opened popover. This should * only be provided in advanced use-cases when a popover should close under diff --git a/packages/compose/README.md b/packages/compose/README.md index ec59fe32784460..3253fcc15a7f73 100644 --- a/packages/compose/README.md +++ b/packages/compose/README.md @@ -362,7 +362,7 @@ const WithFocusOnMount = () => { _Parameters_ -- _focusOnMount_ `useFocusOnMount.Mode`: Behavioral mode. Defaults to `"firstElement"` which focuses the first tabbable element within; `"firstInputElement"` focuses the first value control within; `true` focuses the element itself; `false` does nothing. +- _focusOnMount_ `useFocusOnMount.FocusOptions`: Focus options. Defaults to `"firstElement"` which focuses the first tabbable element within; `"firstInputElement"` focuses the first value control within; `true` focuses the element itself; `false` does nothing. For dynamic resolution of the focus option pass a callback whose return value is one of the preceding options, `undefined` to default or an `HTMLElement` to focus. _Returns_ diff --git a/packages/compose/src/hooks/use-dialog/index.ts b/packages/compose/src/hooks/use-dialog/index.ts index 2672d0fcae8b15..8478b4a09c9010 100644 --- a/packages/compose/src/hooks/use-dialog/index.ts +++ b/packages/compose/src/hooks/use-dialog/index.ts @@ -21,9 +21,13 @@ type DialogOptions = { * - `false` does nothing and _should not be used unless an accessible * substitute behavior is implemented_. * + * For dynamic resolution of the focus option pass a callback whose return + * value is one of the preceding options, `undefined` to default or an + * `HTMLElement` to focus. + * * @default 'firstElement' */ - focusOnMount?: useFocusOnMount.Mode; + focusOnMount?: useFocusOnMount.FocusOptions; /** * Determines whether tabbing is constrained to within the popover, * preventing keyboard focus from leaving the popover content without From 607f410ea572c760f5344e4bccdb0e78202a6be4 Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Wed, 11 Feb 2026 12:19:13 -0800 Subject: [PATCH 3/3] Enable input device specific focus when `Dropdown` opens --- .../components/src/dropdown-menu/index.tsx | 4 +-- packages/components/src/dropdown/index.tsx | 33 ++++++++++++++++--- packages/components/src/dropdown/types.ts | 2 +- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/components/src/dropdown-menu/index.tsx b/packages/components/src/dropdown-menu/index.tsx index 5ab0f4b866f9d0..bffe367231f65b 100644 --- a/packages/components/src/dropdown-menu/index.tsx +++ b/packages/components/src/dropdown-menu/index.tsx @@ -93,7 +93,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { if ( ! isOpen && event.code === 'ArrowDown' ) { event.preventDefault(); - onToggle(); + onToggle( event ); } }; const { as: Toggle = Button, ...restToggleProps } = @@ -114,7 +114,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) { icon={ icon } onClick={ ( ( event ) => { - onToggle(); + onToggle( event ); if ( mergedToggleProps.onClick ) { mergedToggleProps.onClick( event ); } diff --git a/packages/components/src/dropdown/index.tsx b/packages/components/src/dropdown/index.tsx index 2d58b9194f188d..72c5397b3c2362 100644 --- a/packages/components/src/dropdown/index.tsx +++ b/packages/components/src/dropdown/index.tsx @@ -6,7 +6,11 @@ import deprecated from '@wordpress/deprecated'; import { contextConnect, useContextSystem } from '../context'; import { useControlledValue } from '../utils/hooks'; import Popover from '../popover'; -import type { DropdownProps, DropdownInternalContext } from './types'; +import type { + DropdownProps, + DropdownInternalContext, + CallbackProps, +} from './types'; const UnconnectedDropdown = ( props: DropdownProps, @@ -19,7 +23,7 @@ const UnconnectedDropdown = ( contentClassName, expandOnMobile, headerTitle, - focusOnMount, + focusOnMount = () => focusOnMountRef.current, popoverProps, onClose, onToggle, @@ -58,6 +62,21 @@ const UnconnectedDropdown = ( onChange: onToggle, } ); + const focusOnMountRef = useRef< 'firstElement' | true >( 'firstElement' ); + const determineFocusOnMount = ( + event: Parameters< CallbackProps[ 'onToggle' ] >[ 0 ] + ) => { + let doFocusFirstElement = ! event; + if ( event && 'nativeEvent' in event ) { + const { detail, nativeEvent } = event; + doFocusFirstElement = + detail === 0 || + ( 'pointerType' in nativeEvent && + nativeEvent.pointerType === '' ); + } + focusOnMountRef.current = doFocusFirstElement ? 'firstElement' : true; + }; + /** * Closes the popover when focus leaves it unless the toggle was pressed or * focus has moved to a separate dialog. The former is to let the toggle @@ -85,9 +104,15 @@ const UnconnectedDropdown = ( setIsOpen( false ); } - const args = { + const args: CallbackProps = { isOpen: !! isOpen, - onToggle: () => setIsOpen( ! isOpen ), + onToggle: ( event ) => { + const nextIsOpen = ! isOpen; + setIsOpen( nextIsOpen ); + if ( nextIsOpen ) { + determineFocusOnMount( event ); + } + }, onClose: close, }; const popoverPropsHaveAnchor = diff --git a/packages/components/src/dropdown/types.ts b/packages/components/src/dropdown/types.ts index 106cd2605e0153..d0d79931a9ef85 100644 --- a/packages/components/src/dropdown/types.ts +++ b/packages/components/src/dropdown/types.ts @@ -10,7 +10,7 @@ import type { import type Popover from '../popover'; import type { PopoverProps } from '../popover/types'; -type CallbackProps = { +export type CallbackProps = { isOpen: boolean; /** * The optional event parameter is only applicable when the component’s