Skip to content
This repository has been archived by the owner on Apr 21, 2024. It is now read-only.

Commit

Permalink
Fix MapPopup so it displays properly.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlmcneary2 committed May 9, 2023
1 parent 55cc1e2 commit 2274682
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
2 changes: 1 addition & 1 deletion libs/remapgl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react"
],
"name": "remapgl",
"version": "2.4.0",
"version": "2.4.1",
"license": "MIT",
"dependencies": {
"lodash": "^4.17.21"
Expand Down
29 changes: 11 additions & 18 deletions libs/remapgl/src/lib/popup/map-popup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef } from "react";
import React, { useEffect, useRef, useState } from "react";
import {
LngLatLike,
Popup as PopupGL,
Expand All @@ -20,41 +20,34 @@ export function MapPopup({
onClose,
options
}: React.PropsWithChildren<MapPopupProps>) {
const ref = useRef<HTMLElement>(null);
const popup = useRef<PopupGL>(null);
const [popupRef, setPopupRef] = useState<HTMLElement | null>(null);
const popupGL = useRef<PopupGL | null>(null);
const { mapGL } = useMapGL();

useEffect(() => {
if (!ref.current || !lngLat || !!popup.current) {
if (!popupRef || !lngLat || !!popupGL.current) {
return;
}

const nextPopup = new PopupGL(options ?? {})
.setDOMContent(ref.current)
.setDOMContent(popupRef)
.on("close", (...args) => {
onClose && onClose(...args);
})
.addTo(mapGL);
.addTo(mapGL)
.setLngLat(lngLat);

popup.current = nextPopup;
popupGL.current = nextPopup;

obj && obj(nextPopup);

return () => {
nextPopup.remove();
popup.current = null;
popupGL.current = null;
};
}, [lngLat, mapGL, obj, onClose, options]);
}, [lngLat, mapGL, obj, onClose, options, popupGL, popupRef]);

useEffect(() => {
if (!popup.current) {
return;
}

lngLat && popup.current.setLngLat(lngLat);
}, [lngLat, popup]);

return <Popup ref={ref}>{children}</Popup>;
return <Popup ref={setPopupRef}>{children}</Popup>;
}

/**
Expand Down

0 comments on commit 2274682

Please sign in to comment.