-
Notifications
You must be signed in to change notification settings - Fork 112
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
fix: advanced marker anchoring #577
Merged
mrMetalWood
merged 6 commits into
visgl:main
from
mrMetalWood:fix/advanced-marker-anchoring
Oct 24, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11eb23a
fix: repair and improve custom clustering example
mrMetalWood a0f8a4c
fix: use different approach for anchoring the marker
mrMetalWood c528ff4
test: adjust test to match new dome structure
mrMetalWood 1674fdf
docs: comment wording
mrMetalWood d90aaa1
refactor: remove not needed type cast
mrMetalWood c38d7af
test: adjust tests to test for presence rather than hierachy
mrMetalWood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,10 @@ export function isAdvancedMarker( | |
); | ||
} | ||
|
||
function isElementNode(node: Node): node is HTMLElement { | ||
return (node as Node).nodeType === Node.ELEMENT_NODE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, you're right. |
||
} | ||
|
||
/** | ||
* Copy of the `google.maps.CollisionBehavior` constants. | ||
* They have to be duplicated here since we can't wait for the maps API to load to be able to use them. | ||
|
@@ -48,19 +52,19 @@ export const AdvancedMarkerContext = | |
|
||
// [xPosition, yPosition] when the top left corner is [0, 0] | ||
export const AdvancedMarkerAnchorPoint = { | ||
TOP_LEFT: ['0', '0'], | ||
TOP_CENTER: ['50%', '0'], | ||
TOP: ['50%', '0'], | ||
TOP_RIGHT: ['100%', '0'], | ||
LEFT_CENTER: ['0', '50%'], | ||
LEFT_TOP: ['0', '0'], | ||
LEFT: ['0', '50%'], | ||
LEFT_BOTTOM: ['0', '100%'], | ||
RIGHT_TOP: ['100%', '0'], | ||
TOP_LEFT: ['0%', '0%'], | ||
TOP_CENTER: ['50%', '0%'], | ||
TOP: ['50%', '0%'], | ||
TOP_RIGHT: ['100%', '0%'], | ||
LEFT_CENTER: ['0%', '50%'], | ||
LEFT_TOP: ['0%', '0%'], | ||
LEFT: ['0%', '50%'], | ||
LEFT_BOTTOM: ['0%', '100%'], | ||
RIGHT_TOP: ['100%', '0%'], | ||
RIGHT: ['100%', '50%'], | ||
RIGHT_CENTER: ['100%', '50%'], | ||
RIGHT_BOTTOM: ['100%', '100%'], | ||
BOTTOM_LEFT: ['0', '100%'], | ||
BOTTOM_LEFT: ['0%', '100%'], | ||
BOTTOM_CENTER: ['50%', '100%'], | ||
BOTTOM: ['50%', '100%'], | ||
BOTTOM_RIGHT: ['100%', '100%'], | ||
|
@@ -124,28 +128,25 @@ const MarkerContent = ({ | |
const [xTranslation, yTranslation] = | ||
anchorPoint ?? AdvancedMarkerAnchorPoint['BOTTOM']; | ||
|
||
const {transform: userTransform, ...restStyles} = styles ?? {}; | ||
|
||
let transformStyle = `translate(-${xTranslation}, -${yTranslation})`; | ||
// The "translate(50%, 100%)" is here to counter and reset the default anchoring of the advanced marker element | ||
// that comes from the api | ||
const transformStyle = `translate(50%, 100%) translate(-${xTranslation}, -${yTranslation})`; | ||
|
||
// preserve extra transform styles that were set by the user | ||
if (userTransform) { | ||
transformStyle += ` ${userTransform}`; | ||
} | ||
return ( | ||
<div | ||
className={className} | ||
style={{ | ||
width: 'fit-content', | ||
transformOrigin: `${xTranslation} ${yTranslation}`, | ||
transform: transformStyle, | ||
...restStyles | ||
}}> | ||
{children} | ||
// anchoring container | ||
<div style={{transform: transformStyle}}> | ||
{/* AdvancedMarker div that user can give styles and classes */} | ||
<div className={className} style={styles}> | ||
{children} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export type CustomMarkerContent = | ||
| (HTMLDivElement & {isCustomMarker?: boolean}) | ||
| null; | ||
|
||
export type AdvancedMarkerRef = google.maps.marker.AdvancedMarkerElement | null; | ||
function useAdvancedMarker(props: AdvancedMarkerProps) { | ||
const [marker, setMarker] = | ||
|
@@ -185,11 +186,14 @@ function useAdvancedMarker(props: AdvancedMarkerProps) { | |
setMarker(newMarker); | ||
|
||
// create the container for marker content if there are children | ||
let contentElement: HTMLDivElement | null = null; | ||
let contentElement: CustomMarkerContent = null; | ||
if (numChildren > 0) { | ||
contentElement = document.createElement('div'); | ||
contentElement.style.width = '0'; | ||
contentElement.style.height = '0'; | ||
|
||
// We need some kind of flag to identify the custom marker content | ||
// in the infowindow component. Choosing a custom property instead of a className | ||
// to not encourage users to style the marker content directly. | ||
contentElement.isCustomMarker = true; | ||
|
||
newMarker.content = contentElement; | ||
setContentContainer(contentElement); | ||
|
@@ -233,15 +237,31 @@ function useAdvancedMarker(props: AdvancedMarkerProps) { | |
else marker.gmpDraggable = false; | ||
}, [marker, draggable, onDrag, onDragEnd, onDragStart]); | ||
|
||
// set gmpClickable from props (when unspecified, it's true if the onClick event | ||
// callback is specified) | ||
// set gmpClickable from props (when unspecified, it's true if the onClick or one of | ||
// the hover events callbacks are specified) | ||
useEffect(() => { | ||
if (!marker) return; | ||
|
||
if (clickable !== undefined) marker.gmpClickable = clickable; | ||
else if (onClick) marker.gmpClickable = true; | ||
else marker.gmpClickable = false; | ||
}, [marker, clickable, onClick]); | ||
const gmpClickable = | ||
clickable !== undefined || | ||
Boolean(onClick) || | ||
Boolean(onMouseEnter) || | ||
Boolean(onMouseLeave); | ||
|
||
// gmpClickable is only available in beta version of the | ||
// maps api (as of 2024-10-10) | ||
marker.gmpClickable = gmpClickable; | ||
|
||
// enable pointer events for the markers with custom content | ||
if (gmpClickable && marker?.content && isElementNode(marker.content)) { | ||
marker.content.style.pointerEvents = 'none'; | ||
|
||
if (marker.content.firstElementChild) { | ||
(marker.content.firstElementChild as HTMLElement).style.pointerEvents = | ||
'all'; | ||
} | ||
} | ||
}, [marker, clickable, onClick, onMouseEnter, onMouseLeave]); | ||
|
||
useMapsEventListener(marker, 'click', onClick); | ||
useMapsEventListener(marker, 'drag', onDrag); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wondering if we should replace this with something like "expect that there is an element with classname-test somewhere in marker.content" – feels like specifying the exact hierarchy here isn't actually what we want to test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that sounds like a better approach.