Skip to content

Commit

Permalink
Fix Custom Callout Unmount (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikischin authored Jun 17, 2024
1 parent 8446d73 commit 9a48478
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
42 changes: 25 additions & 17 deletions src/components/Annotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, {
useState,
useMemo,
useRef,
useLayoutEffect,
} from 'react';
import { createPortal } from 'react-dom';
import MapContext from '../context/MapContext';
Expand Down Expand Up @@ -63,22 +64,6 @@ export default function Annotation({
const contentEl = useMemo<HTMLDivElement>(() => document.createElement('div'), []);
const map = useContext(MapContext);

// Coordinates
useEffect(() => {
if (map === null) return undefined;

const a = new mapkit.Annotation(
new mapkit.Coordinate(latitude, longitude),
() => contentEl,
);
map.addAnnotation(a);
setAnnotation(a);

return () => {
map.removeAnnotation(a);
};
}, [map, latitude, longitude]);

// Padding
useEffect(() => {
if (!annotation) return;
Expand All @@ -103,7 +88,7 @@ export default function Annotation({
const calloutElementRef = useRef<HTMLDivElement>(null);

// Callout
useEffect(() => {
useLayoutEffect(() => {
if (!annotation) return;

const callOutObj: mapkit.AnnotationCalloutDelegate = {};
Expand Down Expand Up @@ -137,6 +122,12 @@ export default function Annotation({
// @ts-expect-error
delete annotation.callout;
}

// eslint-disable-next-line consistent-return
return () => {
// @ts-expect-error
delete annotation.callout;
};
}, [
annotation,
calloutElement,
Expand Down Expand Up @@ -223,6 +214,23 @@ export default function Annotation({
forwardMapkitEvent(annotation, 'drag-end', onDragEnd, dragEndParameters);
forwardMapkitEvent(annotation, 'dragging', onDragging, draggingParameters);

// Coordinates - This needs to be the last useEffect,
// as removing the annotation needs to be the last unmount action
useLayoutEffect(() => {
if (map === null) return undefined;

const a = new mapkit.Annotation(
new mapkit.Coordinate(latitude, longitude),
() => contentEl,
);
map.addAnnotation(a);
setAnnotation(a);

return () => {
map.removeAnnotation(a);
};
}, [map, latitude, longitude]);

return (
<>
{createPortal(
Expand Down
40 changes: 23 additions & 17 deletions src/components/Marker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {
useContext, useEffect, useRef, useState,
useContext, useEffect, useLayoutEffect, useRef, useState,
} from 'react';
import { createPortal } from 'react-dom';
import MapContext from '../context/MapContext';
Expand Down Expand Up @@ -61,21 +61,6 @@ export default function Marker({
const [marker, setMarker] = useState<mapkit.MarkerAnnotation | null>(null);
const map = useContext(MapContext);

// Coordinates
useEffect(() => {
if (map === null) return undefined;

const m = new mapkit.MarkerAnnotation(
new mapkit.Coordinate(latitude, longitude),
);
map.addAnnotation(m);
setMarker(m);

return () => {
map.removeAnnotation(m);
};
}, [map, latitude, longitude]);

// Enum properties
useEffect(() => {
if (!marker) return;
Expand Down Expand Up @@ -110,7 +95,7 @@ export default function Marker({
const calloutElementRef = useRef<HTMLDivElement>(null);

// Callout
useEffect(() => {
useLayoutEffect(() => {
if (!marker) return;

const callOutObj: mapkit.AnnotationCalloutDelegate = {};
Expand Down Expand Up @@ -144,6 +129,12 @@ export default function Marker({
// @ts-expect-error
delete marker.callout;
}

// eslint-disable-next-line consistent-return
return () => {
// @ts-expect-error
delete marker.callout;
};
}, [
marker,
calloutElement,
Expand Down Expand Up @@ -235,6 +226,21 @@ export default function Marker({
forwardMapkitEvent(marker, 'drag-end', onDragEnd, dragEndParameters);
forwardMapkitEvent(marker, 'dragging', onDragging, draggingParameters);

// Coordinates
useLayoutEffect(() => {
if (map === null) return undefined;

const m = new mapkit.MarkerAnnotation(
new mapkit.Coordinate(latitude, longitude),
);
map.addAnnotation(m);
setMarker(m);

return () => {
map.removeAnnotation(m);
};
}, [map, latitude, longitude]);

return createPortal(
<div style={{ display: 'none' }}>
{(calloutContent !== undefined) && (
Expand Down

0 comments on commit 9a48478

Please sign in to comment.