Skip to content

Commit

Permalink
Only focus within placeholder on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 2, 2021
1 parent e037b0d commit ffcfe01
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function useInitialPosition( clientId ) {
);
}

function useIsMounting() {
const isMounting = useRef( true );

useEffect( () => {
isMounting.current = false;
}, [] );

return isMounting.current;
}

/**
* Transitions focus to the block or inner tabbable when the block becomes
* selected and an initial position is set.
Expand All @@ -62,6 +72,7 @@ function useInitialPosition( clientId ) {
export function useFocusFirstElement( clientId ) {
const ref = useRef();
const initialPosition = useInitialPosition( clientId );
const isMounting = useIsMounting();

useEffect( () => {
if ( initialPosition === undefined || initialPosition === null ) {
Expand Down Expand Up @@ -89,7 +100,10 @@ export function useFocusFirstElement( clientId ) {
// Find all text fields or placeholders within the block.
candidates = focus.tabbable
.find( target )
.filter( ( node ) => isTextField( node ) || node.shadowRoot );
.filter(
( node ) =>
isTextField( node ) || ( isMounting && node.shadowRoot )
);

target = ( isReverse ? last : first )( candidates ) || target;

Expand All @@ -98,7 +112,7 @@ export function useFocusFirstElement( clientId ) {
return;
}

if ( target.shadowRoot ) {
if ( isMounting && target.shadowRoot ) {
// We must wait for the placeholder content to load.
setTimeout( () => {
// Find all text fields within the placeholder.
Expand Down

0 comments on commit ffcfe01

Please sign in to comment.