Map Line
terrae/lineFreeComponent
Static routes and paths on the map.
Install it
npx shadcn@latest add https://www.terrae.dev/line.jsonSource
1"use client"23import type mapboxgl from "mapbox-gl"45import { useEffect, useId, useRef } from "react"6import { useMap } from "./hooks"7import type { MapPath } from "./types"89type MapLineProps = {10 /** Array of [longitude, latitude] coordinate pairs defining the line */11 coordinates: MapPath12 /** Line color as CSS color value (default: "#4285F4") */13 color?: string14 /** Line width in pixels (default: 3) */15 width?: number16 /** Line opacity from 0 to 1 (default: 0.8) */17 opacity?: number18 /** Dash pattern [dash length, gap length] for dashed lines */19 dashArray?: [number, number]20}2122export function MapLine({ coordinates, color = "#4285F4", width = 3, opacity = 0.8, dashArray }: MapLineProps) {23 const { map, isLoaded } = useMap()24 const id = useId()25 const sourceId = `line-source-${id}`26 const layerId = `line-layer-${id}`27 const initializedRef = useRef(false)2829 useEffect(() => {30 if (!isLoaded || !map || initializedRef.current) return3132 try {33 // Create GeoJSON data34 const geojsonData: GeoJSON.Feature<GeoJSON.LineString> = {35 type: "Feature",36 properties: {},37 geometry: {38 type: "LineString",39 coordinates: coordinates,40 },41 }4243 // Add source44 map.addSource(sourceId, {45 type: "geojson",46 data: geojsonData,47 })4849 // Find the first symbol layer to insert the line layer before it50 const layers = map.getStyle().layers51 let firstSymbolId: string | undefined52 if (layers) {53 for (const layer of layers) {54 if (layer.type === "symbol") {55 firstSymbolId = layer.id56 break57 }58 }59 }6061 // Add layer62 map.addLayer(63 {64 id: layerId,65 type: "line",66 source: sourceId,67 layout: {68 "line-join": "round",69 "line-cap": "round",70 },71 paint: {72 "line-color": color,73 "line-width": width,74 "line-opacity": opacity,75 ...(dashArray && { "line-dasharray": dashArray }),76 },77 },78 firstSymbolId79 )8081 initializedRef.current = true82 } catch (error) {83 console.error("Error adding line layer:", error)84 }8586 return () => {87 initializedRef.current = false88 try {89 if (map && map.getStyle()) {90 if (map.getLayer(layerId)) {91 map.removeLayer(layerId)92 }93// … truncated
What it pulls in
npm packages
Other registry items
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 |
