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

Embed: fix select on focus #29431

Merged
merged 1 commit into from
Mar 22, 2021
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
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 );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need this since React will fire the right handlers when the element receives focus.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that React will even call the right handlers with new FocusEvent. I'm not sure why this was ever added, but it was assumed React wouldn't pick it up.

}
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