Skip to content

Commit dafe37c

Browse files
committed
separate pointermove handler into own effect to avoid accumulating listeners
1 parent 8b65ce4 commit dafe37c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/layers/UseBackgroundLayer.tsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,24 @@ export default function useBackgroundLayer(map: Map, styleOption: StyleOption) {
99
useEffect(() => {
1010
removeCurrentBackgroundLayers(map)
1111
addNewBackgroundLayers(map, styleOption)
12-
setupMouseInteraction(map)
1312
return () => {
1413
removeCurrentBackgroundLayers(map)
1514
}
1615
}, [map, styleOption])
16+
17+
// Pointer cursor over interactive features — registered once, independent of style changes
18+
useEffect(() => {
19+
const onPointerMove = (evt: any) => {
20+
if (evt.dragging) return // skip expensive hit-test while panning
21+
const features = map.getFeaturesAtPixel(evt.pixel)
22+
const atFeature = features.some(f => f instanceof Feature)
23+
map.getTargetElement().style.cursor = atFeature ? 'pointer' : 'default'
24+
}
25+
map.on('pointermove', onPointerMove)
26+
return () => {
27+
map.un('pointermove', onPointerMove)
28+
}
29+
}, [map])
1730
}
1831

1932
function removeCurrentBackgroundLayers(map: Map) {
@@ -45,12 +58,3 @@ function addNewBackgroundLayers(map: Map, styleOption: StyleOption) {
4558
map.addLayer(tileLayer)
4659
}
4760
}
48-
49-
function setupMouseInteraction(map: Map) {
50-
map.on('pointermove', function (evt) {
51-
const features = map.getFeaturesAtPixel(evt.pixel)
52-
// features can also contain 'RenderFeatures' for vector tiles, but in this case the cursor should not change
53-
const atFeature = features.some(f => f instanceof Feature)
54-
map.getTargetElement().style.cursor = atFeature ? 'pointer' : 'default'
55-
})
56-
}

0 commit comments

Comments
 (0)