Skip to content

Commit 13d8829

Browse files
committed
Cleanup various event listeners.
Also make sure localizeMap() is executed when map style changes as well. TEST=manual verified map is localized upon style changes as well
1 parent 2f253c4 commit 13d8829

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yext/search-ui-react",
3-
"version": "1.8.9",
3+
"version": "1.8.10",
44
"description": "A library of React Components for powering Yext Search integrations",
55
"author": "watson@yext.com",
66
"license": "BSD-3-Clause",

src/components/MapboxMap.tsx

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,33 @@ export function MapboxMap<T>({
163163
// keep track of the previous value of mapboxOptions across renders
164164
const prevMapboxOptions = useRef(mapboxOptions);
165165

166+
const localizeMap = useCallback(() => {
167+
const mapbox = map.current;
168+
if (!mapbox || !locale) return;
169+
170+
const localizeLabels = () => {
171+
mapbox.getStyle().layers.forEach(layer => {
172+
if (layer.type === "symbol" && layer.layout?.["text-field"]) {
173+
mapbox.setLayoutProperty(
174+
layer.id,
175+
"text-field",
176+
[
177+
'coalesce',
178+
['get', `name_${getMapboxLanguage(locale)}`],
179+
['get', 'name']
180+
]
181+
);
182+
}
183+
});
184+
}
185+
186+
if (mapbox.isStyleLoaded()) {
187+
localizeLabels();
188+
} else {
189+
mapbox.once("styledata", () => localizeLabels())
190+
}
191+
}, [locale]);
192+
166193
useEffect(() => {
167194
if (mapContainer.current) {
168195
if (map.current && allowUpdates) {
@@ -199,37 +226,21 @@ export function MapboxMap<T>({
199226
onDragDebounced(mapbox.getCenter(), mapbox.getBounds());
200227
}
201228
});
229+
return () => {
230+
mapbox.off('drag', () => {
231+
onDragDebounced(mapbox.getCenter(), mapbox.getBounds());
232+
});
233+
mapbox.off('zoom', (e) => {
234+
if (e.originalEvent) {
235+
onDragDebounced(mapbox.getCenter(), mapbox.getBounds());
236+
}
237+
});
238+
};
202239
}
203240
}
204-
}
205-
}, [mapboxOptions, onDragDebounced]);
206-
207-
useEffect(() => {
208-
const mapbox = map.current;
209-
if (!mapbox || !locale) return;
210-
211-
const localizeMap = () => {
212-
mapbox.getStyle().layers.forEach(layer => {
213-
if (layer.type === "symbol" && layer.layout?.["text-field"]) {
214-
mapbox.setLayoutProperty(
215-
layer.id,
216-
"text-field",
217-
[
218-
'coalesce',
219-
['get', `name_${getMapboxLanguage(locale)}`],
220-
['get', 'name']
221-
]
222-
);
223-
}
224-
});
225-
}
226-
227-
if (mapbox.isStyleLoaded()) {
228241
localizeMap();
229-
} else {
230-
mapbox.once("styledata", () => localizeMap())
231242
}
232-
}, [locale]);
243+
}, [mapboxOptions, onDragDebounced, localizeMap]);
233244

234245
useEffect(() => {
235246
if (iframeWindow && map.current) {
@@ -290,8 +301,14 @@ export function MapboxMap<T>({
290301
maxZoom: mapboxOptions?.maxZoom ?? 15
291302
});
292303
}
304+
305+
return () => {
306+
markers.current.forEach((marker, i) => {
307+
marker?.getElement().removeEventListener('click', () => handlePinClick(locationResults[i]));
308+
});
309+
}
293310
}
294-
}, [PinComponent, getCoordinate, locationResults, selectedResult, markerOptionsOverride]);
311+
}, [PinComponent, getCoordinate, locationResults, selectedResult, markerOptionsOverride, locale]);
295312

296313
return (
297314
<div ref={mapContainer} className='h-full w-full' />

0 commit comments

Comments
 (0)