Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow <Pin> glyphs to be passed as children (close #98) #99

Merged
merged 6 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/markers-and-infowindows/assets/info-circle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions examples/markers-and-infowindows/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ const App = () => {
glyphColor={'#0f677a'}></Pin>
</AdvancedMarker>

{/* advanced marker with html pin glyph */}
<AdvancedMarker
position={{lat: 15, lng: 20}}
title={'AdvancedMarker with customized pin.'}>
<Pin background={'#22ccff'} borderColor={'#1e89a1'} scale={1.4}>
{/* child gets rendered as 'glyph' element of pin */}
<img
src="../assets/info-circle.svg"
style={{height: '24px', width: '24px'}}
/>
</Pin>
</AdvancedMarker>

{/* advanced marker with html-content */}
<AdvancedMarker
position={{lat: 30, lng: 10}}
Expand Down
33 changes: 30 additions & 3 deletions src/components/pin.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import {useContext, useEffect} from 'react';
import {
Children,
PropsWithChildren,
useContext,
useEffect,
useMemo
} from 'react';
import {AdvancedMarkerContext} from './advanced-marker';
import {createPortal} from 'react-dom';
import {logErrorOnce} from '../libraries/errors';

/**
* Props for the Pin component
Expand All @@ -9,8 +17,9 @@
/**
* Component to render a google maps marker Pin View
*/
export const Pin = (props: PinProps) => {
export const Pin = (props: PropsWithChildren<PinProps>) => {
const advancedMarker = useContext(AdvancedMarkerContext)?.marker;
const glyphContainer = useMemo(() => document.createElement('div'), []);

// Create Pin View instance
useEffect(() => {
Expand All @@ -24,15 +33,33 @@
return;
}

if (props.glyph && props.children) {
logErrorOnce(
'The <Pin> 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 <Pin> 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]);

Check warning on line 62 in src/components/pin.tsx

View workflow job for this annotation

GitHub Actions / test

React Hook useEffect has a missing dependency: 'glyphContainer'. Either include it or remove the dependency array

return null;
return createPortal(props.children, glyphContainer);
};
Loading