1- import React , { FC } from 'react' ;
1+ import React , { FC , useEffect , useState } from 'react' ;
22import ReactModal from 'react-modal' ;
3+ import scrollbarWidth from 'scrollbarwidth' ;
34import { withTheme , ThemeInterface , styled } from '~/styles' ;
45import { Header } from './Header' ;
56import { Body } from './Body' ;
@@ -19,6 +20,10 @@ interface Props {
1920 theme : ThemeInterface ;
2021}
2122
23+ function hasScrollbar ( ) {
24+ return document . documentElement . scrollHeight > window . innerHeight ;
25+ }
26+
2227export const ModalBase : FC < Props > = ( {
2328 children,
2429 className,
@@ -30,6 +35,7 @@ export const ModalBase: FC<Props> = ({
3035 status,
3136} ) => {
3237 let overlayRef : HTMLDivElement | null = null ;
38+ const [ prevOpen , setPrevOpen ] = useState ( false ) ;
3339 const handleCloseRequest = ( ) => {
3440 if ( ! isCloseable ) {
3541 return ;
@@ -47,6 +53,30 @@ export const ModalBase: FC<Props> = ({
4753 }
4854 } ;
4955
56+ useEffect (
57+ ( ) => {
58+ if ( prevOpen !== isOpen ) {
59+ if ( isOpen ) {
60+ if ( hasScrollbar ( ) ) {
61+ document . body . style . overflow = 'hidden' ;
62+ document . body . style . paddingRight = `${ scrollbarWidth ( ) } px` ;
63+ }
64+ } else {
65+ document . body . style . overflow = null ;
66+ document . body . style . paddingRight = null ;
67+ }
68+
69+ setPrevOpen ( isOpen ) ;
70+ }
71+
72+ return ( ) => {
73+ document . body . style . overflow = null ;
74+ document . body . style . paddingRight = null ;
75+ } ;
76+ } ,
77+ [ isOpen ]
78+ ) ;
79+
5080 return (
5181 < ReactModal
5282 isOpen = { isOpen }
0 commit comments