Skip to content

Commit e0fa849

Browse files
committed
fix(Dialog): it now focuses the first element automatically
1 parent b5416dc commit e0fa849

File tree

1 file changed

+12
-0
lines changed
  • packages/orbit-components/src/Dialog

1 file changed

+12
-0
lines changed

packages/orbit-components/src/Dialog/index.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import useLockScrolling from "../hooks/useLockScrolling";
1313
import useClickOutside from "../hooks/useClickOutside";
1414
import useRandomId from "../hooks/useRandomId";
1515
import type { Props } from "./types";
16+
import FOCUSABLE_ELEMENT_SELECTORS from "../hooks/useFocusTrap/consts";
1617

1718
const ActionButtonWrapper = ({ children }: { children: React.ReactNode }) => {
1819
return (
@@ -72,6 +73,17 @@ const Dialog = ({
7273
};
7374
}, [triggerRef]);
7475

76+
React.useEffect(() => {
77+
if (ref.current) {
78+
const focusableElements = ref.current.querySelectorAll<HTMLElement>(
79+
FOCUSABLE_ELEMENT_SELECTORS,
80+
);
81+
if (focusableElements.length > 0) {
82+
focusableElements[0].focus();
83+
}
84+
}
85+
}, []);
86+
7587
const handleClose = (ev: MouseEvent) => {
7688
if (ref && ref.current && onClose) {
7789
if (ref.current && !ref.current.contains(ev.target as Node)) onClose();

0 commit comments

Comments
 (0)