Skip to content

Commit

Permalink
Focus explicitly (#29431)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Mar 22, 2021
1 parent ec3677d commit 692b8b7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
4 changes: 1 addition & 3 deletions packages/block-library/src/embed/wp-embed-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default function WpEmbedPreview( { html } ) {
useEffect( () => {
const { ownerDocument } = ref.current;
const { defaultView } = ownerDocument;
const { FocusEvent } = defaultView;

/**
* Checks for WordPress embed events signaling the height change when iframe
Expand Down Expand Up @@ -58,8 +57,7 @@ export default function WpEmbedPreview( { html } ) {
return;
}

const focusEvent = new FocusEvent( 'focus', { bubbles: true } );
activeElement.dispatchEvent( focusEvent );
activeElement.focus();
}

defaultView.addEventListener( 'message', resizeWPembeds );
Expand Down
13 changes: 3 additions & 10 deletions packages/components/src/focusable-iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import { useEffect, useRef } from '@wordpress/element';
import { useMergeRefs } from '@wordpress/compose';

export default function FocusableIframe( { iframeRef, onFocus, ...props } ) {
export default function FocusableIframe( { iframeRef, ...props } ) {
const fallbackRef = useRef();
const ref = useMergeRefs( [ iframeRef, fallbackRef ] );

useEffect( () => {
const iframe = fallbackRef.current;
const { ownerDocument } = iframe;
const { defaultView } = ownerDocument;
const { FocusEvent } = defaultView;

/**
* Checks whether the iframe is the activeElement, inferring that it has
Expand All @@ -23,21 +22,15 @@ export default function FocusableIframe( { iframeRef, onFocus, ...props } ) {
return;
}

const focusEvent = new FocusEvent( 'focus', { bubbles: true } );

iframe.dispatchEvent( focusEvent );

if ( onFocus ) {
onFocus( focusEvent );
}
iframe.focus();
}

defaultView.addEventListener( 'blur', checkFocus );

return () => {
defaultView.removeEventListener( 'blur', checkFocus );
};
}, [ onFocus ] );
}, [] );

// Disable reason: The rendered iframe is a pass-through component,
// assigning props inherited from the rendering parent. It's the
Expand Down

0 comments on commit 692b8b7

Please sign in to comment.