Standard Toolkit
Toolkits@accelint/map-toolkitDeckGL

BaseMap

Deck.gl-powered base map with MapLibre GL integration, event bus coordination, and mode management.

Usage

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

// Create id at module level for stability and easy sharing
const MAIN_MAP_ID = uuid();

export function MapView() {
  return <BaseMap className="w-full h-full" id={MAIN_MAP_ID} />;
}

Reference

interface BaseMapProps extends Omit<DeckglProps, 'onLoad'> {
  // Required
  id: UniqueId;
  
  // Layout
  className?: string;
  
  // Map configuration
  defaultView?: '2D' | '2.5D' | '3D';
  styleUrl?: string;
  initialViewState?: Partial<ViewState>;
  mapLibreOptions?: MapLibreOptions;
  
  // Interaction
  enableControlEvents?: boolean;
  boxZoom?: boolean;
  enableRbz?: boolean;
  rbzOptions?: RbzOptions;
  
  // Event handlers
  onClick?: (info: PickingInfo, event: MjolnirGestureEvent) => void;
  onHover?: (info: PickingInfo, event: MjolnirPointerEvent) => void;
  onDragStart?: (info: PickingInfo, event: MjolnirGestureEvent) => void;
  onDrag?: (info: PickingInfo, event: MjolnirGestureEvent) => void;
  onDragEnd?: (info: PickingInfo, event: MjolnirGestureEvent) => void;
  onViewStateChange?: (params: ViewStateChangeParameters) => void;
  onLoad?: () => void;
  
  // All other Deck.gl props (controller, parameters, layers, etc.)
}

Props

PropTypeDefaultRequired
idUniqueId-Yes
classNamestring-No
childrenReact.ReactNode-No
defaultView'2D' | '2.5D' | '3D''2D'No
styleUrlstringDark Matter styleNo
initialViewStatePartial<ViewState>{ zoom: 2, lat: 0, lon: 0 }No
enableControlEventsbooleantrueNo
boxZoombooleantrue (unless RBZ enabled)No
enableRbzbooleanfalseNo
rbzOptionsRbzOptions-No
onClick(info, event) => void-No
onHover(info, event) => void-No
onDragStart(info, event) => void-No
onDrag(info, event) => void-No
onDragEnd(info, event) => void-No
onViewStateChange(params) => void-No
onLoad() => void-No
mapLibreOptionsMapLibreOptions-No

id

Required. Unique identifier for this map instance. Used to isolate map mode state between multiple map instances (e.g., main map vs minimap).

Must be a UUID generated using uuid() from @accelint/core. Should be a module-level constant for stability and easy sharing with useMapMode().

defaultView

Initial view mode for the map:

  • '2D' - Flat map with no pitch (default)
  • '2.5D' - Slight pitch for depth perception
  • '3D' - Full 3D navigation with pitch and rotation

styleUrl

MapLibre style URL for base map tiles. Defaults to Carto's Dark Matter style.

Common options:

  • 'https://tiles.basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json' (default)
  • 'https://tiles.basemaps.cartocdn.com/gl/positron-gl-style/style.json' (light)
  • Custom Mapbox or MapLibre style URLs

enableRbz & rbzOptions

Enable rubber band zoom (Shift + drag to select area for zoom). Automatically disables boxZoom when enabled.

Important: These options are read once on map load. To change dynamically, update the map's key prop to force a remount.

type RbzOptions = {
  fillColor?: string;
  strokeColor?: string;
  strokeWidth?: number;
  shouldActivate?: () => boolean;
};

mapLibreOptions

Additional MapLibre MapOptions forwarded to the underlying map (e.g., transformRequest, maxBounds, minZoom, maxZoom).

Keys BaseMap manages internally are stripped:

  • Camera/view state: center, zoom, pitch, bearing, projection, bounds, fitBoundsOptions
  • Controller gestures: doubleClickZoom, dragRotate, pitchWithRotate, rollEnabled, boxZoom
  • WebGL context: canvasContextAttributes
  • Already exposed props: style, container, maxPitch

onLoad

Fires once when the MapLibre map has loaded, after BaseMap applies the projection and finishes its own load work. Typed () => void; it receives no arguments.

BaseMap runs it from MapLibre's load event, not deck's onLoad prop. It is not forwarded to the deck overlay: deck may not be initialized yet when it fires, so do not call deck viewport methods from it. (An onLoad passed through the overlay could overwrite the overlay's own load wrapper and freeze deck's picking viewport at the initial frame.)

onDragStart, onDrag & onDragEnd

Standard Deck.gl drag handlers, called first with the full PickingInfo and gesture event. After each handler runs, BaseMap emits the matching map:dragStart / map:drag / map:dragEnd bus event for plain drags; see Drag events.

Map Mode Integration

BaseMap automatically creates a MapProvider internally, setting up map mode state management for this instance.

Children vs Siblings:

  • Children: Only Deck.gl layer components can be children. They use useMapMode() without parameters (context-based).
  • Siblings: UI components (buttons, toolbars) must be siblings and pass id to useMapMode(id).
