⚠️ Early Stage Project: This library is not production-ready yet. Developers should use it with caution and expect breaking changes.

Hooks

React hooks for controlling map components and accessing the Mapbox GL instance.

Available Hooks

useMap

Access the Mapbox GL map instance and loading state. Must be used within a Map component.

import { useMap } from "@/registry/map";

const MyComponent = () => {
  const { map, isLoaded } = useMap();

  const flyToLocation = () => {
    if (!map) return;

    map.flyTo({
      center: [-74.006, 40.7128],
      zoom: 14,
    });
  };

  return (
    <button onClick={flyToLocation} disabled={!isLoaded}>
      Fly to NYC
    </button>
  );
};
Return ValueTypeDescription
mapmapboxgl.Map | nullThe Mapbox GL map instance
isLoadedbooleanWhether the map has finished loading

useVideoControl

Control video layer playback. Pass the video layer ID to control a specific MapVideoLayer component.

import { useVideoControl } from "@/registry/map";

const VideoControls = () => {
  const { play, pause, toggle, isPlaying } = useVideoControl("my-video-layer");

  return (
    <div className="flex gap-2">
      <button onClick={play}>Play</button>
      <button onClick={pause}>Pause</button>
      <button onClick={toggle}>
        {isPlaying ? "Pause" : "Play"}
      </button>
    </div>
  );
};
ParameterTypeDescription
layerIdstringThe ID of the video layer to control
Return ValueTypeDescription
play() => voidStart video playback
pause() => voidPause video playback
toggle() => voidToggle between play and pause
isPlayingbooleanCurrent playback state

useLineAnimatedControl

Control animated line playback state. Use with MapLineAnimated component.

import { useLineAnimatedControl, MapLineAnimated } from "@/registry/map";

const AnimatedRoute = () => {
  const { isPlaying, start, stop, toggle } = useLineAnimatedControl();

  return (
    <>
      <MapLineAnimated
        id="route"
        path={routePath}
        autoStart={isPlaying}
      />
      <button onClick={toggle}>
        {isPlaying ? "Stop" : "Start"} Animation
      </button>
    </>
  );
};
Return ValueTypeDescription
start() => voidStart the animation
stop() => voidStop the animation
toggle() => voidToggle animation state
isPlayingbooleanCurrent animation state

useMarkerAnimatedControl

Control animated marker playback state. Use with MapMarkerAnimated component.

import { useMarkerAnimatedControl, MapMarkerAnimated } from "@/registry/map";

const AnimatedMarker = () => {
  const { isPlaying, start, stop, toggle } = useMarkerAnimatedControl();

  return (
    <>
      <MapMarkerAnimated
        id="marker"
        coordinates={pathCoordinates}
        autoStart={isPlaying}
      />
      <button onClick={toggle}>
        {isPlaying ? "Stop" : "Start"} Marker
      </button>
    </>
  );
};
Return ValueTypeDescription
start() => voidStart the animation
stop() => voidStop the animation
toggle() => voidToggle animation state
isPlayingbooleanCurrent animation state