1- import PropTypes from 'prop-types' ;
21import React , { useCallback , useRef } from 'react' ;
3- import MediaQuery from 'react-responsive' ;
2+ import { useMediaQuery } from 'react-responsive' ;
43import { useSelector } from 'react-redux' ;
54import { useHistory } from 'react-router-dom' ;
65import { useTranslation } from 'react-i18next' ;
76import { useModalClose } from '../../../common/useModalClose' ;
7+ import type { RootState } from '../../../reducers' ;
88
99import ExitIcon from '../../../images/exit.svg' ;
1010
11- const Overlay = ( {
11+ type OverlayProps = {
12+ children ?: React . ReactNode ;
13+ actions ?: React . ReactNode ;
14+ closeOverlay ?: ( ) => void ;
15+ title ?: string ;
16+ ariaLabel ?: string ;
17+ isFixedHeight ?: boolean ;
18+ } ;
19+
20+ export const Overlay = ( {
1221 actions,
13- ariaLabel,
22+ ariaLabel = 'modal' ,
1423 children,
1524 closeOverlay,
16- isFixedHeight,
17- title
18- } ) => {
25+ isFixedHeight = false ,
26+ title = 'Modal'
27+ } : OverlayProps ) => {
1928 const { t } = useTranslation ( ) ;
2029
21- const previousPath = useSelector ( ( state ) => state . ide . previousPath ) ;
30+ const previousPath = useSelector (
31+ ( state : RootState ) => state . ide . previousPath
32+ ) ;
2233
23- const ref = useRef ( null ) ;
34+ const ref = useRef < HTMLElement > ( null ) ;
2435
2536 const browserHistory = useHistory ( ) ;
2637
38+ const isDesktop = useMediaQuery ( { minWidth : 770 } ) ;
39+ const isMobile = useMediaQuery ( { maxWidth : 769 } ) ;
40+
2741 const close = useCallback ( ( ) => {
2842 const node = ref . current ;
2943 if ( ! node ) return ;
3044 // Only close if it is the last (and therefore the topmost overlay)
3145 const overlays = document . getElementsByClassName ( 'overlay' ) ;
32- if ( node . parentElement . parentElement !== overlays [ overlays . length - 1 ] )
46+ if ( node . parentElement ? .parentElement !== overlays [ overlays . length - 1 ] )
3347 return ;
3448
3549 if ( ! closeOverlay ) {
@@ -55,7 +69,7 @@ const Overlay = ({
5569 < header className = "overlay__header" >
5670 < h2 className = "overlay__title" > { title } </ h2 >
5771 < div className = "overlay__actions" >
58- < MediaQuery minWidth = { 770 } > { actions } </ MediaQuery >
72+ { isDesktop && actions }
5973 < button
6074 className = "overlay__close-button"
6175 onClick = { close }
@@ -65,34 +79,12 @@ const Overlay = ({
6579 </ button >
6680 </ div >
6781 </ header >
68- < MediaQuery maxWidth = { 769 } >
69- { actions && (
70- < div className = "overlay__actions-mobile" > { actions } </ div >
71- ) }
72- </ MediaQuery >
82+ { isMobile && actions && (
83+ < div className = "overlay__actions-mobile" > { actions } </ div >
84+ ) }
7385 { children }
7486 </ section >
7587 </ div >
7688 </ div >
7789 ) ;
7890} ;
79-
80- Overlay . propTypes = {
81- children : PropTypes . element ,
82- actions : PropTypes . element ,
83- closeOverlay : PropTypes . func ,
84- title : PropTypes . string ,
85- ariaLabel : PropTypes . string ,
86- isFixedHeight : PropTypes . bool
87- } ;
88-
89- Overlay . defaultProps = {
90- children : null ,
91- actions : null ,
92- title : 'Modal' ,
93- closeOverlay : null ,
94- ariaLabel : 'modal' ,
95- isFixedHeight : false
96- } ;
97-
98- export default Overlay ;
0 commit comments