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 27, 2023
1 parent 109c334 commit 1d26d3f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/mapbox/mapbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,14 +786,23 @@ export default class Mapbox {
}
};

_uniqueLayersCount(features: MapboxGeoJSONFeature[] | null) {
if (!features) {
return 0;
}
return features.filter(
(feature, index) => features.findIndex(f => f.layer.id === feature.layer.id) === index
).length;
}

_updateHover(e: MapMouseEvent) {
const {props} = this;
const shouldTrackHoveredFeatures =
props.interactiveLayerIds && (props.onMouseMove || props.onMouseEnter || props.onMouseLeave);

if (shouldTrackHoveredFeatures) {
const eventType = e.type;
const wasHovering = this._hoveredFeatures?.length > 0;
const hoveredLayersCount = this._uniqueLayersCount(this._hoveredFeatures);
let features;
if (eventType === 'mousemove') {
try {
Expand All @@ -806,14 +815,14 @@ export default class Mapbox {
} else {
features = [];
}
const isHovering = features.length > 0;
const hoveringLayersCount = this._uniqueLayersCount(features);

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

0 comments on commit 1d26d3f

Please sign in to comment.