Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/components/src/dropdown-menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) {

if ( ! isOpen && event.code === 'ArrowDown' ) {
event.preventDefault();
onToggle();
onToggle( event );
}
};
const { as: Toggle = Button, ...restToggleProps } =
Expand All @@ -114,7 +114,7 @@ function UnconnectedDropdownMenu( dropdownMenuProps: DropdownMenuProps ) {
icon={ icon }
onClick={
( ( event ) => {
onToggle();
onToggle( event );
if ( mergedToggleProps.onClick ) {
mergedToggleProps.onClick( event );
}
Expand Down
33 changes: 29 additions & 4 deletions packages/components/src/dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,7 +23,7 @@ const UnconnectedDropdown = (
contentClassName,
expandOnMobile,
headerTitle,
focusOnMount,
focusOnMount = () => focusOnMountRef.current,
popoverProps,
onClose,
onToggle,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
24 changes: 20 additions & 4 deletions packages/components/src/dropdown/types.ts
Original file line number Diff line number Diff line change
@@ -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 = {
export 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;
};

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/modal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
6 changes: 5 additions & 1 deletion packages/components/src/popover/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_

Expand Down
6 changes: 5 additions & 1 deletion packages/compose/src/hooks/use-dialog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 35 additions & 13 deletions packages/compose/src/hooks/use-focus-on-mount/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 );

Expand All @@ -50,25 +52,40 @@ export function useFocusOnMount(
}, [ focusOnMount ] );

return useRefEffect< HTMLElement >( ( node ) => {
if ( focusOnMountRef.current === false ) {
const currentFocusOnMount = focusOnMountRef.current;
if ( currentFocusOnMount === false ) {
return;
}

if ( node.contains( node.ownerDocument?.activeElement ?? null ) ) {
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])'
);
Expand All @@ -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 );

Expand All @@ -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;
}
Loading