← Back to Skills Marketplace
🔌

Mapbox Web Integration Patterns

by Mapbox · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ Security Clean
107
Downloads
0
Stars
0
Active Installs
1
Versions
Install in OpenClaw
/install mapbox-web-integration-patterns
Description
Official integration patterns for Mapbox GL JS across popular web frameworks (React, Vue, Svelte, Angular). Covers setup, lifecycle management, token handlin...
README (SKILL.md)

Mapbox Integration Patterns Skill

This skill provides official patterns for integrating Mapbox GL JS into web applications using React, Vue, Svelte, Angular, and vanilla JavaScript. These patterns are based on Mapbox's create-web-app scaffolding tool and represent production-ready best practices.

Version Requirements

Mapbox GL JS

Recommended: v3.x (latest)

  • Minimum: v3.0.0
  • Why v3.x: Modern API, improved performance, active development
  • v2.x: Legacy; no longer actively developed (see migration notes below)

Installing via npm (recommended for production):

npm install mapbox-gl@^3.0.0    # Installs latest v3.x

CDN (for prototyping only):

\x3C!-- Replace VERSION with latest v3.x from https://docs.mapbox.com/mapbox-gl-js/ -->
\x3Cscript src="https://api.mapbox.com/mapbox-gl-js/vVERSION/mapbox-gl.js">\x3C/script>
\x3Clink href="https://api.mapbox.com/mapbox-gl-js/vVERSION/mapbox-gl.css" rel="stylesheet" />

Framework Requirements

React: GL JS works with React 16.8+ (requires hooks). create-web-app scaffolds with React 19.x. Vue: GL JS works with Vue 2.x+ (Vue 3 Composition API recommended). Svelte: GL JS works with any Svelte version. create-web-app scaffolds with Svelte 5.x. Angular: GL JS works with Angular 2+. create-web-app scaffolds with Angular 19.x. Next.js: Minimum 13.x (App Router), Pages Router 12.x+.

Mapbox Search JS

npm install @mapbox/search-js-react@^1.0.0      # React
npm install @mapbox/search-js-web@^1.0.0        # Other frameworks

Version Migration Notes (v2.x to v3.x)

  • WebGL 2 now required
  • optimizeForTerrain option removed
  • Improved TypeScript types, better tree-shaking support
  • No breaking changes to core initialization patterns

Token patterns (work in v2.x and v3.x):

const token = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN; // Use env vars in production

// Global token (works since v1.x)
mapboxgl.accessToken = token;
const map = new mapboxgl.Map({ container: '...' });

// Per-map token (preferred for multi-map setups)
const map = new mapboxgl.Map({
  accessToken: token,
  container: '...'
});

Core Principles

Every Mapbox GL JS integration must:

  1. Initialize the map in the correct lifecycle hook
  2. Store map instance in component state (not recreate on every render)
  3. Always call map.remove() on cleanup to prevent memory leaks
  4. Handle token management securely (environment variables)
  5. Import CSS: import 'mapbox-gl/dist/mapbox-gl.css'

React Integration (Primary Pattern)

Pattern: useRef + useEffect with cleanup

Note: These examples use Vite (the bundler used in create-web-app). If using Create React App, replace import.meta.env.VITE_MAPBOX_ACCESS_TOKEN with process.env.REACT_APP_MAPBOX_TOKEN. See Token Management Patterns for other bundlers.

import { useRef, useEffect } from 'react';
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

function MapComponent() {
  const mapRef = useRef(null); // Store map instance
  const mapContainerRef = useRef(null); // Store DOM reference

  useEffect(() => {
    mapboxgl.accessToken = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN;

    mapRef.current = new mapboxgl.Map({
      container: mapContainerRef.current,
      center: [-71.05953, 42.3629],
      zoom: 13
    });

    // CRITICAL: Cleanup to prevent memory leaks
    return () => {
      mapRef.current.remove();
    };
  }, []); // Empty dependency array = run once on mount

  return \x3Cdiv ref={mapContainerRef} style={{ height: '100vh' }} />;
}

Key points:

  • Use useRef for both map instance and container
  • Initialize in useEffect with empty deps []
  • Always return cleanup function that calls map.remove()
  • Never initialize map in render (causes infinite loops)

React + Search JS

import { useRef, useEffect, useState } from 'react';
import mapboxgl from 'mapbox-gl';
import { SearchBox } from '@mapbox/search-js-react';
import 'mapbox-gl/dist/mapbox-gl.css';

const accessToken = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN;
const center = [-71.05953, 42.3629];

