Skip to content

Commit 4eb18ea

Browse files
committed
feat(react-navigation-menu): use get-deep-active-element to cross the shadow DOM boundary
1 parent c0aaf24 commit 4eb18ea

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

packages/react/navigation-menu/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/navigation-menu/src/navigation-menu.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { useCallbackRef } from '@radix-ui/react-use-callback-ref';
1616
import * as VisuallyHiddenPrimitive from '@radix-ui/react-visually-hidden';
1717

1818
import type { Scope } from '@radix-ui/react-context';
19+
import { getDeepActiveElement } from '@radix-ui/deep-active-element'
1920

2021
type Orientation = 'vertical' | 'horizontal';
2122
type Direction = 'ltr' | 'rtl';
@@ -875,7 +876,7 @@ const NavigationMenuContentImpl = React.forwardRef<
875876
const handleClose = () => {
876877
onItemDismiss();
877878
onRootContentClose();
878-
if (content.contains(document.activeElement)) triggerRef.current?.focus();
879+
if (content.contains(getDeepActiveElement())) triggerRef.current?.focus();
879880
};
880881
content.addEventListener(ROOT_CONTENT_DISMISS, handleClose);
881882
return () => content.removeEventListener(ROOT_CONTENT_DISMISS, handleClose);
@@ -946,7 +947,7 @@ const NavigationMenuContentImpl = React.forwardRef<
946947
const isTabKey = event.key === 'Tab' && !isMetaKey;
947948
if (isTabKey) {
948949
const candidates = getTabbableCandidates(event.currentTarget);
949-
const focusedElement = document.activeElement;
950+
const focusedElement = getDeepActiveElement();
950951
const index = candidates.findIndex((candidate) => candidate === focusedElement);
951952
const isMovingBackwards = event.shiftKey;
952953
const nextCandidates = isMovingBackwards
@@ -1175,12 +1176,12 @@ function getTabbableCandidates(container: HTMLElement) {
11751176
}
11761177

11771178
function focusFirst(candidates: HTMLElement[]) {
1178-
const previouslyFocusedElement = document.activeElement;
1179+
const previouslyFocusedElement = getDeepActiveElement();
11791180
return candidates.some((candidate) => {
11801181
// if focus is already where we want to go, we don't want to keep going through the candidates
11811182
if (candidate === previouslyFocusedElement) return true;
11821183
candidate.focus();
1183-
return document.activeElement !== previouslyFocusedElement;
1184+
return getDeepActiveElement() !== previouslyFocusedElement;
11841185
});
11851186
}
11861187

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)