Skip to content

Commit

Permalink
Try fixing auto focus
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Nov 1, 2021
1 parent 0ebc1d9 commit 829d041
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,38 @@ export function useFocusFirstElement( clientId ) {
return;
}

// Find all tabbables within node.
const textInputs = focus.tabbable
.find( ref.current )
.filter( ( node ) => isTextField( node ) );
let target = ref.current;
let candidates;

// If reversed (e.g. merge via backspace), use the last in the set of
// tabbables.
const isReverse = -1 === initialPosition;
const target =
( isReverse ? last : first )( textInputs ) || ref.current;

// Find all text fields or placeholders within the block.
candidates = focus.tabbable
.find( target )
.filter( ( node ) => isTextField( node ) || node.shadowRoot );

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

if ( ! isInsideRootBlock( ref.current, target ) ) {
ref.current.focus();
return;
}

placeCaretAtHorizontalEdge( target, isReverse );
if ( target.shadowRoot ) {
// We must wait for the placeholder content to load.
setTimeout( () => {
// Find all text fields within the placeholder.
candidates = focus.tabbable
.find( target.shadowRoot )
.filter( ( node ) => isTextField( node ) );
target = ( isReverse ? last : first )( candidates ) || target;
placeCaretAtHorizontalEdge( target, isReverse );
} );
} else {
placeCaretAtHorizontalEdge( target, isReverse );
}
}, [ initialPosition ] );

return ref;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export default function EmbeddedAdminContext( props ) {
clearTimeout( timeoutId );
};
}, [] );
const content = (
<StyleProvider document={ { head: shadow } }>
{ props.children }
</StyleProvider>
);

return (
<div
{ ...props }
Expand All @@ -87,13 +93,7 @@ export default function EmbeddedAdminContext( props ) {
role="button"
aria-pressed={ hasFocus }
>
{ shadow &&
createPortal(
<StyleProvider document={ { head: shadow } }>
{ props.children }
</StyleProvider>,
shadow
) }
{ shadow && createPortal( content, shadow ) }
</div>
);
}
3 changes: 2 additions & 1 deletion packages/dom/src/tabbable.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ function filterTabbable( focusables ) {
}

/**
* @param {Element} context
* @param {Element|Document|ShadowRoot} context
*
* @return {Element[]} Tabbable elements within the context.
*/
export function find( context ) {
Expand Down
6 changes: 6 additions & 0 deletions packages/e2e-tests/specs/editor/various/embedding.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ async function insertEmbed( URL ) {
`//*[contains(@class, "components-autocomplete__result") and contains(@class, "is-selected") and contains(text(), 'Embed')]`
);
await page.keyboard.press( 'Enter' );
await page.evaluate(
() =>
new Promise( ( resolve ) => {
setTimeout( resolve );
} )
);
await page.keyboard.type( URL );
await page.keyboard.press( 'Enter' );
}
Expand Down

0 comments on commit 829d041

Please sign in to comment.