Skip to content

Commit 9298c79

Browse files
authored
hide modal siblings from screen reader (#11247)
1 parent 400e42f commit 9298c79

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

react-common/components/controls/Modal.tsx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,34 @@ export const Modal = (props: ModalProps) => {
6161
className
6262
);
6363

64+
const modalRef = React.useRef<HTMLDivElement>(null);
65+
66+
React.useEffect(() => {
67+
const root = parentElement || document.body;
68+
const parent = modalRef.current?.parentElement;
69+
70+
const hiddenSiblings: Element[] = [];
71+
72+
for (const child of root.children) {
73+
if (child !== parent) {
74+
if (!child.hasAttribute("aria-hidden") || child.getAttribute("aria-hidden") === "false") {
75+
(child as HTMLElement).setAttribute("aria-hidden", "true");
76+
hiddenSiblings.push(child);
77+
}
78+
}
79+
}
80+
81+
return () => {
82+
for (const child of hiddenSiblings) {
83+
(child as HTMLElement).removeAttribute("aria-hidden");
84+
}
85+
};
86+
}, [parentElement])
87+
6488
return ReactDOM.createPortal(<FocusTrap className={classes} onEscape={closeClickHandler}>
6589
<div id={id}
6690
className="common-modal"
91+
ref={modalRef}
6792
role={role || "dialog"}
6893
aria-hidden={ariaHidden}
6994
aria-label={ariaLabel}
@@ -131,5 +156,5 @@ export const Modal = (props: ModalProps) => {
131156
</div>
132157
}
133158
</div>
134-
</FocusTrap>, parentElement || document.getElementById("root") || document.body)
159+
</FocusTrap>, parentElement || document.body)
135160
}

0 commit comments

Comments
 (0)