Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions .changeset/tidy-melons-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@primer/react": patch
---

Prevents body scroll when Dialog (the newer Dialog) is open

<!-- Changed components: Dialog -->
15 changes: 15 additions & 0 deletions src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,21 @@ const _Dialog = React.forwardRef<HTMLDivElement, React.PropsWithChildren<DialogP
[onClose],
)

React.useEffect(() => {
// If the body is already set to overflow: hidden, 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 (document.body.style.overflow === 'hidden') {
return
}

document.body.style.overflow = 'hidden'

return () => {
document.body.style.overflow = ''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking: should we keep a track of what it was set to before and reset to that, just in case it was scroll or clip instead of the default visible?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll update it.

}
}, [])

const header = (renderHeader ?? DefaultHeader)(defaultedProps)
const body = (renderBody ?? DefaultBody)(defaultedProps)
const footer = (renderFooter ?? DefaultFooter)(defaultedProps)
Expand Down