allow popup freezing [#282] (#378)

* allow popup freezing [#282]

* formatting
This commit is contained in:
Brandon Liu
2024-03-15 12:01:40 +08:00
committed by GitHub
parent 2422839816
commit 1527d57d23

View File

@@ -33,6 +33,8 @@ const MapContainer = styled("div", {
const PopupContainer = styled("div", {
color: "black",
maxHeight: "400px",
overflowY: "scroll",
});
const FeatureRow = styled("div", {
@@ -344,6 +346,10 @@ function MaplibreMap(props: { file: PMTiles; mapHashPassed: boolean }) {
const [layersVisibility, setLayersVisibility] = useState<LayerVisibility[]>(
[]
);
const [popupFrozen, setPopupFrozen] = useState<boolean>(false);
const popupFrozenRef = useRef<boolean>();
popupFrozenRef.current = popupFrozen;
const mapRef = useRef<maplibregl.Map | null>(null);
const hoveredFeaturesRef = useRef<Set<MapGeoJSONFeature>>(new Set());
@@ -410,6 +416,9 @@ function MaplibreMap(props: { file: PMTiles; mapHashPassed: boolean }) {
mapRef.current = map;
map.on("mousemove", (e) => {
if (popupFrozenRef.current) {
return;
}
const hoveredFeatures = hoveredFeaturesRef.current;
for (const feature of hoveredFeatures) {
map.setFeatureState(feature, { hover: false });
@@ -448,6 +457,10 @@ function MaplibreMap(props: { file: PMTiles; mapHashPassed: boolean }) {
}
});
map.on("click", (e) => {
setPopupFrozen((p) => !p);
});
return () => {
map.remove();
};