@@ -163,6 +163,33 @@ export function MapboxMap<T>({
163
163
// keep track of the previous value of mapboxOptions across renders
164
164
const prevMapboxOptions = useRef ( mapboxOptions ) ;
165
165
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
+
166
193
useEffect ( ( ) => {
167
194
if ( mapContainer . current ) {
168
195
if ( map . current && allowUpdates ) {
@@ -199,37 +226,21 @@ export function MapboxMap<T>({
199
226
onDragDebounced ( mapbox . getCenter ( ) , mapbox . getBounds ( ) ) ;
200
227
}
201
228
} ) ;
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
+ } ;
202
239
}
203
240
}
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 ( ) ) {
228
241
localizeMap ( ) ;
229
- } else {
230
- mapbox . once ( "styledata" , ( ) => localizeMap ( ) )
231
242
}
232
- } , [ locale ] ) ;
243
+ } , [ mapboxOptions , onDragDebounced , localizeMap ] ) ;
233
244
234
245
useEffect ( ( ) => {
235
246
if ( iframeWindow && map . current ) {
@@ -290,8 +301,14 @@ export function MapboxMap<T>({
290
301
maxZoom : mapboxOptions ?. maxZoom ?? 15
291
302
} ) ;
292
303
}
304
+
305
+ return ( ) => {
306
+ markers . current . forEach ( ( marker , i ) => {
307
+ marker ?. getElement ( ) . removeEventListener ( 'click' , ( ) => handlePinClick ( locationResults [ i ] ) ) ;
308
+ } ) ;
309
+ }
293
310
}
294
- } , [ PinComponent , getCoordinate , locationResults , selectedResult , markerOptionsOverride ] ) ;
311
+ } , [ PinComponent , getCoordinate , locationResults , selectedResult , markerOptionsOverride , locale ] ) ;
295
312
296
313
return (
297
314
< div ref = { mapContainer } className = 'h-full w-full' />
0 commit comments