How to implement Mapbox Controls?

@Lovely_Jaca
I not sure, but after analysis the Yext component and the component don’t provides the map instance, only on PinComponent, I did a copy from the original component here:

and

interface

export interface CustomMapboxMapProps<T> extends MapboxMapProps<T> {
    /**
     * Custom zoom controls for the map.
     * Whether to show zoom controls on the map.
     * */
    zoomControls?: boolean
}

first useEffect interaction

    useEffect(() => {
        if (mapContainer.current && !map.current) {
            const options: mapboxgl.MapboxOptions = {
                container: mapContainer.current,
                style: 'mapbox://styles/mapbox/streets-v11',
                center: [-74.005371, 40.741611],
                zoom: 9,
                ...mapboxOptions,
            }
            map.current = new mapboxgl.Map(options)
            const mapbox = map.current
            if (zoomControls) {
                mapbox.addControl(new mapboxgl.NavigationControl(), 'top-right')
            }
            mapbox.resize()
            if (onDragDebounced) {
                mapbox.on('drag', () => {
                    onDragDebounced(mapbox.getCenter(), mapbox.getBounds())
                })
            }
        }
    }, [mapboxOptions, onDragDebounced])

Result

Full solution here: Custom Yext MapboxMap · GitHub

useDebouncedFunction( it don’t export from Yext)

1 Like