// ✅ Correct: Deck.gl layer as child
<BaseMap id={mapId}>
  <ScatterplotLayer {...props} />
</BaseMap>

// ✅ Correct: UI as sibling
<>
  <Toolbar mapId={mapId} />
  <BaseMap id={mapId} />
</>

// ❌ Wrong: UI as child
<BaseMap id={mapId}>
  <Toolbar /> {/* Won't render - not a Deck.gl layer */}
</BaseMap>

Event Bus

Click, hover, viewport, and drag events are emitted through the event bus with id included in the payload, allowing multiple map instances to coexist without interference.

// Events
MapEvents.click     // MapClickEvent
MapEvents.hover     // MapHoverEvent
MapEvents.viewport  // MapViewportEvent
MapEvents.dragStart // MapDragStartEvent
MapEvents.drag      // MapDragEvent
MapEvents.dragEnd   // MapDragEndEvent

// Control events
MapEvents.enablePan
MapEvents.disablePan
MapEvents.enableZoom
MapEvents.disableZoom

Drag events

map:dragStart and map:drag carry a MapDragPayload; map:dragEnd carries a MapDragEndPayload. The modifier flags let subscribers decide whether to claim a gesture (the Measurement Layer's useMeasurement hook is built on these events).

type MapDragPayload = {
  id: UniqueId;
  coordinate: [number, number]; // Unprojected [longitude, latitude] at the drag position
  shiftKey: boolean;
  ctrlKey: boolean;
  altKey: boolean;
};

type MapDragEndPayload = Omit<MapDragPayload, 'coordinate'> & {
  coordinate: [number, number] | null; // null when the release position is off the globe
};
  • map:dragStart and map:drag fire after the corresponding onDragStart / onDrag prop, and only when the pointer position unprojects to finite map coordinates. Drags that start or move off the globe are skipped, so coordinate is always a finite tuple.
  • map:dragEnd is always emitted for a plain drag, so subscribers that claimed the gesture can always release it. Its coordinate is null when the release position does not unproject to finite coordinates (for example, released off the globe in 3D view); check for null before using it.
  • Tilt gestures (right-drag, or Ctrl + left-drag) rotate and pitch the camera and emit none of the three drag events. ctrlKey is therefore only true for other button combinations.

The payload types and their Payload wrappers (MapDragPayload, MapDragEndPayload, MapDragStartEvent, MapDragEvent, MapDragEndEvent) are exported from @accelint/map-toolkit/deckgl alongside the other Map* event types.

Control events

BaseMap listens for control events when enableControlEvents is true and applies them to the MapLibre map for the id in the payload:

  • map:disablePan disables MapLibre's dragPan handler; map:enablePan re-enables it.
  • map:disableZoom turns off scroll zoom and native box zoom and, when enableRbz is set, stops the rubber band zoom handler listening for Shift.
  • map:enableZoom restores only what BaseMap configured: scroll zoom always, native box zoom only when the boxZoom prop resolves to true (it is false while enableRbz is set), and RBZ listening only if map:disableZoom stopped it. Double-click zoom is never touched; BaseMap locks it off.

BaseMap passes its resolved boxZoom to the internal MapControls component, which accepts an optional boxZoom prop (default true) for this purpose.

Inherited Props

BaseMap inherits all props from Deck.gl's DeckglProps, including:

  • controller - Enable/disable map controls
  • interleaved - Interleaved rendering with MapLibre
  • parameters - WebGL parameters
  • widgets - Deck.gl widgets
  • useDevicePixels - High-DPI rendering
  • pickingRadius - Picking tolerance in pixels

See Deck.gl Documentation for full API.

Examples

Example: Basic map with id

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

// Module-level constant for stability
const MAIN_MAP_ID = uuid();

export function SimpleMap() {
  return (
    <div className="w-full h-screen">
      <BaseMap id={MAIN_MAP_ID} className="w-full h-full" />
    </div>
  );
}

Example: With event handlers and mode management

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { useMapMode } from '@accelint/map-toolkit/map-mode';
import { uuid } from '@accelint/core';
import type { PickingInfo } from '@deck.gl/core';
import type { MjolnirGestureEvent } from 'mjolnir.js';

const MAIN_MAP_ID = uuid();

function Toolbar() {
  const { mode, requestModeChange } = useMapMode(MAIN_MAP_ID);
  
  return (
    <div className="absolute top-4 left-4 z-10">
      <button onClick={() => requestModeChange('default', 'toolbar')}>
        Navigate
      </button>
      <button onClick={() => requestModeChange('drawing', 'toolbar')}>
        Draw
      </button>
      <div>Current mode: {mode}</div>
    </div>
  );
}

export function InteractiveMap() {
  const handleClick = (info: PickingInfo, event: MjolnirGestureEvent) => {
    if (info.object) {
      console.log('Clicked:', info.object);
    }
  };
  
  const handleHover = (info: PickingInfo) => {
    console.log('Hovering:', info.coordinate);
  };
  
  return (
    <div className="relative w-full h-screen">
      <Toolbar />
      <BaseMap
        id={MAIN_MAP_ID}
        className="w-full h-full"
        onClick={handleClick}
        onHover={handleHover}
      />
    </div>
  );
}

Example: With custom initial view

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

const MAP_ID = uuid();

export function CenteredMap() {
  return (
    <BaseMap
      id={MAP_ID}
      className="w-full h-full"
      defaultView="2.5D"
      initialViewState={{
        latitude: 37.7749,
        longitude: -122.4194,
        zoom: 12,
      }}
    />
  );
}

Example: With Deck.gl layers

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { ScatterplotLayer } from '@deck.gl/layers';
import { uuid } from '@accelint/core';

const MAP_ID = uuid();

const data = [
  { position: [-122.4, 37.8], name: 'San Francisco' },
  { position: [-74.0, 40.7], name: 'New York' },
];

export function MapWithLayers() {
  return (
    <BaseMap id={MAP_ID} className="w-full h-full">
      <ScatterplotLayer
        id="cities"
        data={data}
        getPosition={(d) => d.position}
        getRadius={10000}
        getFillColor={[255, 0, 0]}
      />
    </BaseMap>
  );
}

Example: Listening to viewport changes

import { useOn } from '@accelint/bus/react';
import { MapEvents } from '@accelint/map-toolkit/deckgl';
import type { MapViewportEvent } from '@accelint/map-toolkit/deckgl';
import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

const MAP_ID = uuid();

function ViewportLogger() {
  useOn<MapViewportEvent>(MapEvents.viewport, (event) => {
    if (event.payload.id === MAP_ID) {
      console.log('Viewport:', {
        zoom: event.payload.zoom,
        bounds: event.payload.bounds,
        center: [event.payload.latitude, event.payload.longitude],
      });
    }
  });
  
  return null;
}

export function TrackedMap() {
  return (
    <>
      <ViewportLogger />
      <BaseMap id={MAP_ID} className="w-full h-full" />
    </>
  );
}

Example: Listening to drag events

import { useOn } from '@accelint/bus/react';
import { BaseMap, MapEvents } from '@accelint/map-toolkit/deckgl';
import type { MapDragEndEvent, MapDragStartEvent } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

const MAP_ID = uuid();

function DragLogger() {
  useOn<MapDragStartEvent>(MapEvents.dragStart, (event) => {
    if (event.payload.id === MAP_ID && event.payload.altKey) {
      console.log('Alt+drag started at', event.payload.coordinate);
    }
  });

  useOn<MapDragEndEvent>(MapEvents.dragEnd, (event) => {
    if (event.payload.id !== MAP_ID || event.payload.coordinate === null) {
      return; // other map, or released off the globe
    }

    const [longitude, latitude] = event.payload.coordinate;
    console.log('Drag ended at', longitude, latitude);
  });

  return null;
}

export function DragTrackedMap() {
  return (
    <>
      <DragLogger />
      <BaseMap id={MAP_ID} className="w-full h-full" />
    </>
  );
}

Example: Running setup once the map has loaded

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

const MAP_ID = uuid();

export function LoadedMap() {
  return (
    <BaseMap
      id={MAP_ID}
      className="w-full h-full"
      onLoad={() => {
        console.log('MapLibre loaded; projection applied');
      }}
    />
  );
}

Good to know: onLoad fires from MapLibre's load event. Deck may not be initialized yet at that point, so do not call deck viewport methods from it.

Example: Multiple map instances

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

const MAIN_MAP_ID = uuid();
const MINI_MAP_ID = uuid();

export function DualMapView() {
  return (
    <div className="flex h-screen">
      <div className="flex-1">
        <BaseMap id={MAIN_MAP_ID} className="w-full h-full" />
      </div>
      <div className="w-64">
        <BaseMap
          id={MINI_MAP_ID}
          className="w-full h-full"
          initialViewState={{ zoom: 0 }}
        />
      </div>
    </div>
  );
}

Example: With rubber band zoom

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

const MAP_ID = uuid();

export function RbzMap() {
  return (
    <BaseMap
      id={MAP_ID}
      className="w-full h-full"
      enableRbz={true}
      rbzOptions={{
        fillColor: 'rgba(0, 120, 255, 0.1)',
        strokeColor: '#0078ff',
        strokeWidth: 2,
      }}
    />
  );
}

Good to know: When enableRbz is true, boxZoom is automatically disabled to prevent gesture conflicts.

Example: Custom MapLibre options

import { BaseMap } from '@accelint/map-toolkit/deckgl';
import { uuid } from '@accelint/core';

const MAP_ID = uuid();

export function RestrictedMap() {
  return (
    <BaseMap
      id={MAP_ID}
      className="w-full h-full"
      mapLibreOptions={{
        maxBounds: [[-130, 20], [-60, 55]], // Restrict to US
        minZoom: 4,
        maxZoom: 18,
        transformRequest: (url, resourceType) => {
          // Add auth headers for custom tile server
          if (url.startsWith('https://tiles.example.com/')) {
            return {
              url,
              headers: { Authorization: `Bearer ${token}` },
            };
          }
          return { url };
        },
      }}
    />
  );
}

On this page