Skip to content

Commit 7b2d500

Browse files
committed
[front-end] Toggle popup when marker clicked
1 parent 7429f68 commit 7b2d500

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

apps/front-end/src/components/map/mapLibre.ts

+17
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ const openPopup = async (
4141
popupClosedCallback: () => void,
4242
offset?: [number, number],
4343
) => {
44+
if (popup?.isOpen() && popupIx === itemIx) {
45+
console.log(`Popup for item @${itemIx} already open`);
46+
return;
47+
}
48+
4449
console.log(`Open popup for item @${itemIx} ${coordinates}`);
4550

4651
// Shift the popup up a bit so it doesn't cover the marker
@@ -250,6 +255,16 @@ export const createMap = (
250255
const coordinates = feature.geometry.coordinates.slice();
251256
const itemIx = feature.properties?.ix;
252257

258+
if (popup?.isOpen() && popupIx === itemIx) {
259+
console.log(
260+
`Popup for item @${itemIx} already open so toggle closed`,
261+
);
262+
popup?.remove();
263+
popupIx = undefined;
264+
popup = undefined;
265+
return;
266+
}
267+
253268
// ease to a position so that the popup is fully visible
254269
map
255270
.easeTo({
@@ -339,6 +354,8 @@ export const createMap = (
339354
// Remove previous popup - remove listener to prevent looping back and confusing React code
340355
popup?.off("close", popupClosedCallback);
341356
popup?.remove();
357+
popupIx = undefined;
358+
popup = undefined;
342359
flyToThenOpenPopupRecursive();
343360
});
344361

0 commit comments

Comments
 (0)