Skip to content

Commit 0c834eb

Browse files
jeryjajlende
andauthored
Refactor setting focusedBeforeMount ref
Co-authored-by: Alex Lende <[email protected]>
1 parent ebce63e commit 0c834eb

File tree

1 file changed

+6
-14
lines changed
  • packages/compose/src/hooks/use-focus-return

1 file changed

+6
-14
lines changed

packages/compose/src/hooks/use-focus-return/index.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,13 @@ function useFocusReturn( onFocusReturn ) {
4848
return;
4949
}
5050

51-
/** @type {any} */
52-
const activeElement = node.ownerDocument.activeElement;
51+
const activeDocument =
52+
node.ownerDocument.activeElement instanceof
53+
window.HTMLIFrameElement
54+
? node.ownerDocument.activeElement.contentDocument
55+
: node.ownerDocument;
5356

54-
// If the activeElement is an iframe, we need to get the active element within the iframe.
55-
// Otherwise, focus from items within the iframed canvas will get sent to the iframe itself,
56-
// not the active element within the iframe.
57-
if (
58-
activeElement?.tagName === 'IFRAME' &&
59-
activeElement.contentDocument?.activeElement
60-
) {
61-
focusedBeforeMount.current =
62-
activeElement.contentDocument.activeElement;
63-
} else {
64-
focusedBeforeMount.current = activeElement;
65-
}
57+
focusedBeforeMount.current = activeDocument?.activeElement ?? null;
6658
} else if ( focusedBeforeMount.current ) {
6759
const isFocused = ref.current?.contains(
6860
ref.current?.ownerDocument.activeElement

0 commit comments

Comments
 (0)