diff --git a/.changeset/tidy-melons-type.md b/.changeset/tidy-melons-type.md new file mode 100644 index 00000000000..3007a564861 --- /dev/null +++ b/.changeset/tidy-melons-type.md @@ -0,0 +1,7 @@ +--- +"@primer/react": patch +--- + +Prevents body scroll when Dialog (the newer Dialog) is open + + diff --git a/src/Dialog/Dialog.tsx b/src/Dialog/Dialog.tsx index 1db909aac6e..6a6391384af 100644 --- a/src/Dialog/Dialog.tsx +++ b/src/Dialog/Dialog.tsx @@ -287,6 +287,22 @@ const _Dialog = React.forwardRef { + const bodyOverflowStyle = document.body.style.overflow || '' + // 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 (bodyOverflowStyle === 'hidden') { + return + } + + document.body.style.overflow = 'hidden' + + return () => { + document.body.style.overflow = bodyOverflowStyle + } + }, []) + const header = (renderHeader ?? DefaultHeader)(defaultedProps) const body = (renderBody ?? DefaultBody)(defaultedProps) const footer = (renderFooter ?? DefaultFooter)(defaultedProps)