Skip to content

Commit 0ee2ea6

Browse files
committed
fix: modal z-index and apply scroll lock when opened
1 parent 95aef75 commit 0ee2ea6

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/components/modal/src/index.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ interface ModalProps extends React.HTMLAttributes<HTMLDivElement> {
2727

2828
// TODO: add a11y https://react.dev/reference/react-dom/createPortal#rendering-a-modal-dialog-with-a-portal
2929

30+
let openModals = 0;
31+
3032
/**
3133
* Modal is a full screen overlay that sits atop the page content.
3234
* It’s used to focus attention on an important task or message and requires user input to be dismissed.
@@ -82,6 +84,18 @@ const Modal = ({
8284
setInternalOpen(open);
8385
}, [open]);
8486

87+
// Lock scroll if there are open modals
88+
useEffect(() => {
89+
if (internalOpen) openModals += 1;
90+
else openModals -= 1;
91+
92+
if (openModals > 1) {
93+
document.body.classList.add(`${styles.lock_scroll}`);
94+
} else {
95+
document.body.classList.remove(`${styles.lock_scroll}`);
96+
}
97+
}, [internalOpen, onClose, onOpen]);
98+
8599
return (
86100
internalOpen && (
87101
<Layer elevation="overlay">

packages/components/modal/src/styles/index.module.scss

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
@import "@react-ck/theme";
2+
@import "@react-ck/elevation";
23

34
.root {
45
@include define-css-var(modal, spacing, get-spacing(5));
56

67
display: flex;
78
align-items: center;
89
justify-content: center;
9-
position: absolute;
10+
position: fixed;
1011
top: 0;
1112
left: 0;
1213
height: 100%;
1314
width: 100%;
15+
z-index: map-get-strict($elevation, overlay);
16+
}
17+
18+
.lock_scroll {
19+
overflow: hidden;
1420
}
1521

1622
.clickable_overlay {

0 commit comments

Comments
 (0)