Skip to content

Commit

Permalink
Merge pull request #93 from dimfeld/deckgl-click
Browse files Browse the repository at this point in the history
Fix deck.gl layer click handler
  • Loading branch information
dimfeld authored Oct 23, 2023
2 parents a1ac10f + 4357161 commit 7a5717e
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';
}

1 comment on commit 7a5717e

@vercel
Copy link

@vercel vercel bot commented on 7a5717e Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.