Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dialog background layout shift #5489

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/react/src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -475,18 +475,26 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
)

React.useEffect(() => {
const bodyOverflowStyle = document.body.style.overflow || ''
// If the body is already set to overflow: hidden, it likely means
const {body, documentElement} = document
let {scrollTop} = documentElement
const bodyOverflowStyle = body.style.overflowY || ''

// If the body is already set to scroll, it likely means
// that there is already a modal open. In that case, we should bail
// so we don't re-enable scroll after the second dialog is closed.
if (bodyOverflowStyle === 'hidden') {
if (bodyOverflowStyle === 'scroll') {
return
}

document.body.style.overflow = 'hidden'
scrollTop = documentElement.scrollTop
body.style.top = `-${scrollTop}px`
body.style.position = 'fixed'
body.style.overflowY = 'scroll'

return () => {
document.body.style.overflow = bodyOverflowStyle
documentElement.scrollTop = scrollTop
body.style.position = 'static'
body.style.overflowY = 'auto'
}
}, [])

Expand Down
Loading