Skip to content

Commit 569516f

Browse files
committed
refactor(CModal): prevent body scroll when the modal is visible
1 parent 4b8171c commit 569516f

File tree

1 file changed

+18
-1
lines changed
  • packages/coreui-react/src/components/modal

1 file changed

+18
-1
lines changed

packages/coreui-react/src/components/modal/CModal.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
171171
useLayoutEffect(() => {
172172
if (_visible) {
173173
document.body.classList.add('modal-open')
174+
175+
if (backdrop) {
176+
document.body.style.overflow = 'hidden'
177+
document.body.style.paddingRight = '0px'
178+
}
179+
174180
setTimeout(
175181
() => {
176182
modalRef.current?.focus()
@@ -179,8 +185,19 @@ export const CModal = forwardRef<HTMLDivElement, CModalProps>(
179185
)
180186
} else {
181187
document.body.classList.remove('modal-open')
188+
189+
if (backdrop) {
190+
document.body.style.removeProperty('overflow')
191+
document.body.style.removeProperty('padding-right')
192+
}
193+
}
194+
return () => {
195+
document.body.classList.remove('modal-open')
196+
if (backdrop) {
197+
document.body.style.removeProperty('overflow')
198+
document.body.style.removeProperty('padding-right')
199+
}
182200
}
183-
return () => document.body.classList.remove('modal-open')
184201
}, [_visible])
185202

186203
const handleClickOutside = (event: Event) => {

0 commit comments

Comments
 (0)