Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure useFocusOnMount runs when all the children tabbable elements have mounted #42187

Merged
merged 7 commits into from
Mar 2, 2023
Merged
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
7 changes: 5 additions & 2 deletions packages/components/src/popover/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ describe( 'Popover', () => {
} );

describe( 'focus behavior', () => {
it( 'should focus the popover by default when opened', async () => {
it( 'should focus the popover container when opened', async () => {
render(
<Popover data-testid="popover-element">
<Popover
focusOnMount={ true }
data-testid="popover-element"
>
Popover content
</Popover>
);
Expand Down
48 changes: 36 additions & 12 deletions packages/compose/src/hooks/use-focus-on-mount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,37 @@ import { focus } from '@wordpress/dom';
*/
export default function useFocusOnMount( focusOnMount = 'firstElement' ) {
const focusOnMountRef = useRef( focusOnMount );

/**
* Sets focus on a DOM element.
*
* @param {HTMLElement} target The DOM element to set focus to.
* @return {void}
*/
const setFocus = ( target ) => {
target.focus( {
// When focusing newly mounted dialogs,
// the position of the popover is often not right on the first render
// This prevents the layout shifts when focusing the dialogs.
preventScroll: true,
} );
};

/** @type {import('react').MutableRefObject<ReturnType<setTimeout> | undefined>} */
const timerId = useRef();

useEffect( () => {
focusOnMountRef.current = focusOnMount;
}, [ focusOnMount ] );

useEffect( () => {
return () => {
if ( timerId.current ) {
clearTimeout( timerId.current );
}
};
}, [] );

return useCallback( ( node ) => {
if ( ! node || focusOnMountRef.current === false ) {
return;
Expand All @@ -40,21 +67,18 @@ export default function useFocusOnMount( focusOnMount = 'firstElement' ) {
return;
}

let target = node;

if ( focusOnMountRef.current === 'firstElement' ) {
const firstTabbable = focus.tabbable.find( node )[ 0 ];
timerId.current = setTimeout( () => {
const firstTabbable = focus.tabbable.find( node )[ 0 ];

if ( firstTabbable ) {
target = /** @type {HTMLElement} */ ( firstTabbable );
}
if ( firstTabbable ) {
setFocus( /** @type {HTMLElement} */ ( firstTabbable ) );
}
}, 0 );

return;
}

target.focus( {
// When focusing newly mounted dialogs,
// the position of the popover is often not right on the first render
// This prevents the layout shifts when focusing the dialogs.
preventScroll: true,
} );
setFocus( node );
}, [] );
}
4 changes: 2 additions & 2 deletions packages/e2e-tests/specs/editor/various/rich-text.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,13 @@ describe( 'RichText', () => {
await page.waitForXPath(
'//button[@role="tab"][@aria-selected="true"][text()="Text"]'
);
// Tab to the "Text" tab.
// Initial focus is on the "Text" tab.
// Tab to the "Custom color picker".
await page.keyboard.press( 'Tab' );
// Tab to black.
await page.keyboard.press( 'Tab' );
// Select color other than black.
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Enter' );

expect( await getEditedPostContent() ).toMatchSnapshot();
Expand Down