Skip to content

Commit 1d6333d

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

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

packages/react/select/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/number": "workspace:*",
3839
"@radix-ui/primitive": "workspace:*",
3940
"@radix-ui/react-collection": "workspace:*",

packages/react/select/src/select.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { hideOthers } from 'aria-hidden';
2424
import { RemoveScroll } from 'react-remove-scroll';
2525

2626
import type { Scope } from '@radix-ui/react-context';
27+
import { getDeepActiveElement } from '@radix-ui/deep-active-element'
2728

2829
type Direction = 'ltr' | 'rtl';
2930

@@ -583,7 +584,7 @@ const SelectContentImpl = React.forwardRef<SelectContentImplElement, SelectConte
583584
const [firstItem, ...restItems] = getItems().map((item) => item.ref.current);
584585
const [lastItem] = restItems.slice(-1);
585586

586-
const PREVIOUSLY_FOCUSED_ELEMENT = document.activeElement;
587+
const PREVIOUSLY_FOCUSED_ELEMENT = getDeepActiveElement();
587588
for (const candidate of candidates) {
588589
// if focus is already where we want to go, we don't want to keep going through the candidates
589590
if (candidate === PREVIOUSLY_FOCUSED_ELEMENT) return;
@@ -592,7 +593,7 @@ const SelectContentImpl = React.forwardRef<SelectContentImplElement, SelectConte
592593
if (candidate === firstItem && viewport) viewport.scrollTop = 0;
593594
if (candidate === lastItem && viewport) viewport.scrollTop = viewport.scrollHeight;
594595
candidate?.focus();
595-
if (document.activeElement !== PREVIOUSLY_FOCUSED_ELEMENT) return;
596+
if (getDeepActiveElement() !== PREVIOUSLY_FOCUSED_ELEMENT) return;
596597
}
597598
},
598599
[getItems, viewport]
@@ -662,7 +663,7 @@ const SelectContentImpl = React.forwardRef<SelectContentImplElement, SelectConte
662663

663664
const [searchRef, handleTypeaheadSearch] = useTypeaheadSearch((search) => {
664665
const enabledItems = getItems().filter((item) => !item.disabled);
665-
const currentItem = enabledItems.find((item) => item.ref.current === document.activeElement);
666+
const currentItem = enabledItems.find((item) => item.ref.current === getDeepActiveElement());
666667
const nextItem = findNextItem(enabledItems, search, currentItem);
667668
if (nextItem) {
668669
/**
@@ -1335,7 +1336,7 @@ const SelectItem = React.forwardRef<SelectItemElement, SelectItemProps>(
13351336
}
13361337
})}
13371338
onPointerLeave={composeEventHandlers(itemProps.onPointerLeave, (event) => {
1338-
if (event.currentTarget === document.activeElement) {
1339+
if (event.currentTarget === getDeepActiveElement()) {
13391340
contentContext.onItemLeave?.();
13401341
}
13411342
})}
@@ -1559,7 +1560,7 @@ const SelectScrollButtonImpl = React.forwardRef<
15591560
// the viewport, potentially causing the active item to now be partially out of view.
15601561
// We re-run the `scrollIntoView` logic to make sure it stays within the viewport.
15611562
useLayoutEffect(() => {
1562-
const activeItem = getItems().find((item) => item.ref.current === document.activeElement);
1563+
const activeItem = getItems().find((item) => item.ref.current === getDeepActiveElement());
15631564
activeItem?.ref.current?.scrollIntoView({ block: 'nearest' });
15641565
}, [getItems]);
15651566

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)