Skip to content

Commit

Permalink
fix: use correct document for activeElement (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense authored Jun 2, 2023
1 parent d7027d6 commit ceafd14
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ const Modal: React.ForwardRefExoticComponent<
}: ModalProps,
ref: React.Ref<ModalHandle>,
) => {
const ownerWindow = useWindow();
const container = useWaitForDOMRef(containerRef);
const modal = useModalManager(providedManager);

Expand All @@ -279,7 +280,9 @@ const Modal: React.ForwardRefExoticComponent<
useImperativeHandle(ref, () => modal, [modal]);

if (canUseDOM && !prevShow && show) {
lastFocusRef.current = activeElement() as HTMLElement;
lastFocusRef.current = activeElement(
ownerWindow?.document,
) as HTMLElement | null;
}

// TODO: I think this needs to be in an effect
Expand Down Expand Up @@ -312,7 +315,9 @@ const Modal: React.ForwardRefExoticComponent<
// autofocus after onShow to not trigger a focus event for previous
// modals before this one is shown.
if (autoFocus) {
const currentActiveElement = activeElement(document) as HTMLElement;
const currentActiveElement = activeElement(
modal.dialog?.ownerDocument ?? ownerWindow?.document,
) as HTMLElement | null;

if (
modal.dialog &&
Expand Down Expand Up @@ -368,7 +373,7 @@ const Modal: React.ForwardRefExoticComponent<
return;
}

const currentActiveElement = activeElement();
const currentActiveElement = activeElement(ownerWindow?.document);

if (
modal.dialog &&
Expand Down

0 comments on commit ceafd14

Please sign in to comment.