|
1 | | -import React, { useEffect, useRef } from 'react'; |
| 1 | +import React, { Ref, useEffect, useRef } from 'react'; |
2 | 2 | import { useDispatch, useSelector } from 'react-redux'; |
3 | 3 |
|
4 | 4 | import { Button } from '@mui/material'; |
@@ -295,29 +295,39 @@ function navbarDropDown(props) { |
295 | 295 | let showMenu = props.dropMenu ? 'navDropDown' : 'hideNavDropDown'; |
296 | 296 |
|
297 | 297 | //for closing the menu on clicks outside of it. |
298 | | - const useOutsideClick = (callback) => { |
| 298 | + const useOutsideClick = () => { |
299 | 299 | const dropdownRef = useRef(null); |
300 | 300 |
|
301 | 301 | useEffect(() => { |
302 | 302 | const handleClick = (event) => { |
303 | | - if ( |
304 | | - dropdownRef.current && |
305 | | - !dropdownRef.current.contains(event.target) |
| 303 | + if (event.type === "click" && |
| 304 | + (dropdownRef.current && |
| 305 | + !dropdownRef.current.contains(event.target)) || event.type === "message" && event.data === 'iframeClicked' |
306 | 306 | ) { |
307 | | - callback(); |
| 307 | + handleClose(); |
308 | 308 | } |
309 | 309 | }; |
310 | | - document.addEventListener('click', handleClick, true); |
| 310 | + window.addEventListener('click', handleClick, true); |
| 311 | + window.addEventListener('message', handleClick);//to capture clicks in the iframe |
311 | 312 |
|
312 | 313 | return () => { |
313 | | - document.removeEventListener('click', handleClick, true); //cleanup for memory purposes. ensures handleclick isn't called after the component is no longer rendered |
| 314 | + window.removeEventListener('click', handleClick, true); |
| 315 | + window.addEventListener('message', handleClick); //cleanup for memory purposes. ensures handleclick isn't called after the component is no longer rendered |
314 | 316 | }; |
315 | 317 | }, [dropdownRef]); |
316 | 318 |
|
317 | 319 | return dropdownRef; |
318 | 320 | }; |
319 | 321 |
|
320 | | - const ref = useOutsideClick(handleClose); |
| 322 | + const ref = useOutsideClick(); |
| 323 | + |
| 324 | + // const handleMessage = (event) => { |
| 325 | + // if (event.data === 'iframeClicked') { |
| 326 | + // // Handle the click event here, e.g., close the dropdown menu |
| 327 | + // handleClose(); |
| 328 | + // } |
| 329 | + // }; |
| 330 | + // window.addEventListener('message', handleMessage); |
321 | 331 |
|
322 | 332 | return ( |
323 | 333 | // <div ref={dropdownRef} className={showMenu}> dropdownRef making the menu fly off and anchorel messingup |
|
0 commit comments