function MapWithSearch() {
  const mapRef = useRef(null);
  const mapContainerRef = useRef(null);
  const [inputValue, setInputValue] = useState('');

  useEffect(() => {
    mapboxgl.accessToken = accessToken;

    mapRef.current = new mapboxgl.Map({
      container: mapContainerRef.current,
      center: center,
      zoom: 13
    });

    return () => {
      mapRef.current.remove();
    };
  }, []);

  return (
    \x3C>
      \x3Cdiv
        style={{
          margin: '10px 10px 0 0',
          width: 300,
          right: 0,
          top: 0,
          position: 'absolute',
          zIndex: 10
        }}
      >
        \x3CSearchBox
          accessToken={accessToken}
          map={mapRef.current}
          mapboxgl={mapboxgl}
          value={inputValue}
          proximity={center}
          onChange={(d) => setInputValue(d)}
          marker
        />
      \x3C/div>
      \x3Cdiv ref={mapContainerRef} style={{ height: '100vh' }} />
    \x3C/>
  );
}

Search JS Integration Summary

Install:

npm install @mapbox/search-js-react      # React
npm install @mapbox/search-js-web        # Vanilla/Vue/Svelte

Both packages include @mapbox/search-js-core as a dependency. Only install -core directly if building a custom search UI.

Key configuration options:

  • accessToken: Your Mapbox public token
  • map: Map instance (must be initialized first)
  • mapboxgl: The mapboxgl library reference
  • proximity: [lng, lat] to bias results geographically
  • marker: Boolean to show/hide result marker
  • placeholder: Search box placeholder text

Positioning Search Box

Absolute positioning (overlay):

\x3Cdiv
  style={{
    position: 'absolute',
    top: 10,
    right: 10,
    zIndex: 10,
    width: 300
  }}
>
  \x3CSearchBox {...props} />
\x3C/div>

Common positions:

  • Top-right: top: 10px, right: 10px
  • Top-left: top: 10px, left: 10px
  • Bottom-left: bottom: 10px, left: 10px

Common Mistakes (Critical)

Mistake 1: Forgetting to call map.remove()

// BAD - Memory leak!
useEffect(() => {
  const map = new mapboxgl.Map({ ... })
  // No cleanup function
}, [])

// GOOD - Proper cleanup
useEffect(() => {
  const map = new mapboxgl.Map({ ... })
  return () => map.remove()  // Cleanup
}, [])

Why: Every Map instance creates WebGL contexts, event listeners, and DOM nodes. Without cleanup, these accumulate and cause memory leaks.

Mistake 2: Initializing map in render

// BAD - Infinite loop in React!
function MapComponent() {
  const map = new mapboxgl.Map({ ... })  // Runs on every render
  return \x3Cdiv />
}

// GOOD - Initialize in effect
function MapComponent() {
  useEffect(() => {
    const map = new mapboxgl.Map({ ... })
  }, [])
  return \x3Cdiv />
}

Why: React components re-render frequently. Creating a new map on every render causes infinite loops and crashes.

Mistake 3: Not storing map instance properly

// BAD - map variable lost between renders
function MapComponent() {
  useEffect(() => {
    let map = new mapboxgl.Map({ ... })
    // map variable is not accessible later
  }, [])
}

// GOOD - Store in useRef
function MapComponent() {
  const mapRef = useRef()
  useEffect(() => {
    mapRef.current = new mapboxgl.Map({ ... })
    // mapRef.current accessible throughout component
  }, [])
}

Why: You need to access the map instance for operations like adding layers, markers, or calling remove().

Mistake 4: Storing map instance in Vue's data() (Vue-specific)

// BAD - Vue's reactivity wraps data() objects in a Proxy, breaking mapbox-gl internals!
export default {
  data() {
    return {
      map: null  // Will be wrapped in a Proxy
    }
  },
  mounted() {
    this.map = new mapboxgl.Map({ ... })  // Proxy breaks GL internals
  }
}

// GOOD - Assign map as a plain instance property, not in data()
export default {
  mounted() {
    this.map = new mapboxgl.Map({
      container: this.$refs.mapContainer,
      center: [-71.05953, 42.3629],
      zoom: 13
    })
  },
  unmounted() {
    this.map?.remove()
  }
}

Why: In Vue (especially Vue 3), data() properties are wrapped in a Proxy for reactivity. Mapbox GL JS internally checks object identity and uses properties that don't survive proxy wrapping. Storing the map in data() causes subtle, hard-to-debug failures. Instead, assign the map instance directly as this.map in mounted() — properties assigned outside data() are not made reactive.

Reference Files

