1- import React , { useRef , useEffect } from 'react' ;
1+ import React , { useRef , useEffect , useState } from 'react' ;
22import BottomPanel from '../components/bottom/BottomPanel' ;
33import CanvasContainer from '../components/main/CanvasContainer' ;
44import DemoRender from '../components/main/DemoRender' ;
@@ -11,6 +11,7 @@ import { Amplify, Storage } from 'aws-amplify';
1111import awsconfig from '../../../src/custom-aws-exports' ;
1212
1313const MainContainer = ( props ) : JSX . Element => {
14+ const [ bottomShow , setBottomShow ] = useState ( false )
1415 const dispatch = useDispatch ( ) ;
1516 const screenshotTrigger = useSelector ( ( store : RootState ) => store . appState . screenshotTrigger ) ;
1617 const id : string = useSelector ( ( store : RootState ) => store . appState . _id ) ;
@@ -65,13 +66,46 @@ const MainContainer = (props): JSX.Element => {
6566 }
6667 }
6768
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+
68102 return (
69103 < div className = "main-container" style = { style } ref = { containerRef } >
70104 < div className = "main" >
71105 < CanvasContainer isThemeLight = { props . isThemeLight } />
72106 < DemoRender />
73107 </ div >
74- < div className = "bottom-hide" >
108+ < div onMouseOver = { ( ) => setBottomShow ( true ) } className = { showPanel } ref = { ref } >
75109 < BottomPanel isThemeLight = { props . isThemeLight } />
76110 </ div >
77111 </ div >
0 commit comments