Skip to content

Commit

Permalink
Refactor setting focusedBeforeMount ref
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Lende <alex+github.com@lende.xyz>
  • Loading branch information
jeryj and ajlende authored Dec 18, 2024
1 parent ebce63e commit 0c834eb
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions packages/compose/src/hooks/use-focus-return/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,13 @@ function useFocusReturn( onFocusReturn ) {
return;
}

/** @type {any} */
const activeElement = node.ownerDocument.activeElement;
const activeDocument =
node.ownerDocument.activeElement instanceof
window.HTMLIFrameElement
? node.ownerDocument.activeElement.contentDocument
: node.ownerDocument;

// If the activeElement is an iframe, we need to get the active element within the iframe.
// Otherwise, focus from items within the iframed canvas will get sent to the iframe itself,
// not the active element within the iframe.
if (
activeElement?.tagName === 'IFRAME' &&
activeElement.contentDocument?.activeElement
) {
focusedBeforeMount.current =
activeElement.contentDocument.activeElement;
} else {
focusedBeforeMount.current = activeElement;
}
focusedBeforeMount.current = activeDocument?.activeElement ?? null;
} else if ( focusedBeforeMount.current ) {
const isFocused = ref.current?.contains(
ref.current?.ownerDocument.activeElement
Expand Down

0 comments on commit 0c834eb

Please sign in to comment.