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

Get active element within the iframe when restoring focus #68060

Merged
merged 3 commits into from
Dec 18, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import clsx from 'clsx';
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { forwardRef, useRef } from '@wordpress/element';
import { forwardRef } from '@wordpress/element';
import { _x, sprintf } from '@wordpress/i18n';
import { Icon, plus } from '@wordpress/icons';
import deprecated from '@wordpress/deprecated';
Expand All @@ -16,15 +16,11 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import Inserter from '../inserter';
import { useMergeRefs } from '@wordpress/compose';

function ButtonBlockAppender(
{ rootClientId, className, onFocus, tabIndex, onSelect },
ref
) {
const inserterButtonRef = useRef();

const mergedInserterButtonRef = useMergeRefs( [ inserterButtonRef, ref ] );
return (
<Inserter
position="bottom center"
Expand All @@ -34,7 +30,6 @@ function ButtonBlockAppender(
if ( onSelect && typeof onSelect === 'function' ) {
onSelect( ...args );
}
inserterButtonRef.current?.focus();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file is undoing work from https://github.com/WordPress/gutenberg/pull/64877/files#diff-4cdcf916fb8cf5f88b98cd9e95ab532d90369cd768334f7896720a2b85c018bbR37 which fixed moving focus to the appender, but did it without addressing the root issue.

} }
renderToggle={ ( {
onToggle,
Expand All @@ -61,7 +56,7 @@ function ButtonBlockAppender(
return (
<Button
__next40pxDefaultSize
ref={ mergedInserterButtonRef }
ref={ ref }
onFocus={ onFocus }
tabIndex={ tabIndex }
className={ clsx(
Expand Down
8 changes: 7 additions & 1 deletion packages/compose/src/hooks/use-focus-return/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ function useFocusReturn( onFocusReturn ) {
return;
}

focusedBeforeMount.current = node.ownerDocument.activeElement;
const activeDocument =
node.ownerDocument.activeElement instanceof
window.HTMLIFrameElement
? node.ownerDocument.activeElement.contentDocument
: node.ownerDocument;

focusedBeforeMount.current = activeDocument?.activeElement ?? null;
} else if ( focusedBeforeMount.current ) {
const isFocused = ref.current?.contains(
ref.current?.ownerDocument.activeElement
Expand Down
Loading