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] Add Support for onMouseOver and onMouseLeave props in <AdvancedMarker /> #304

Open
rileymiller opened this issue Apr 9, 2024 · 11 comments

Comments

@rileymiller
Copy link

Target Use Case

Adding these props would enable developers to build functionality around AdvancedMarker hover states. e.g I'd like to be able to show a preview of some metadata in an InfoWindow when a marker is being hovered over.

Proposal

Add onMouseOver and onMouseLeave as props, similar to the current api of onClick and the drag event handlers.

@rvanderfeer
Copy link

I ran into this too. You can work around this by adding the props to your AdvancedMarker content, for example;

<AdvancedMarker
	ref={markerRef}
	position={markerPosition}
	onClick={() => setInfowindowOpen(true)}
	{...other}
>
	<div
		onMouseOver={() => setInfowindowOpen(true)}
		onMouseOut={() => setInfowindowOpen(false)}
		style={{
			width: 20,
			height: 20,
			position: 'absolute',
			top: 0,
			left: 0,
			background: '#F00',
			border: `2px solid #FFF`,
			borderRadius: '50%',
			transform: 'translate(-50%, -50%)'
		}}
	/>
	{infowindowOpen && (
		<InfoWindow
			anchor={marker}
			maxWidth={200}
			onCloseClick={() => setInfowindowOpen(false)}
		>
			infowindow content
		</InfoWindow>
	)}
</AdvancedMarker>

@usefulthink
Copy link
Collaborator

Unfortunately, that doesn't work if your content is just a Pin component. I should be able to look into this tomorrow.

@BParvatkar
Copy link

Were you able to look into it @usefulthink. I was looking into the API reference for AdvanceMarker here. Seems that the addEventListener method is part of beta version as of now. The APIProvider component does allow us to pass version as prop. Was wondering if this will work:

  • Pass version as beta in APIProvider prop
  • The AdvancedMarker component extends for all EventListener.
  • The user passes any listener as prop, let say onMouseEnter
  • If APIProvider is initialized in beta mode then we allow this events, else give error saying that its still in beta and needs to initialize APIProvider in beta version.

The benefit of this approach is that we don't have to manually add each listener to the components like AdvancedMarker or Pin

@usefulthink
Copy link
Collaborator

We're in a bit of a transition-period right now – the version currently in beta will introduce the web-components (<gmp-map> and <gmp-advanced-marker>), and with that we'll probably also get support for DOM events on the AdvancedMarkerElement itself.

But I wouldn't want to make the beta-channel a requirement for using AdvancedMarker, if we can solve this in another way.

Until the web-components are in GA (weekly and quarterly channels), the best thing to do is probably to add the event-listeners to the advancedMarker.element instead. We probably don't even need a different handling of this for when web-components are released, but that remains to be seen.

@BParvatkar
Copy link

Is there any example of adding listeners to the advancedMarker.element or does it need to be built?

@usefulthink
Copy link
Collaborator

No, it's on my ToDo-list, but I haven't gotten to it yet. I'm still pondering if we should use wrapped events for the markers like we do for the map itself, or if we should just use the native events.

@BParvatkar
Copy link

Earlier it made sense to add each declaration because the type of events was MapMouseEvent and not the native events emitted by the element. But as per the docs, they will be emitting native events so I think it will be better to go the native way.

@usefulthink
Copy link
Collaborator

Yeah, I'm thinking the same. For the map it was necessary because the map-events mostly don't contain any useful data about the event, but here we have perfectly functional DOM-events already :)

@jogoley
Copy link

jogoley commented Jul 17, 2024

Did anyone ever figure this out, I am following generally what you are saying but don't know how to implement the addition of listener.

@jogoley
Copy link

jogoley commented Jul 17, 2024

I ran into this too. You can work around this by adding the props to your AdvancedMarker content, for example;

<AdvancedMarker
	ref={markerRef}
	position={markerPosition}
	onClick={() => setInfowindowOpen(true)}
	{...other}
>
	<div
		onMouseOver={() => setInfowindowOpen(true)}
		onMouseOut={() => setInfowindowOpen(false)}
		style={{
			width: 20,
			height: 20,
			position: 'absolute',
			top: 0,
			left: 0,
			background: '#F00',
			border: `2px solid #FFF`,
			borderRadius: '50%',
			transform: 'translate(-50%, -50%)'
		}}
	/>
	{infowindowOpen && (
		<InfoWindow
			anchor={marker}
			maxWidth={200}
			onCloseClick={() => setInfowindowOpen(false)}
		>
			infowindow content
		</InfoWindow>
	)}
</AdvancedMarker>

I was able to use this method but was only able to use it after setting version='beta' in the props for APIProvider

@ewarrenG
Copy link

ewarrenG commented Jul 28, 2024

Were you able to look into it @usefulthink. I was looking into the API reference for AdvanceMarker here. Seems that the addEventListener method is part of beta version as of now. The APIProvider component does allow us to pass version as prop. Was wondering if this will work:

  • Pass version as beta in APIProvider prop
  • The AdvancedMarker component extends for all EventListener.
  • The user passes any listener as prop, let say onMouseEnter
  • If APIProvider is initialized in beta mode then we allow this events, else give error saying that its still in beta and needs to initialize APIProvider in beta version.

The benefit of this approach is that we don't have to manually add each listener to the components like AdvancedMarker or Pin

@BParvatkar Can you share a code example of this? I'm unable to make it work how you describe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants