1- import { forwardRef , useEffect , useMemo , useState } from 'react' ;
1+ import { forwardRef , useEffect , useMemo , useRef , useState } from 'react' ;
22
33import { AnchorContextData , AnchorProvider } from './Context' ;
44import { InfoWindowProps } from './type' ;
55import { useMapContext } from '../../components/Provider/MapProvider' ;
66import { useApplyInfoWindowEvent } from '../../hooks/useApplyInfoWindowEvent' ;
77import { useImportLibrary } from '../../hooks/useImportLibrary' ;
88import { passRef } from '../../utils/passRef' ;
9+ import { createPortal } from 'react-dom' ;
910
1011export const InfoWindow = forwardRef < google . maps . InfoWindow , InfoWindowProps > ( function InfoWindow (
1112 {
@@ -33,7 +34,7 @@ export const InfoWindow = forwardRef<google.maps.InfoWindow, InfoWindowProps>(fu
3334 const mapsLib = useImportLibrary ( 'maps' ) ;
3435
3536 const [ infoWindow , setInfoWindow ] = useState < google . maps . InfoWindow | null > ( null ) ;
36-
37+ const fragment = useRef < HTMLDivElement > ( document . createElement ( 'div' ) ) ;
3738 const [ anchor , setAnchor ] = useState < Parameters < AnchorContextData [ 'setAnchor' ] > [ 0 ] > ( ) ;
3839
3940 const value = useMemo < AnchorContextData > (
@@ -50,7 +51,7 @@ export const InfoWindow = forwardRef<google.maps.InfoWindow, InfoWindowProps>(fu
5051
5152 const infoWindow = new mapsLib . InfoWindow ( {
5253 ariaLabel,
53- content,
54+ content : fragment . current ,
5455 disableAutoPan,
5556 maxWidth,
5657 minWidth,
@@ -79,10 +80,6 @@ export const InfoWindow = forwardRef<google.maps.InfoWindow, InfoWindowProps>(fu
7980 }
8081 } , [ infoWindow , anchor , open ] ) ;
8182
82- useEffect ( ( ) => {
83- infoWindow ?. setContent ( content ) ;
84- } , [ infoWindow , content ] ) ;
85-
8683 useEffect ( ( ) => {
8784 infoWindow ?. setPosition ( position ) ;
8885 } , [ infoWindow , position ?. lat , position ?. lng ] ) ;
@@ -107,5 +104,8 @@ export const InfoWindow = forwardRef<google.maps.InfoWindow, InfoWindowProps>(fu
107104 onZIndexChanged,
108105 } ) ;
109106
110- return < AnchorProvider value = { value } > { children } </ AnchorProvider > ;
107+ return < AnchorProvider value = { value } >
108+ { createPortal ( < > { content } </ > , fragment . current ) }
109+ { children }
110+ </ AnchorProvider > ;
111111} ) ;
0 commit comments