1
- import React , { useRef , useEffect } from 'react' ;
1
+ import React , { useRef , useEffect , useState } from 'react' ;
2
2
import BottomPanel from '../components/bottom/BottomPanel' ;
3
3
import CanvasContainer from '../components/main/CanvasContainer' ;
4
4
import DemoRender from '../components/main/DemoRender' ;
@@ -11,6 +11,7 @@ import { Amplify, Storage } from 'aws-amplify';
11
11
import awsconfig from '../../../src/custom-aws-exports' ;
12
12
13
13
const MainContainer = ( props ) : JSX . Element => {
14
+ const [ bottomShow , setBottomShow ] = useState ( false )
14
15
const dispatch = useDispatch ( ) ;
15
16
const screenshotTrigger = useSelector ( ( store : RootState ) => store . appState . screenshotTrigger ) ;
16
17
const id : string = useSelector ( ( store : RootState ) => store . appState . _id ) ;
@@ -65,13 +66,46 @@ const MainContainer = (props): JSX.Element => {
65
66
}
66
67
}
67
68
69
+ //Logic to close the bottompanel when clicking outside of it
70
+ const useOutsideClick = ( ) => {
71
+ const bottomPanelRef = useRef ( null ) ;
72
+
73
+ useEffect ( ( ) => {
74
+ const handleClick = ( event ) => {
75
+ if ( event . type === "click" &&
76
+ ( bottomPanelRef . current &&
77
+ ! bottomPanelRef . current . contains ( event . target ) ) || ( event . type === "message" && event . data === 'iframeClicked' ) ) {
78
+ //menuButtonRef is to ensure that handleClose does not get invoked when the user clicks on the menu dropdown button
79
+ handleClose ( ) ;
80
+ }
81
+ } ;
82
+ window . addEventListener ( 'click' , handleClick , true ) ;
83
+ window . addEventListener ( 'message' , handleClick ) ; //to capture clicks in the iframe
84
+
85
+ return ( ) => {
86
+ window . removeEventListener ( 'click' , handleClick , true ) ;
87
+ window . addEventListener ( 'message' , handleClick ) ; //cleanup for memory purposes. ensures handleclick isn't called after the component is no longer rendered
88
+ } ;
89
+ } , [ bottomPanelRef ] ) ;
90
+
91
+ return bottomPanelRef ;
92
+ } ;
93
+
94
+ const ref = useOutsideClick ( ) ;
95
+
96
+ const handleClose = ( ) => {
97
+ setBottomShow ( false ) ;
98
+ } ;
99
+
100
+ let showPanel = bottomShow ? 'bottom-show' : 'bottom-hide' ;
101
+
68
102
return (
69
103
< div className = "main-container" style = { style } ref = { containerRef } >
70
104
< div className = "main" >
71
105
< CanvasContainer isThemeLight = { props . isThemeLight } />
72
106
< DemoRender />
73
107
</ div >
74
- < div className = "bottom-hide" >
108
+ < div onMouseOver = { ( ) => setBottomShow ( true ) } className = { showPanel } ref = { ref } >
75
109
< BottomPanel isThemeLight = { props . isThemeLight } />
76
110
</ div >
77
111
</ div >
0 commit comments