Skip to content

Commit 0f84cd0

Browse files
committed
feat(react-toast): use get-deep-active-element to cross the shadow DOM boundary
1 parent 1d6333d commit 0f84cd0

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

packages/react/toast/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"build": "radix-build"
3535
},
3636
"dependencies": {
37+
"@radix-ui/deep-active-element": "workspace:*",
3738
"@radix-ui/primitive": "workspace:*",
3839
"@radix-ui/react-collection": "workspace:*",
3940
"@radix-ui/react-compose-refs": "workspace:*",

packages/react/toast/src/toast.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { useLayoutEffect } from '@radix-ui/react-use-layout-effect';
1414
import { VisuallyHidden } from '@radix-ui/react-visually-hidden';
1515

1616
import type { Scope } from '@radix-ui/react-context';
17+
import { getDeepActiveElement } from '@radix-ui/deep-active-element'
1718

1819
/* -------------------------------------------------------------------------------------------------
1920
* ToastProvider
@@ -193,7 +194,7 @@ const ToastViewport = React.forwardRef<ToastViewportElement, ToastViewportProps>
193194
};
194195

195196
const handlePointerLeaveResume = () => {
196-
const isFocusInside = wrapper.contains(document.activeElement);
197+
const isFocusInside = wrapper.contains(getDeepActiveElement());
197198
if (!isFocusInside) handleResume();
198199
};
199200

@@ -243,7 +244,7 @@ const ToastViewport = React.forwardRef<ToastViewportElement, ToastViewportProps>
243244
const isTabKey = event.key === 'Tab' && !isMetaKey;
244245

245246
if (isTabKey) {
246-
const focusedElement = document.activeElement;
247+
const focusedElement = getDeepActiveElement();
247248
const isTabbingBackwards = event.shiftKey;
248249
const targetIsViewport = event.target === viewport;
249250

@@ -491,7 +492,7 @@ const ToastImpl = React.forwardRef<ToastImplElement, ToastImplProps>(
491492
const handleClose = useCallbackRef(() => {
492493
// focus viewport if focus is within toast to read the remaining toast
493494
// count to SR users and ensure focus isn't lost
494-
const isFocusInToast = node?.contains(document.activeElement);
495+
const isFocusInToast = node?.contains(getDeepActiveElement());
495496
if (isFocusInToast) context.viewport?.focus();
496497
onClose();
497498
});
@@ -935,12 +936,12 @@ function getTabbableCandidates(container: HTMLElement) {
935936
}
936937

937938
function focusFirst(candidates: HTMLElement[]) {
938-
const previouslyFocusedElement = document.activeElement;
939+
const previouslyFocusedElement = getDeepActiveElement();
939940
return candidates.some((candidate) => {
940941
// if focus is already where we want to go, we don't want to keep going through the candidates
941942
if (candidate === previouslyFocusedElement) return true;
942943
candidate.focus();
943-
return document.activeElement !== previouslyFocusedElement;
944+
return getDeepActiveElement() !== previouslyFocusedElement;
944945
});
945946
}
946947

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)