From 637445313c8c9364cbf1f32346d3438fc0589d74 Mon Sep 17 00:00:00 2001
From: Ben Schlegel <31989404+benschlegel@users.noreply.github.com>
Date: Tue, 28 Nov 2023 19:59:12 +0100
Subject: [PATCH] feat: Allow glyphs to be passed as children (close #98)
(#99)
Allows the glyph prop of to be passed via JSX children. Functionality is implemented like discussed in #98.
---
.../assets/info-circle.svg | 6 ++++
examples/markers-and-infowindows/src/app.tsx | 13 ++++++++
src/components/pin.tsx | 33 +++++++++++++++++--
3 files changed, 49 insertions(+), 3 deletions(-)
create mode 100644 examples/markers-and-infowindows/assets/info-circle.svg
diff --git a/examples/markers-and-infowindows/assets/info-circle.svg b/examples/markers-and-infowindows/assets/info-circle.svg
new file mode 100644
index 0000000..8728591
--- /dev/null
+++ b/examples/markers-and-infowindows/assets/info-circle.svg
@@ -0,0 +1,6 @@
+
+
diff --git a/examples/markers-and-infowindows/src/app.tsx b/examples/markers-and-infowindows/src/app.tsx
index 01c4192..5b426b6 100644
--- a/examples/markers-and-infowindows/src/app.tsx
+++ b/examples/markers-and-infowindows/src/app.tsx
@@ -43,6 +43,19 @@ const App = () => {
glyphColor={'#0f677a'}>
+ {/* advanced marker with html pin glyph */}
+
+
+ {/* child gets rendered as 'glyph' element of pin */}
+
+
+
+
{/* advanced marker with html-content */}
{
+export const Pin = (props: PropsWithChildren) => {
const advancedMarker = useContext(AdvancedMarkerContext)?.marker;
+ const glyphContainer = useMemo(() => document.createElement('div'), []);
// Create Pin View instance
useEffect(() => {
@@ -24,15 +33,33 @@ export const Pin = (props: PinProps) => {
return;
}
+ if (props.glyph && props.children) {
+ logErrorOnce(
+ 'The component only uses children to render the glyph if both the glyph property and children are present.'
+ );
+ }
+
+ if (Children.count(props.children) > 1) {
+ logErrorOnce(
+ 'Passing multiple children to the component might lead to unexpected results.'
+ );
+ }
+
const pinViewOptions: google.maps.marker.PinElementOptions = {
...props
};
const pinElement = new google.maps.marker.PinElement(pinViewOptions);
+ // Set glyph to glyph container if children are present (rendered via portal).
+ // If both props.glyph and props.children are present, props.children takes priority.
+ if (props.children) {
+ pinElement.glyph = glyphContainer;
+ }
+
// Set content of Advanced Marker View to the Pin View element
advancedMarker.content = pinElement.element;
}, [advancedMarker, props]);
- return null;
+ return createPortal(props.children, glyphContainer);
};