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

Refactor focus on mount #27699

Merged
merged 3 commits into from
Dec 14, 2020
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
6 changes: 2 additions & 4 deletions packages/components/src/modal/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ function ModalFrameContent( {
}
}
}
const focusOnMountRef = useFocusOnMount(
focusOnMount ? 'container' : false
);
const focusOnMountRef = useFocusOnMount();
const constrainedTabbingRef = useConstrainedTabbing();
const focusReturnRef = useFocusReturn();

Expand All @@ -60,9 +58,9 @@ function ModalFrameContent( {
className={ classnames( 'components-modal__frame', className ) }
style={ style }
ref={ mergeRefs( [
focusOnMountRef,
constrainedTabbingRef,
focusReturnRef,
focusOnMount ? focusOnMountRef : null,
] ) }
role={ role }
aria-label={ contentLabel }
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ const Popover = ( {
containerRef,
focusOnMount ? constrainedTabbingRef : null,
focusOnMount ? focusReturnRef : null,
focusOnMountRef,
focusOnMount ? focusOnMountRef : null,
];
const mergedRefs = useCallback( mergeRefs( allRefs ), allRefs );

Expand Down
2 changes: 1 addition & 1 deletion packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ _Parameters_

_Returns_

- `(Function|Object)`: Element Ref.
- `Function`: Ref callback.

<a name="useFocusReturn" href="#useFocusReturn">#</a> **useFocusReturn**

Expand Down
2 changes: 1 addition & 1 deletion packages/compose/src/hooks/use-dialog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function useDialog( options ) {
return [
mergeRefs( [
constrainedTabbingRef,
focusOnMountRef,
focusReturnRef,
focusOnMountRef,
closeOnEscapeRef,
] ),
focusOutsideProps,
Expand Down
65 changes: 18 additions & 47 deletions packages/compose/src/hooks/use-focus-on-mount/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* WordPress dependencies
*/
import { useCallback, useRef } from '@wordpress/element';
import { focus } from '@wordpress/dom';
import { useCallback, useEffect, useRef, useState } from '@wordpress/element';

/**
* Hook used to focus the first tabbable element on mount.
*
* @param {boolean|string} focusOnMount Focus on mount mode.
* @return {Function|Object} Element Ref.
* @return {Function} Ref callback.
*
* @example
* ```js
Expand All @@ -25,58 +25,29 @@ import { useCallback, useEffect, useRef, useState } from '@wordpress/element';
* }
* ```
*/
function useFocusOnMount( focusOnMount = 'firstElement' ) {
const focusOnMountRef = useRef( focusOnMount );
const [ node, setNode ] = useState();
useEffect( () => {
focusOnMountRef.current = focusOnMount;
}, [ focusOnMount ] );
export default function useFocusOnMount( focusOnMount ) {
const didMount = useRef( false );
return useCallback( ( node ) => {
if ( ! node || didMount.current === true ) {
return;
}

// Ideally we should be running the focus behavior in the useCallback directly
// Right now we have some issues where the link popover remounts
// prevents us from doing that.
const ref = useCallback( setNode, [] );
didMount.current = true;

// Focus handling
useEffect( () => {
if ( ! node ) {
if ( node.contains( node.ownerDocument.activeElement ) ) {
return;
}
/*
* Without the setTimeout, the dom node is not being focused. Related:
* https://stackoverflow.com/questions/35522220/react-ref-with-focus-doesnt-work-without-settimeout-my-example
*
* TODO: Treat the cause, not the symptom.
*/
const focusTimeout = setTimeout( () => {
if ( focusOnMountRef.current === false || ! node ) {
return;
}

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

if ( firstTabbable ) {
firstTabbable.focus();
} else {
node.focus();
}
let target = node;

return;
}
if ( focusOnMount === 'firstElement' ) {
const firstTabbable = focus.tabbable.find( node )[ 0 ];

if (
focusOnMountRef.current === 'container' ||
focusOnMountRef.current === true
) {
node.focus();
if ( firstTabbable ) {
target = firstTabbable;
}
}, 0 );

return () => clearTimeout( focusTimeout );
}, [ node ] );
}

return ref;
target.focus();
}, [] );
}

export default useFocusOnMount;