Skip to content

Commit

Permalink
fix: onMouseEnter & onMouseLeave trigger between joined layers (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jungzl committed Feb 23, 2023
1 parent 109c334 commit a9772e6
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/mapbox/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -793,27 +793,32 @@ export default class Mapbox {

if (shouldTrackHoveredFeatures) {
const eventType = e.type;
const wasHovering = this._hoveredFeatures?.length > 0;
const hoveredFeaturesLen = this._hoveredFeatures?.length || 0;
let features;
if (eventType === 'mousemove') {
try {
features = this._map.queryRenderedFeatures(e.point, {
layers: props.interactiveLayerIds
});
features = this._map
.queryRenderedFeatures(e.point, {
layers: props.interactiveLayerIds
})
.filter(
(feature, index, array) =>
array.findIndex(f => f.layer.id === feature.layer.id) === index
);
} catch {
features = [];
}
} else {
features = [];
}
const isHovering = features.length > 0;
const hoveringFeatureLen = features.length;

if (!isHovering && wasHovering) {
if (hoveringFeatureLen < hoveredFeaturesLen) {
e.type = 'mouseleave';
this._onPointerEvent(e);
}
this._hoveredFeatures = features;
if (isHovering && !wasHovering) {
if (hoveringFeatureLen > hoveredFeaturesLen) {
e.type = 'mouseenter';
this._onPointerEvent(e);
}
Expand Down

0 comments on commit a9772e6

Please sign in to comment.