Map
terrae/mapFreeComponent
Core map component with Mapbox GL and MapLibre GL support.
Install it
npx shadcn@latest add https://www.terrae.dev/map.jsonSource
1"use client"23import { useTheme } from "next-themes"4import { createContext, useEffect, useRef, useState, type ReactNode } from "react"5import type mapboxgl from "mapbox-gl"6import { mapgl, detectedLibrary } from "./map-library"7import { Globe } from "lucide-react"8import {9 defaultMapStyles,10 defaultMapLibreStyles,11 type MapContextValue,12 type MapThemeStyles,13 type MapProjection,14 type MapCoordinates,15 type MapBounds,16} from "./types"1718export const MapContext = createContext<MapContextValue | null>(null)1920const DEFAULT_CENTER: MapCoordinates = [0, 0]21const DEFAULT_ZOOM = 222const DEFAULT_BEARING = 023const DEFAULT_PITCH = 024const DEFAULT_ROTATE_SPEED = 32526type MapProps = {27 accessToken?: string28 children?: ReactNode29 loader?: ReactNode30 // Forces loader to show when true, hides when false, auto when undefined31 showLoader?: boolean32 // Overrides theme-based styles when set33 style?: string34 styles?: MapThemeStyles35 center?: MapCoordinates36 zoom?: number37 bearing?: number38 pitch?: number39 projection?: MapProjection40 minZoom?: number41 maxZoom?: number42 maxBounds?: MapBounds43 // Auto-rotate the globe (only works with projection="globe")44 autoRotate?: boolean45 // Rotation speed in degrees per second46 rotateSpeed?: number47}4849export const Map = ({50 accessToken,51 children,52 loader,53 showLoader,54 style,55 styles,56 center = DEFAULT_CENTER,57 zoom = DEFAULT_ZOOM,58 bearing = DEFAULT_BEARING,59 pitch = DEFAULT_PITCH,60 projection,61 minZoom,62 maxZoom,63 maxBounds,64 autoRotate,65 rotateSpeed = DEFAULT_ROTATE_SPEED,66}: MapProps) => {67 const containerRef = useRef<HTMLDivElement>(null)68 const mapRef = useRef<mapboxgl.Map | null>(null)69 const [isLoaded, setIsLoaded] = useState(false)70 const [error, setError] = useState<string | null>(null)71 const { resolvedTheme } = useTheme()72 const initializedRef = useRef(false)7374 const shouldShowLoader = showLoader ?? !isLoaded7576 const getMapStyle = () => {77 if (style) {78 return style79 }80 const defaults = detectedLibrary === "maplibre" ? defaultMapLibreStyles : defaultMapStyles81 const darkStyle = styles?.dark ?? defaults.dark82 const lightStyle = styles?.light ?? defaults.light8384 return resolvedTheme === "dark" ? darkStyle : lightStyle85 }8687 const createMapInstance = (container: HTMLDivElement) => {88 return new mapgl.Map({89 container,90 style: getMapStyle(),91 center,92 zoom,93 bearing,94 pitch,95 projection,96 minZoom,97 maxZoom,98// … truncated
What it pulls in
npm packages
Files it writes
More from terrae
All 40 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Map Animated Circleanimated-circle | terrae | Components | Free | 1 dep | |
| Map Animated Footprintanimated-footprint | terrae | Components | Free | 2 deps | |
| Map Animated Polygonanimated-polygon | terrae | Components | Free | 1 dep | |
| Map Animated Pulseanimated-pulse | terrae | Components | Free | 1 dep | |
| Map Arc Animatedarc-animated | terrae | Components | Free | 1 dep | |
| Map Blur Areablur-area | terrae | Components | Free | 1 dep | |
| Map Camera Followcamera-follow | terrae | Components | Free | 1 dep | |
| Map Choroplethchoropleth | terrae | Components | Free | 1 dep |
