Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: onMouseEnter & onMouseLeave trigger between joined layers (#1671) #2126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is very inefficient. Use new Set(ids).size

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

).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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this correct, though? What if the layer count is the same but one layer is replaced by another?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

don't know if it's ok to call pointEvents twice in one callback

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