Skip to content

Commit 869bb2f

Browse files
committed
feat: support ReactElement to InfoWindow content
1 parent 537c6c7 commit 869bb2f

3 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/components/CustomMarker/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { CustomMarkerProps, OverlayMarker } from './type';
66
import { passRef } from '../../utils/passRef';
77
import { useMapContext } from '../Provider/MapProvider';
88
import { useOverlayMarker } from './hooks';
9-
import { useEvent } from '@@hooks/useEvent';
9+
import { useEvent } from '../../hooks/useEvent';
1010

1111
export const CustomMarker = forwardRef<OverlayMarker, CustomMarkerProps>(function CustomMarker(
1212
{ children, lat, lng, draggable, preventDragOnClickable = true, onDragStart = null, onDrag = null, onDragEnd = null },

src/components/InfoWindow/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { forwardRef, useEffect, useMemo, useState } from 'react';
1+
import { forwardRef, useEffect, useMemo, useRef, useState } from 'react';
22

33
import { AnchorContextData, AnchorProvider } from './Context';
44
import { InfoWindowProps } from './type';
55
import { useMapContext } from '../../components/Provider/MapProvider';
66
import { useApplyInfoWindowEvent } from '../../hooks/useApplyInfoWindowEvent';
77
import { useImportLibrary } from '../../hooks/useImportLibrary';
88
import { passRef } from '../../utils/passRef';
9+
import { createPortal } from 'react-dom';
910

1011
export 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
});

src/components/InfoWindow/type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ export interface InfoWindowEvent {
99
onZIndexChanged?: (infoWindow: google.maps.InfoWindow) => void;
1010
}
1111

12-
export interface InfoWindowProps extends google.maps.InfoWindowOptions, InfoWindowEvent {
12+
export interface InfoWindowProps extends Omit<google.maps.InfoWindowOptions, 'content'>, InfoWindowEvent {
1313
children?: ReactElement;
1414
open?: boolean;
1515
shouldFocus?: boolean;
16+
content?: ReactElement<unknown> | string | Element | Text | null;
1617
}

0 commit comments

Comments
 (0)