From 692b8b76cf38c8e5916d7c19a1001e46e8456c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ella=20van=C2=A0Durpe?= Date: Mon, 22 Mar 2021 16:25:51 +0200 Subject: [PATCH] Focus explicitly (#29431) --- .../block-library/src/embed/wp-embed-preview.js | 4 +--- packages/components/src/focusable-iframe/index.js | 13 +++---------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/packages/block-library/src/embed/wp-embed-preview.js b/packages/block-library/src/embed/wp-embed-preview.js index 2bdc086e3bc19..f83341853c250 100644 --- a/packages/block-library/src/embed/wp-embed-preview.js +++ b/packages/block-library/src/embed/wp-embed-preview.js @@ -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 @@ -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 ); diff --git a/packages/components/src/focusable-iframe/index.js b/packages/components/src/focusable-iframe/index.js index a2ee9b98169a9..f98df583a2016 100644 --- a/packages/components/src/focusable-iframe/index.js +++ b/packages/components/src/focusable-iframe/index.js @@ -4,7 +4,7 @@ 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 ] ); @@ -12,7 +12,6 @@ export default function FocusableIframe( { iframeRef, onFocus, ...props } ) { const iframe = fallbackRef.current; const { ownerDocument } = iframe; const { defaultView } = ownerDocument; - const { FocusEvent } = defaultView; /** * Checks whether the iframe is the activeElement, inferring that it has @@ -23,13 +22,7 @@ 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 ); @@ -37,7 +30,7 @@ export default function FocusableIframe( { iframeRef, onFocus, ...props } ) { 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