Load these for framework-specific patterns and additional details:

  • references/vue.md — Vue Integration (mounted/unmounted lifecycle)
  • references/svelte.md — Svelte Integration (onMount/onDestroy)
  • references/angular.md — Angular Integration with SSR handling
  • references/vanilla.md — Vanilla JS (Vite) + Vanilla JS (CDN)
  • references/web-components.md — Web Components (basic + reactive + usage in React/Vue/Svelte)
  • references/nextjs.md — Next.js App Router + Pages Router
  • references/common-mistakes.md — Common Mistakes 4-7 + Testing Patterns
  • references/token-management.md — Token Management per bundler + Style Configuration

When to Use This Skill

Invoke this skill when:

  • Setting up Mapbox GL JS in a new project
  • Integrating Mapbox into a specific framework (React, Vue, Svelte, Angular, Next.js)
  • Building framework-agnostic Web Components
  • Creating reusable map components for component libraries
  • Debugging map initialization issues
  • Adding Mapbox Search functionality
  • Implementing proper cleanup and lifecycle management
  • Converting between frameworks (e.g., React to Vue)
  • Reviewing code for Mapbox integration best practices

Related Skills

  • mapbox-cartography: Map design principles and styling
  • mapbox-token-security: Token management and security
  • mapbox-style-patterns: Common map style patterns

Resources

Usage Guidance
This skill is coherently documented and low-risk because it's instruction-only and asks for nothing from your system. Before installing, verify the skill source (homepage is missing) if you require official Mapbox provenance; do not paste or commit your secret access tokens into source code — follow the provided guidance to use environment variables and public client tokens (pk.*) for client-side apps. If you plan to copy examples into production, pin library versions and ensure you use server-side tokens for privileged operations (and rotate tokens if they are exposed).
Capability Analysis
Type: OpenClaw Skill Name: mapbox-web-integration-patterns Version: 1.0.0 The skill bundle provides legitimate, high-quality integration patterns and best practices for Mapbox GL JS across various web frameworks (React, Vue, Svelte, Angular, and Next.js). It correctly emphasizes security (using environment variables for tokens in 'references/token-management.md') and performance (proper lifecycle management and cleanup with map.remove() in 'SKILL.md' and 'AGENTS.md'). No evidence of malicious intent, data exfiltration, or harmful prompt injection was found.
Capability Assessment
Purpose & Capability
The name/description (Mapbox GL JS integration patterns) align with the provided files: guides, framework-specific examples, and token-management guidance. Nothing in the manifest or content requests unrelated capabilities (no cloud creds, no system binaries).
Instruction Scope
SKILL.md and the reference files only contain guidance and example code snippets for initializing Mapbox maps, lifecycle cleanup, SSR handling, and token-management patterns. Examples reference environment variables for tokens (Vite/Next.js/CRA), but the skill does not instruct reading system files or sending data to unexpected endpoints. External URLs are Mapbox CDN/documentation links, expected for this purpose.
Install Mechanism
There is no install specification and no code files that would be executed; this is instruction-only, which is the lowest-risk install profile for a documentation skill.
Credentials
The skill declares no required environment variables or credentials. Example code references standard client-side env var names (VITE_MAPBOX_ACCESS_TOKEN, NEXT_PUBLIC_MAPBOX_TOKEN, REACT_APP_MAPBOX_TOKEN) — appropriate and proportionate for Mapbox usage. There are no requests for unrelated secrets or multiple unrelated credentials.
Persistence & Privilege
The skill is not always-enabled (always: false) and does not request persistence or modify other skills. Default autonomous invocation is allowed but not combined with other risky properties.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install mapbox-web-integration-patterns
  3. After installation, invoke the skill by name or use /mapbox-web-integration-patterns
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.0
Initial release of official Mapbox GL JS integration patterns for major web frameworks. - Provides production-ready examples for React, Vue, Svelte, Angular, and vanilla JS. - Covers core topics: setup, lifecycle management, token handling, search integration, and avoiding common pitfalls. - Includes up-to-date patterns for Mapbox GL JS v3.x and migration notes from v2.x. - Features best practices for cleanup (`map.remove()`), map instance storage, and secure token usage. - Offers sample integration with Mapbox Search JS for React and other frameworks.
Metadata
Slug mapbox-web-integration-patterns
Version 1.0.0
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 1
Frequently Asked Questions

What is Mapbox Web Integration Patterns?

Official integration patterns for Mapbox GL JS across popular web frameworks (React, Vue, Svelte, Angular). Covers setup, lifecycle management, token handlin... It is an AI Agent Skill for Claude Code / OpenClaw, with 107 downloads so far.

How do I install Mapbox Web Integration Patterns?

Run "/install mapbox-web-integration-patterns" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Mapbox Web Integration Patterns free?

Yes, Mapbox Web Integration Patterns is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Mapbox Web Integration Patterns support?

Mapbox Web Integration Patterns is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Mapbox Web Integration Patterns?

It is built and maintained by Mapbox (@mapbox); the current version is v1.0.0.

💬 Comments