@@ -10,14 +10,14 @@ import { ModalProps } from './Modal';
1010const ESC_KEY = 27 ;
1111
1212class Modal extends React . PureComponent < ModalProps > {
13- public el : HTMLDivElement ;
14- public modalRoot : HTMLBodyElement ;
13+ public el : HTMLDivElement | null ;
14+ public modalRoot : HTMLBodyElement | null ;
1515 public content : React . RefObject < HTMLDivElement > = React . createRef ( ) ;
1616
1717 constructor ( props : ModalProps ) {
1818 super ( props ) ;
19- this . el = document . createElement ( 'div' ) ;
20- this . modalRoot = document . getElementsByTagName ( 'body' ) [ 0 ] ;
19+ this . el = null ;
20+ this . modalRoot = null ;
2121 }
2222
2323 public componentDidMount ( ) {
@@ -29,7 +29,10 @@ class Modal extends React.PureComponent<ModalProps> {
2929 document . body . style . height = 'initial' ;
3030 document . body . style . minHeight = 'initial' ;
3131
32+ this . el = document . createElement ( 'div' ) ;
33+ this . modalRoot = document . getElementsByTagName ( 'body' ) [ 0 ] ;
3234 this . modalRoot . appendChild ( this . el ) ;
35+ this . forceUpdate ( ) ;
3336 }
3437
3538 public componentDidUpdate ( ) {
@@ -39,7 +42,9 @@ class Modal extends React.PureComponent<ModalProps> {
3942 }
4043
4144 public componentWillUnmount ( ) {
42- this . modalRoot . removeChild ( this . el ) ;
45+ if ( this . modalRoot && this . el ) {
46+ this . modalRoot . removeChild ( this . el ) ;
47+ }
4348 }
4449
4550 public handleKeyDown = ( event : React . KeyboardEvent < HTMLDivElement > ) => {
@@ -54,7 +59,7 @@ class Modal extends React.PureComponent<ModalProps> {
5459 public render ( ) {
5560 const { transparent, visible, isScrollable = false } = this . props ;
5661
57- if ( ! visible ) return null ;
62+ if ( ! visible || ! this . el ) return null ;
5863
5964 return ReactDOM . createPortal (
6065 < FocusTrap >
0 commit comments