Skip to content

Commit

Permalink
Fix deck.gl layer click handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dimfeld committed Oct 23, 2023
1 parent a1ac10f commit 4357161
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-mugs-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-maplibre': patch
---

Fix exception when clicking a deck.gl layer with a Popup
24 changes: 17 additions & 7 deletions src/lib/Popup.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
type MapLayerTouchEvent,
} from 'maplibre-gl';
import { onDestroy, onMount, tick } from 'svelte';
import { mapContext, type DeckGlMouseEvent, type LayerEvent } from './context.js';
import {
mapContext,
type DeckGlMouseEvent,
type LayerEvent,
isDeckGlMouseEvent,
} from './context.js';
/** Show the built-in close button. By default the close button will be shown
* only if closeOnClickOutside and closeOnClickInside are not set. */
Expand Down Expand Up @@ -167,18 +172,23 @@
};
});
function skipHandlingEvent(e: MapLayerMouseEvent) {
if (!openIfTopMost) {
return false;
}
// Marker clicks are always only on the top-most marker. Otherwise check for the top-most layer.
return !('marker' in e) && !isDeckGlMouseEvent(e) && eventTopMost(e) !== $layer;
}
let features: Feature[] | null = null;
let touchOpenState: 'normal' | 'opening' | 'justOpened' = 'normal';
function handleLayerClick(e: MapLayerMouseEvent | LayerEvent) {
if (e.type !== openOn) {
return;
}
if (openIfTopMost) {
// Marker clicks are always only on the top-most marker. Otherwise check for the top-most layer.
if (!('marker' in e) && eventTopMost(e) !== $layer) {
return;
}
if (skipHandlingEvent(e)) {
return;
}
if ('layerType' in e) {
Expand Down Expand Up @@ -238,7 +248,7 @@
return;
}
if (openIfTopMost && eventTopMost(e) !== $layer) {
if (skipHandlingEvent(e)) {
open = false;
features = null;
return;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,9 @@ export function updatedZoomRangeContext(
maxzoom,
};
}

export function isDeckGlMouseEvent(
event: MapMouseEvent | DeckGlMouseEvent
): event is DeckGlMouseEvent<unknown> {
return 'layerType' in event && event.layerType === 'deckgl';
}

0 comments on commit 4357161

Please sign in to comment.