Beautiful map components. 100% Free, Zero config, one command setup.
TypeScript
Update: added attribution
Anmoldeep Singh501f6f961fcb
diff --git a/public/maps/map.json b/public/maps/map.jsonindex b871a3b..5439a55 100644--- a/public/maps/map.json+++ b/public/maps/map.json@@ -12,7 +12,7 @@"files": [{"path": "src/registry/map.tsx",- "content": "\"use client\";\n\nimport MapLibreGL, { type PopupOptions, type MarkerOptions } from \"maplibre-gl\";\nimport \"maplibre-gl/dist/maplibre-gl.css\";\nimport { useTheme } from \"next-themes\";\nimport {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useId,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { X, Minus, Plus, Locate, Maximize, Loader2 } from \"lucide-react\";\n\nimport { cn } from \"@/lib/utils\";\n\ntype MapContextValue = {\n map: MapLibreGL.Map | null;\n isLoaded: boolean;\n};\n\nconst MapContext = createContext<MapContextValue | null>(null);\n\nfunction useMap() {\n const context = useContext(MapContext);\n if (!context) {\n throw new Error(\"useMap must be used within a Map component\");\n }\n return context;\n}\n\nconst defaultStyles = {\n dark: \"https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json\",\n light: \"https://basemaps.cartocdn.com/gl/positron-gl-style/style.json\",\n};\n\ntype MapStyleOption = string | MapLibreGL.StyleSpecification;\n\ntype MapProps = {\n children?: ReactNode;\n /** Custom map styles for light and dark themes. Overrides the default Carto styles. */\n styles?: {\n light?: MapStyleOption;\n dark?: MapStyleOption;\n };\n} & Omit<MapLibreGL.MapOptions, \"container\" | \"style\">;\n\nconst DefaultLoader = () => (\n <div className=\"absolute inset-0 flex items-center justify-center\">\n <div className=\"flex gap-1\">\n <span className=\"size-1.5 rounded-full bg-muted-foreground/60 animate-pulse\" />\n <span className=\"size-1.5 rounded-full bg-muted-foreground/60 animate-pulse [animation-delay:150ms]\" />\n <span className=\"size-1.5 rounded-full bg-muted-foreground/60 animate-pulse [animation-delay:300ms]\" />\n </div>\n </div>\n);\n\nfunction Map({ children, styles, ...props }: MapProps) {\n const containerRef = useRef<HTMLDivElement>(null);\n const mapRef = useRef<MapLibreGL.Map | null>(null);\n const [isMounted, setIsMounted] = useState(false);\n const [isLoaded, setIsLoaded] = useState(false);\n const [isStyleLoaded, setIsStyleLoaded] = useState(false);\n const { resolvedTheme } = useTheme();\n\n const mapStyles = useMemo(\n () => ({\n dark: styles?.dark ?? defaultStyles.dark,\n light: styles?.light ?? defaultStyles.light,\n }),\n [styles]\n );\n\n useEffect(() => {\n setIsMounted(true);\n }, []);\n\n useEffect(() => {\n if (!isMounted || !containerRef.current) return;\n\n const mapStyle =\n resolvedTheme === \"dark\" ? mapStyles.dark : mapStyles.light;\n\n const mapInstance = new MapLibreGL.Map({\n container: containerRef.current,\n style: mapStyle,\n attributionControl: false,\n ...props,\n });\n\n const styleDataHandler = () => setIsStyleLoaded(true);\n const loadHandler = () => setIsLoaded(true);\n\n mapInstance.on(\"load\", loadHandler);\n mapInstance.on(\"styledata\", styleDataHandler);\n mapRef.current = mapInstance;\n\n return () => {\n mapInstance.off(\"load\", loadHandler);\n mapInstance.off(\"styledata\", styleDataHandler);\n mapInstance.remove();\n mapRef.current = null;\n };\n }, [isMounted]);\n\n useEffect(() => {\n if (mapRef.current) {\n setIsStyleLoaded(false);\n mapRef.current.setStyle(\n resolvedTheme === \"dark\" ? mapStyles.dark : mapStyles.light,\n { diff: true }\n );\n }\n }, [resolvedTheme]);\n\n const isLoading = !isMounted || !isLoaded || !isStyleLoaded;\n\n return (\n <MapContext.Provider\n value={{\n map: mapRef.current,\n isLoaded: isMounted && isLoaded && isStyleLoaded,\n }}\n >\n <div ref={containerRef} className=\"relative w-full h-full\">\n {isLoading && <DefaultLoader />}\n {/* guard against hydration error */}\n {isMounted && children}\n </div>\n </MapContext.Provider>\n );\n}\n\ntype MarkerContextValue = {\n markerRef: React.RefObject<MapLibreGL.Marker | null>;\n markerElementRef: React.RefObject<HTMLDivElement | null>;\n map: MapLibreGL.Map | null;\n isReady: boolean;\n};\n\nconst MarkerContext = createContext<MarkerContextValue | null>(null);\n\nfunction useMarkerContext() {\n const context = useContext(MarkerContext);\n if (!context) {\n throw new Error(\"Marker components must be used within MapMarker\");\n }\n return context;\n}\n\ntype MapMarkerProps = {\n longitude: number;\n latitude: number;\n children: ReactNode;\n onClick?: (e: MouseEvent) => void;\n onMouseEnter?: (e: MouseEvent) => void;\n onMouseLeave?: (e: MouseEvent) => void;\n onDragStart?: (lngLat: { lng: number; lat: number }) => void;\n onDrag?: (lngLat: { lng: number; lat: number }) => void;\n onDragEnd?: (lngLat: { lng: number; lat: number }) => void;\n} & Omit<MarkerOptions, \"element\">;\n\nfunction MapMarker({\n longitude,\n latitude,\n children,\n onClick,\n onMouseEnter,\n onMouseLeave,\n onDragStart,\n onDrag,\n onDragEnd,\n draggable = false,\n ...markerOptions\n}: MapMarkerProps) {\n const { map, isLoaded } = useMap();\n const markerRef = useRef<MapLibreGL.Marker | null>(null);\n const markerElementRef = useRef<HTMLDivElement | null>(null);\n const [isReady, setIsReady] = useState(false);\n const markerOptionsRef = useRef(markerOptions);\n\n useEffect(() => {\n if (!isLoaded || !map) return;\n\n const container = document.createElement(\"div\");\n markerElementRef.current = container;\n\n const marker = new MapLibreGL.Marker({\n ...markerOptions,\n element: container,\n draggable,\n })\n .setLngLat([longitude, latitude])\n .addTo(map);\n\n markerRef.current = marker;\n\n const handleClick = (e: MouseEvent) => onClick?.(e);\n const handleMouseEnter = (e: MouseEvent) => onMouseEnter?.(e);\n const handleMouseLeave = (e: MouseEvent) => onMouseLeave?.(e);\n\n container.addEventListener(\"click\", handleClick);\n container.addEventListener(\"mouseenter\", handleMouseEnter);\n container.addEventListener(\"mouseleave\", handleMouseLeave);\n\n const handleDragStart = () => {\n const lngLat = marker.getLngLat();\n onDragStart?.({ lng: lngLat.lng, lat: lngLat.lat });\n };\n const handleDrag = () => {\n const lngLat = marker.getLngLat();\n onDrag?.({ lng: lngLat.lng, lat: lngLat.lat });\n };\n const handleDragEnd = () => {\n const lngLat = marker.getLngLat();\n onDragEnd?.({ lng: lngLat.lng, lat: lngLat.lat });\n };\n\n marker.on(\"dragstart\", handleDragStart);\n marker.on(\"drag\", handleDrag);\n marker.on(\"dragend\", handleDragEnd);\n\n setIsReady(true);\n\n return () => {\n container.removeEventListener(\"click\", handleClick);\n container.removeEventListener(\"mouseenter\", handleMouseEnter);\n container.removeEventListener(\"mouseleave\", handleMouseLeave);\n\n marker.off(\"dragstart\", handleDragStart);\n marker.off(\"drag\", handleDrag);\n marker.off(\"dragend\", handleDragEnd);\n\n marker.remove();\n markerRef.current = null;\n markerElementRef.current = null;\n setIsReady(false);\n };\n }, [map, isLoaded]);\n\n useEffect(() => {\n markerRef.current?.setLngLat([longitude, latitude]);\n }, [longitude, latitude]);\n\n useEffect(() => {\n markerRef.current?.setDraggable(draggable);\n }, [draggable]);\n\n useEffect(() => {\n if (!markerRef.current) return;\n const prev = markerOptionsRef.current;\n\n if (prev.offset !== markerOptions.offset) {\n markerRef.current.setOffset(markerOptions.offset ?? [0, 0]);\n }\n if (prev.rotation !== markerOptions.rotation) {\n markerRef.current.setRotation(markerOptions.rotation ?? 0);\n }\n if (prev.rotationAlignment !== markerOptions.rotationAlignment) {\n markerRef.current.setRotationAlignment(\n markerOptions.rotationAlignment ?? \"auto\"\n );\n }\n if (prev.pitchAlignment !== markerOptions.pitchAlignment) {\n markerRef.current.setPitchAlignment(\n markerOptions.pitchAlignment ?? \"auto\"\n );\n }\n\n markerOptionsRef.current = markerOptions;\n }, [markerOptions]);\n\n return (\n <MarkerContext.Provider\n value={{ markerRef, markerElementRef, map, isReady }}\n >\n {children}\n </MarkerContext.Provider>\n );\n}\n\ntype MarkerContentProps = {\n children?: ReactNode;\n className?: string;\n};\n\nfunction MarkerContent({ children, className }: MarkerContentProps) {\n const { markerElementRef, isReady } = useMarkerContext();\n\n if (!isReady || !markerElementRef.current) return null;\n\n return createPortal(\n <div className={cn(\"relative cursor-pointer\", className)}>\n {children || <DefaultMarkerIcon />}\n </div>,\n markerElementRef.current\n );\n}\n\nfunction DefaultMarkerIcon() {\n return (\n <div className=\"relative h-4 w-4 rounded-full border-2 border-white bg-blue-500 shadow-lg\" />\n );\n}\n\ntype MarkerPopupProps = {\n children: ReactNode;\n className?: string;\n closeButton?: boolean;\n} & Omit<PopupOptions, \"className\">;\n\nfunction MarkerPopup({\n children,\n className,\n closeButton = false,\n ...popupOptions\n}: MarkerPopupProps) {\n const { markerRef, isReady } = useMarkerContext();\n const containerRef = useRef<HTMLDivElement | null>(null);\n const popupRef = useRef<MapLibreGL.Popup | null>(null);\n const [mounted, setMounted] = useState(false);\n const popupOptionsRef = useRef(popupOptions);\n\n useEffect(() => {\n if (!isReady || !markerRef.current) return;\n\n const container = document.createElement(\"div\");\n containerRef.current = container;\n\n const popup = new MapLibreGL.Popup({\n offset: 16,\n ...popupOptions,\n closeButton: false,\n })\n .setMaxWidth(\"none\")\n .setDOMContent(container);\n\n popupRef.current = popup;\n markerRef.current.setPopup(popup);\n setMounted(true);\n\n return () => {\n popup.remove();\n popupRef.current = null;\n containerRef.current = null;\n setMounted(false);\n };\n }, [isReady]);\n\n useEffect(() => {\n if (!popupRef.current) return;\n const prev = popupOptionsRef.current;\n\n if (prev.offset !== popupOptions.offset) {\n popupRef.current.setOffset(popupOptions.offset ?? 16);\n }\n if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {\n popupRef.current.setMaxWidth(popupOptions.maxWidth ?? \"none\");\n }\n\n popupOptionsRef.current = popupOptions;\n }, [popupOptions]);\n\n const handleClose = () => popupRef.current?.remove();\n\n if (!mounted || !containerRef.current) return null;\n\n return createPortal(\n <div\n className={cn(\n \"relative rounded-md border bg-popover p-3 text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95\",\n className\n )}\n >\n {closeButton && (\n <button\n type=\"button\"\n onClick={handleClose}\n className=\"absolute top-1 right-1 z-10 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2\"\n aria-label=\"Close popup\"\n >\n <X className=\"h-4 w-4\" />\n <span className=\"sr-only\">Close</span>\n </button>\n )}\n {children}\n </div>,\n containerRef.current\n );\n}\n\ntype MarkerTooltipProps = {\n children: ReactNode;\n className?: string;\n} & Omit<PopupOptions, \"className\" | \"closeButton\" | \"closeOnClick\">;\n\nfunction MarkerTooltip({\n children,\n className,\n ...popupOptions\n}: MarkerTooltipProps) {\n const { markerRef, markerElementRef, map, isReady } = useMarkerContext();\n const containerRef = useRef<HTMLDivElement | null>(null);\n const popupRef = useRef<MapLibreGL.Popup | null>(null);\n const [mounted, setMounted] = useState(false);\n const popupOptionsRef = useRef(popupOptions);\n\n useEffect(() => {\n if (!isReady || !markerRef.current || !markerElementRef.current || !map)\n return;\n\n const container = document.createElement(\"div\");\n containerRef.current = container;\n\n const popup = new MapLibreGL.Popup({\n offset: 16,\n ...popupOptions,\n closeOnClick: true,\n closeButton: false,\n })\n .setMaxWidth(\"none\")\n .setDOMContent(container);\n\n popupRef.current = popup;\n\n const markerElement = markerElementRef.current;\n const marker = markerRef.current;\n\n const handleMouseEnter = () => {\n popup.setLngLat(marker.getLngLat()).addTo(map);\n };\n const handleMouseLeave = () => popup.remove();\n\n markerElement.addEventListener(\"mouseenter\", handleMouseEnter);\n markerElement.addEventListener(\"mouseleave\", handleMouseLeave);\n setMounted(true);\n\n return () => {\n markerElement.removeEventListener(\"mouseenter\", handleMouseEnter);\n markerElement.removeEventListener(\"mouseleave\", handleMouseLeave);\n popup.remove();\n popupRef.current = null;\n containerRef.current = null;\n setMounted(false);\n };\n }, [isReady, map]);\n\n useEffect(() => {\n if (!popupRef.current) return;\n const prev = popupOptionsRef.current;\n\n if (prev.offset !== popupOptions.offset) {\n popupRef.current.setOffset(popupOptions.offset ?? 16);\n }\n if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {\n popupRef.current.setMaxWidth(popupOptions.maxWidth ?? \"none\");\n }\n\n popupOptionsRef.current = popupOptions;\n }, [popupOptions]);\n\n if (!mounted || !containerRef.current) return null;\n\n return createPortal(\n <div\n className={cn(\n \"rounded-md bg-foreground px-2 py-1 text-xs text-background shadow-md animate-in fade-in-0 zoom-in-95\",\n className\n )}\n >\n {children}\n </div>,\n containerRef.current\n );\n}\n\ntype MarkerLabelProps = {\n children: ReactNode;\n className?: string;\n position?: \"top\" | \"bottom\";\n};\n\nfunction MarkerLabel({\n children,\n className,\n position = \"top\",\n}: MarkerLabelProps) {\n const positionClasses = {\n top: \"bottom-full mb-1\",\n bottom: \"top-full mt-1\",\n };\n\n return (\n <div\n className={cn(\n \"absolute left-1/2 -translate-x-1/2 whitespace-nowrap\",\n \"text-[10px] font-medium text-foreground\",\n positionClasses[position],\n className\n )}\n >\n {children}\n </div>\n );\n}\n\ntype MapControlsProps = {\n position?: \"top-left\" | \"top-right\" | \"bottom-left\" | \"bottom-right\";\n showZoom?: boolean;\n showCompass?: boolean;\n showLocate?: boolean;\n showFullscreen?: boolean;\n className?: string;\n onLocate?: (coords: { longitude: number; latitude: number }) => void;\n};\n\nconst positionClasses = {\n \"top-left\": \"top-2 left-2\",\n \"top-right\": \"top-2 right-2\",\n \"bottom-left\": \"bottom-2 left-2\",\n \"bottom-right\": \"bottom-2 right-2\",\n};\n\nfunction ControlGroup({ children }: { children: React.ReactNode }) {\n return (\n <div className=\"flex flex-col rounded-md border border-border bg-background shadow-sm overflow-hidden [&>button:not(:last-child)]:border-b [&>button:not(:last-child)]:border-border\">\n {children}\n </div>\n );\n}\n\nfunction ControlButton({\n onClick,\n label,\n children,\n disabled = false,\n}: {\n onClick: () => void;\n label: string;\n children: React.ReactNode;\n disabled?: boolean;\n}) {\n return (\n <button\n onClick={onClick}\n aria-label={label}\n type=\"button\"\n className={cn(\n \"flex items-center justify-center size-8 hover:bg-accent dark:hover:bg-accent/40 transition-colors\",\n disabled && \"opacity-50 pointer-events-none cursor-not-allowed\"\n )}\n disabled={disabled}\n >\n {children}\n </button>\n );\n}\n\nfunction MapControls({\n position = \"bottom-right\",\n showZoom = true,\n showCompass = false,\n showLocate = false,\n showFullscreen = false,\n className,\n onLocate,\n}: MapControlsProps) {\n const { map, isLoaded } = useMap();\n const [waitingForLocation, setWaitingForLocation] = useState(false);\n\n const handleZoomIn = useCallback(() => {\n map?.zoomTo(map.getZoom() + 1, { duration: 300 });\n }, [map]);\n\n const handleZoomOut = useCallback(() => {\n map?.zoomTo(map.getZoom() - 1, { duration: 300 });\n }, [map]);\n\n const handleResetBearing = useCallback(() => {\n map?.resetNorthPitch({ duration: 300 });\n }, [map]);\n\n const handleLocate = useCallback(() => {\n setWaitingForLocation(true);\n if (\"geolocation\" in navigator) {\n navigator.geolocation.getCurrentPosition(\n (pos) => {\n const coords = {\n longitude: pos.coords.longitude,\n latitude: pos.coords.latitude,\n };\n map?.flyTo({\n center: [coords.longitude, coords.latitude],\n zoom: 14,\n duration: 1500,\n });\n onLocate?.(coords);\n setWaitingForLocation(false);\n },\n (error) => {\n console.error(\"Error getting location:\", error);\n setWaitingForLocation(false);\n }\n );\n }\n }, [map, onLocate]);\n\n const handleFullscreen = useCallback(() => {\n const container = map?.getContainer();\n if (!container) return;\n if (document.fullscreenElement) {\n document.exitFullscreen();\n } else {\n container.requestFullscreen();\n }\n }, [map]);\n\n if (!isLoaded) return null;\n\n return (\n <div\n className={cn(\n \"absolute z-10 flex flex-col gap-1.5\",\n positionClasses[position],\n className\n )}\n >\n {showZoom && (\n <ControlGroup>\n <ControlButton onClick={handleZoomIn} label=\"Zoom in\">\n <Plus className=\"size-4\" />\n </ControlButton>\n <ControlButton onClick={handleZoomOut} label=\"Zoom out\">\n <Minus className=\"size-4\" />\n </ControlButton>\n </ControlGroup>\n )}\n {showCompass && (\n <ControlGroup>\n <CompassButton onClick={handleResetBearing} />\n </ControlGroup>\n )}\n {showLocate && (\n <ControlGroup>\n <ControlButton\n onClick={handleLocate}\n label=\"Find my location\"\n disabled={waitingForLocation}\n >\n {waitingForLocation ? (\n <Loader2 className=\"size-4 animate-spin\" />\n ) : (\n <Locate className=\"size-4\" />\n )}\n </ControlButton>\n </ControlGroup>\n )}\n {showFullscreen && (\n <ControlGroup>\n <ControlButton onClick={handleFullscreen} label=\"Toggle fullscreen\">\n <Maximize className=\"size-4\" />\n </ControlButton>\n </ControlGroup>\n )}\n </div>\n );\n}\n\nfunction CompassButton({ onClick }: { onClick: () => void }) {\n const { isLoaded, map } = useMap();\n const compassRef = useRef<SVGSVGElement>(null);\n\n useEffect(() => {\n if (!isLoaded || !map || !compassRef.current) return;\n\n const compass = compassRef.current;\n\n const updateRotation = () => {\n const bearing = map.getBearing();\n const pitch = map.getPitch();\n compass.style.transform = `rotateX(${pitch}deg) rotateZ(${-bearing}deg)`;\n };\n\n map.on(\"rotate\", updateRotation);\n map.on(\"pitch\", updateRotation);\n updateRotation();\n\n return () => {\n map.off(\"rotate\", updateRotation);\n map.off(\"pitch\", updateRotation);\n };\n }, [isLoaded, map]);\n\n return (\n <ControlButton onClick={onClick} label=\"Reset bearing to north\">\n <svg\n ref={compassRef}\n viewBox=\"0 0 24 24\"\n className=\"size-5 transition-transform duration-200\"\n style={{ transformStyle: \"preserve-3d\" }}\n >\n <path d=\"M12 2L16 12H12V2Z\" className=\"fill-red-500\" />\n <path d=\"M12 2L8 12H12V2Z\" className=\"fill-red-300\" />\n <path d=\"M12 22L16 12H12V22Z\" className=\"fill-muted-foreground/60\" />\n <path d=\"M12 22L8 12H12V22Z\" className=\"fill-muted-foreground/30\" />\n </svg>\n </ControlButton>\n );\n}\n\ntype MapPopupProps = {\n longitude: number;\n latitude: number;\n onClose?: () => void;\n children: ReactNode;\n className?: string;\n closeButton?: boolean;\n} & Omit<PopupOptions, \"className\">;\n\nfunction MapPopup({\n longitude,\n latitude,\n onClose,\n children,\n className,\n closeButton = false,\n ...popupOptions\n}: MapPopupProps) {\n const { map } = useMap();\n const popupRef = useRef<MapLibreGL.Popup | null>(null);\n const popupOptionsRef = useRef(popupOptions);\n\n const container = useMemo(() => document.createElement(\"div\"), []);\n\n useEffect(() => {\n if (!map) return;\n\n const popup = new MapLibreGL.Popup({\n offset: 16,\n ...popupOptions,\n closeButton: false,\n })\n .setMaxWidth(\"none\")\n .setDOMContent(container)\n .setLngLat([longitude, latitude])\n .addTo(map);\n\n const onCloseProp = () => onClose?.();\n\n popup.on(\"close\", onCloseProp);\n\n popupRef.current = popup;\n\n return () => {\n popup.off(\"close\", onCloseProp);\n if (popup.isOpen()) {\n popup.remove();\n }\n popupRef.current = null;\n };\n }, [map]);\n\n useEffect(() => {\n popupRef.current?.setLngLat([longitude, latitude]);\n }, [longitude, latitude]);\n\n useEffect(() => {\n if (!popupRef.current) return;\n const prev = popupOptionsRef.current;\n\n if (prev.offset !== popupOptions.offset) {\n popupRef.current.setOffset(popupOptions.offset ?? 16);\n }\n if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {\n popupRef.current.setMaxWidth(popupOptions.maxWidth ?? \"none\");\n }\n\n popupOptionsRef.current = popupOptions;\n }, [popupOptions]);\n\n const handleClose = () => {\n popupRef.current?.remove();\n onClose?.();\n };\n\n return createPortal(\n <div\n className={cn(\n \"relative rounded-md border bg-popover p-3 text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95\",\n className\n )}\n >\n {closeButton && (\n <button\n type=\"button\"\n onClick={handleClose}\n className=\"absolute top-1 right-1 z-10 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2\"\n aria-label=\"Close popup\"\n >\n <X className=\"h-4 w-4\" />\n <span className=\"sr-only\">Close</span>\n </button>\n )}\n {children}\n </div>,\n container\n );\n}\n\ntype MapRouteProps = {\n coordinates: [number, number][];\n color?: string;\n width?: number;\n opacity?: number;\n dashArray?: [number, number];\n};\n\nfunction MapRoute({\n coordinates,\n color = \"#4285F4\",\n width = 3,\n opacity = 0.8,\n dashArray,\n}: MapRouteProps) {\n const { map, isLoaded } = useMap();\n const id = useId();\n const sourceId = `route-source-${id}`;\n const layerId = `route-layer-${id}`;\n\n // Add source and layer on mount\n useEffect(() => {\n if (!isLoaded || !map) return;\n\n map.addSource(sourceId, {\n type: \"geojson\",\n data: {\n type: \"Feature\",\n properties: {},\n geometry: { type: \"LineString\", coordinates: [] },\n },\n });\n\n map.addLayer({\n id: layerId,\n type: \"line\",\n source: sourceId,\n layout: { \"line-join\": \"round\", \"line-cap\": \"round\" },\n paint: {\n \"line-color\": color,\n \"line-width\": width,\n \"line-opacity\": opacity,\n ...(dashArray && { \"line-dasharray\": dashArray }),\n },\n });\n\n return () => {\n try {\n if (map.getLayer(layerId)) map.removeLayer(layerId);\n if (map.getSource(sourceId)) map.removeSource(sourceId);\n } catch {\n // ignore\n }\n };\n }, [isLoaded, map, sourceId, layerId]);\n\n // When coordinates change, update the source data\n useEffect(() => {\n if (!isLoaded || !map || coordinates.length < 2) return;\n\n const source = map.getSource(sourceId) as MapLibreGL.GeoJSONSource;\n if (source) {\n source.setData({\n type: \"Feature\",\n properties: {},\n geometry: { type: \"LineString\", coordinates },\n });\n }\n }, [isLoaded, map, coordinates, sourceId]);\n\n useEffect(() => {\n if (!isLoaded || !map || !map.getLayer(layerId)) return;\n\n map.setPaintProperty(layerId, \"line-color\", color);\n map.setPaintProperty(layerId, \"line-width\", width);\n map.setPaintProperty(layerId, \"line-opacity\", opacity);\n if (dashArray) {\n map.setPaintProperty(layerId, \"line-dasharray\", dashArray);\n }\n }, [isLoaded, map, layerId, color, width, opacity, dashArray]);\n\n return null;\n}\n\nexport {\n Map,\n useMap,\n MapMarker,\n MarkerContent,\n MarkerPopup,\n MarkerTooltip,\n MarkerLabel,\n MapPopup,\n MapControls,\n MapRoute,\n};\n",+ "content": "\"use client\";\n\nimport MapLibreGL, { type PopupOptions, type MarkerOptions } from \"maplibre-gl\";\nimport \"maplibre-gl/dist/maplibre-gl.css\";\nimport { useTheme } from \"next-themes\";\nimport {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useId,\n useMemo,\n useRef,\n useState,\n type ReactNode,\n} from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { X, Minus, Plus, Locate, Maximize, Loader2 } from \"lucide-react\";\n\nimport { cn } from \"@/lib/utils\";\n\ntype MapContextValue = {\n map: MapLibreGL.Map | null;\n isLoaded: boolean;\n};\n\nconst MapContext = createContext<MapContextValue | null>(null);\n\nfunction useMap() {\n const context = useContext(MapContext);\n if (!context) {\n throw new Error(\"useMap must be used within a Map component\");\n }\n return context;\n}\n\nconst defaultStyles = {\n dark: \"https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json\",\n light: \"https://basemaps.cartocdn.com/gl/positron-gl-style/style.json\",\n};\n\ntype MapStyleOption = string | MapLibreGL.StyleSpecification;\n\ntype MapProps = {\n children?: ReactNode;\n /** Custom map styles for light and dark themes. Overrides the default Carto styles. */\n styles?: {\n light?: MapStyleOption;\n dark?: MapStyleOption;\n };\n} & Omit<MapLibreGL.MapOptions, \"container\" | \"style\">;\n\nconst DefaultLoader = () => (\n <div className=\"absolute inset-0 flex items-center justify-center\">\n <div className=\"flex gap-1\">\n <span className=\"size-1.5 rounded-full bg-muted-foreground/60 animate-pulse\" />\n <span className=\"size-1.5 rounded-full bg-muted-foreground/60 animate-pulse [animation-delay:150ms]\" />\n <span className=\"size-1.5 rounded-full bg-muted-foreground/60 animate-pulse [animation-delay:300ms]\" />\n </div>\n </div>\n);\n\nfunction Map({ children, styles, ...props }: MapProps) {\n const containerRef = useRef<HTMLDivElement>(null);\n const mapRef = useRef<MapLibreGL.Map | null>(null);\n const [isMounted, setIsMounted] = useState(false);\n const [isLoaded, setIsLoaded] = useState(false);\n const [isStyleLoaded, setIsStyleLoaded] = useState(false);\n const { resolvedTheme } = useTheme();\n\n const mapStyles = useMemo(\n () => ({\n dark: styles?.dark ?? defaultStyles.dark,\n light: styles?.light ?? defaultStyles.light,\n }),\n [styles]\n );\n\n useEffect(() => {\n setIsMounted(true);\n }, []);\n\n useEffect(() => {\n if (!isMounted || !containerRef.current) return;\n\n const mapStyle =\n resolvedTheme === \"dark\" ? mapStyles.dark : mapStyles.light;\n\n const mapInstance = new MapLibreGL.Map({\n container: containerRef.current,\n style: mapStyle,\n renderWorldCopies: false,\n attributionControl: {\n compact: true,\n },\n ...props,\n });\n\n const styleDataHandler = () => setIsStyleLoaded(true);\n const loadHandler = () => setIsLoaded(true);\n\n mapInstance.on(\"load\", loadHandler);\n mapInstance.on(\"styledata\", styleDataHandler);\n mapRef.current = mapInstance;\n\n return () => {\n mapInstance.off(\"load\", loadHandler);\n mapInstance.off(\"styledata\", styleDataHandler);\n mapInstance.remove();\n mapRef.current = null;\n };\n }, [isMounted]);\n\n useEffect(() => {\n if (mapRef.current) {\n setIsStyleLoaded(false);\n mapRef.current.setStyle(\n resolvedTheme === \"dark\" ? mapStyles.dark : mapStyles.light,\n { diff: true }\n );\n }\n }, [resolvedTheme]);\n\n const isLoading = !isMounted || !isLoaded || !isStyleLoaded;\n\n return (\n <MapContext.Provider\n value={{\n map: mapRef.current,\n isLoaded: isMounted && isLoaded && isStyleLoaded,\n }}\n >\n <div ref={containerRef} className=\"relative w-full h-full\">\n {isLoading && <DefaultLoader />}\n {/* guard against hydration error */}\n {isMounted && children}\n </div>\n </MapContext.Provider>\n );\n}\n\ntype MarkerContextValue = {\n markerRef: React.RefObject<MapLibreGL.Marker | null>;\n markerElementRef: React.RefObject<HTMLDivElement | null>;\n map: MapLibreGL.Map | null;\n isReady: boolean;\n};\n\nconst MarkerContext = createContext<MarkerContextValue | null>(null);\n\nfunction useMarkerContext() {\n const context = useContext(MarkerContext);\n if (!context) {\n throw new Error(\"Marker components must be used within MapMarker\");\n }\n return context;\n}\n\ntype MapMarkerProps = {\n longitude: number;\n latitude: number;\n children: ReactNode;\n onClick?: (e: MouseEvent) => void;\n onMouseEnter?: (e: MouseEvent) => void;\n onMouseLeave?: (e: MouseEvent) => void;\n onDragStart?: (lngLat: { lng: number; lat: number }) => void;\n onDrag?: (lngLat: { lng: number; lat: number }) => void;\n onDragEnd?: (lngLat: { lng: number; lat: number }) => void;\n} & Omit<MarkerOptions, \"element\">;\n\nfunction MapMarker({\n longitude,\n latitude,\n children,\n onClick,\n onMouseEnter,\n onMouseLeave,\n onDragStart,\n onDrag,\n onDragEnd,\n draggable = false,\n ...markerOptions\n}: MapMarkerProps) {\n const { map, isLoaded } = useMap();\n const markerRef = useRef<MapLibreGL.Marker | null>(null);\n const markerElementRef = useRef<HTMLDivElement | null>(null);\n const [isReady, setIsReady] = useState(false);\n const markerOptionsRef = useRef(markerOptions);\n\n useEffect(() => {\n if (!isLoaded || !map) return;\n\n const container = document.createElement(\"div\");\n markerElementRef.current = container;\n\n const marker = new MapLibreGL.Marker({\n ...markerOptions,\n element: container,\n draggable,\n })\n .setLngLat([longitude, latitude])\n .addTo(map);\n\n markerRef.current = marker;\n\n const handleClick = (e: MouseEvent) => onClick?.(e);\n const handleMouseEnter = (e: MouseEvent) => onMouseEnter?.(e);\n const handleMouseLeave = (e: MouseEvent) => onMouseLeave?.(e);\n\n container.addEventListener(\"click\", handleClick);\n container.addEventListener(\"mouseenter\", handleMouseEnter);\n container.addEventListener(\"mouseleave\", handleMouseLeave);\n\n const handleDragStart = () => {\n const lngLat = marker.getLngLat();\n onDragStart?.({ lng: lngLat.lng, lat: lngLat.lat });\n };\n const handleDrag = () => {\n const lngLat = marker.getLngLat();\n onDrag?.({ lng: lngLat.lng, lat: lngLat.lat });\n };\n const handleDragEnd = () => {\n const lngLat = marker.getLngLat();\n onDragEnd?.({ lng: lngLat.lng, lat: lngLat.lat });\n };\n\n marker.on(\"dragstart\", handleDragStart);\n marker.on(\"drag\", handleDrag);\n marker.on(\"dragend\", handleDragEnd);\n\n setIsReady(true);\n\n return () => {\n container.removeEventListener(\"click\", handleClick);\n container.removeEventListener(\"mouseenter\", handleMouseEnter);\n container.removeEventListener(\"mouseleave\", handleMouseLeave);\n\n marker.off(\"dragstart\", handleDragStart);\n marker.off(\"drag\", handleDrag);\n marker.off(\"dragend\", handleDragEnd);\n\n marker.remove();\n markerRef.current = null;\n markerElementRef.current = null;\n setIsReady(false);\n };\n }, [map, isLoaded]);\n\n useEffect(() => {\n markerRef.current?.setLngLat([longitude, latitude]);\n }, [longitude, latitude]);\n\n useEffect(() => {\n markerRef.current?.setDraggable(draggable);\n }, [draggable]);\n\n useEffect(() => {\n if (!markerRef.current) return;\n const prev = markerOptionsRef.current;\n\n if (prev.offset !== markerOptions.offset) {\n markerRef.current.setOffset(markerOptions.offset ?? [0, 0]);\n }\n if (prev.rotation !== markerOptions.rotation) {\n markerRef.current.setRotation(markerOptions.rotation ?? 0);\n }\n if (prev.rotationAlignment !== markerOptions.rotationAlignment) {\n markerRef.current.setRotationAlignment(\n markerOptions.rotationAlignment ?? \"auto\"\n );\n }\n if (prev.pitchAlignment !== markerOptions.pitchAlignment) {\n markerRef.current.setPitchAlignment(\n markerOptions.pitchAlignment ?? \"auto\"\n );\n }\n\n markerOptionsRef.current = markerOptions;\n }, [markerOptions]);\n\n return (\n <MarkerContext.Provider\n value={{ markerRef, markerElementRef, map, isReady }}\n >\n {children}\n </MarkerContext.Provider>\n );\n}\n\ntype MarkerContentProps = {\n children?: ReactNode;\n className?: string;\n};\n\nfunction MarkerContent({ children, className }: MarkerContentProps) {\n const { markerElementRef, isReady } = useMarkerContext();\n\n if (!isReady || !markerElementRef.current) return null;\n\n return createPortal(\n <div className={cn(\"relative cursor-pointer\", className)}>\n {children || <DefaultMarkerIcon />}\n </div>,\n markerElementRef.current\n );\n}\n\nfunction DefaultMarkerIcon() {\n return (\n <div className=\"relative h-4 w-4 rounded-full border-2 border-white bg-blue-500 shadow-lg\" />\n );\n}\n\ntype MarkerPopupProps = {\n children: ReactNode;\n className?: string;\n closeButton?: boolean;\n} & Omit<PopupOptions, \"className\">;\n\nfunction MarkerPopup({\n children,\n className,\n closeButton = false,\n ...popupOptions\n}: MarkerPopupProps) {\n const { markerRef, isReady } = useMarkerContext();\n const containerRef = useRef<HTMLDivElement | null>(null);\n const popupRef = useRef<MapLibreGL.Popup | null>(null);\n const [mounted, setMounted] = useState(false);\n const popupOptionsRef = useRef(popupOptions);\n\n useEffect(() => {\n if (!isReady || !markerRef.current) return;\n\n const container = document.createElement(\"div\");\n containerRef.current = container;\n\n const popup = new MapLibreGL.Popup({\n offset: 16,\n ...popupOptions,\n closeButton: false,\n })\n .setMaxWidth(\"none\")\n .setDOMContent(container);\n\n popupRef.current = popup;\n markerRef.current.setPopup(popup);\n setMounted(true);\n\n return () => {\n popup.remove();\n popupRef.current = null;\n containerRef.current = null;\n setMounted(false);\n };\n }, [isReady]);\n\n useEffect(() => {\n if (!popupRef.current) return;\n const prev = popupOptionsRef.current;\n\n if (prev.offset !== popupOptions.offset) {\n popupRef.current.setOffset(popupOptions.offset ?? 16);\n }\n if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {\n popupRef.current.setMaxWidth(popupOptions.maxWidth ?? \"none\");\n }\n\n popupOptionsRef.current = popupOptions;\n }, [popupOptions]);\n\n const handleClose = () => popupRef.current?.remove();\n\n if (!mounted || !containerRef.current) return null;\n\n return createPortal(\n <div\n className={cn(\n \"relative rounded-md border bg-popover p-3 text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95\",\n className\n )}\n >\n {closeButton && (\n <button\n type=\"button\"\n onClick={handleClose}\n className=\"absolute top-1 right-1 z-10 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2\"\n aria-label=\"Close popup\"\n >\n <X className=\"h-4 w-4\" />\n <span className=\"sr-only\">Close</span>\n </button>\n )}\n {children}\n </div>,\n containerRef.current\n );\n}\n\ntype MarkerTooltipProps = {\n children: ReactNode;\n className?: string;\n} & Omit<PopupOptions, \"className\" | \"closeButton\" | \"closeOnClick\">;\n\nfunction MarkerTooltip({\n children,\n className,\n ...popupOptions\n}: MarkerTooltipProps) {\n const { markerRef, markerElementRef, map, isReady } = useMarkerContext();\n const containerRef = useRef<HTMLDivElement | null>(null);\n const popupRef = useRef<MapLibreGL.Popup | null>(null);\n const [mounted, setMounted] = useState(false);\n const popupOptionsRef = useRef(popupOptions);\n\n useEffect(() => {\n if (!isReady || !markerRef.current || !markerElementRef.current || !map)\n return;\n\n const container = document.createElement(\"div\");\n containerRef.current = container;\n\n const popup = new MapLibreGL.Popup({\n offset: 16,\n ...popupOptions,\n closeOnClick: true,\n closeButton: false,\n })\n .setMaxWidth(\"none\")\n .setDOMContent(container);\n\n popupRef.current = popup;\n\n const markerElement = markerElementRef.current;\n const marker = markerRef.current;\n\n const handleMouseEnter = () => {\n popup.setLngLat(marker.getLngLat()).addTo(map);\n };\n const handleMouseLeave = () => popup.remove();\n\n markerElement.addEventListener(\"mouseenter\", handleMouseEnter);\n markerElement.addEventListener(\"mouseleave\", handleMouseLeave);\n setMounted(true);\n\n return () => {\n markerElement.removeEventListener(\"mouseenter\", handleMouseEnter);\n markerElement.removeEventListener(\"mouseleave\", handleMouseLeave);\n popup.remove();\n popupRef.current = null;\n containerRef.current = null;\n setMounted(false);\n };\n }, [isReady, map]);\n\n useEffect(() => {\n if (!popupRef.current) return;\n const prev = popupOptionsRef.current;\n\n if (prev.offset !== popupOptions.offset) {\n popupRef.current.setOffset(popupOptions.offset ?? 16);\n }\n if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {\n popupRef.current.setMaxWidth(popupOptions.maxWidth ?? \"none\");\n }\n\n popupOptionsRef.current = popupOptions;\n }, [popupOptions]);\n\n if (!mounted || !containerRef.current) return null;\n\n return createPortal(\n <div\n className={cn(\n \"rounded-md bg-foreground px-2 py-1 text-xs text-background shadow-md animate-in fade-in-0 zoom-in-95\",\n className\n )}\n >\n {children}\n </div>,\n containerRef.current\n );\n}\n\ntype MarkerLabelProps = {\n children: ReactNode;\n className?: string;\n position?: \"top\" | \"bottom\";\n};\n\nfunction MarkerLabel({\n children,\n className,\n position = \"top\",\n}: MarkerLabelProps) {\n const positionClasses = {\n top: \"bottom-full mb-1\",\n bottom: \"top-full mt-1\",\n };\n\n return (\n <div\n className={cn(\n \"absolute left-1/2 -translate-x-1/2 whitespace-nowrap\",\n \"text-[10px] font-medium text-foreground\",\n positionClasses[position],\n className\n )}\n >\n {children}\n </div>\n );\n}\n\ntype MapControlsProps = {\n position?: \"top-left\" | \"top-right\" | \"bottom-left\" | \"bottom-right\";\n showZoom?: boolean;\n showCompass?: boolean;\n showLocate?: boolean;\n showFullscreen?: boolean;\n className?: string;\n onLocate?: (coords: { longitude: number; latitude: number }) => void;\n};\n\nconst positionClasses = {\n \"top-left\": \"top-2 left-2\",\n \"top-right\": \"top-2 right-2\",\n \"bottom-left\": \"bottom-2 left-2\",\n \"bottom-right\": \"bottom-10 right-2\",\n};\n\nfunction ControlGroup({ children }: { children: React.ReactNode }) {\n return (\n <div className=\"flex flex-col rounded-md border border-border bg-background shadow-sm overflow-hidden [&>button:not(:last-child)]:border-b [&>button:not(:last-child)]:border-border\">\n {children}\n </div>\n );\n}\n\nfunction ControlButton({\n onClick,\n label,\n children,\n disabled = false,\n}: {\n onClick: () => void;\n label: string;\n children: React.ReactNode;\n disabled?: boolean;\n}) {\n return (\n <button\n onClick={onClick}\n aria-label={label}\n type=\"button\"\n className={cn(\n \"flex items-center justify-center size-8 hover:bg-accent dark:hover:bg-accent/40 transition-colors\",\n disabled && \"opacity-50 pointer-events-none cursor-not-allowed\"\n )}\n disabled={disabled}\n >\n {children}\n </button>\n );\n}\n\nfunction MapControls({\n position = \"bottom-right\",\n showZoom = true,\n showCompass = false,\n showLocate = false,\n showFullscreen = false,\n className,\n onLocate,\n}: MapControlsProps) {\n const { map, isLoaded } = useMap();\n const [waitingForLocation, setWaitingForLocation] = useState(false);\n\n const handleZoomIn = useCallback(() => {\n map?.zoomTo(map.getZoom() + 1, { duration: 300 });\n }, [map]);\n\n const handleZoomOut = useCallback(() => {\n map?.zoomTo(map.getZoom() - 1, { duration: 300 });\n }, [map]);\n\n const handleResetBearing = useCallback(() => {\n map?.resetNorthPitch({ duration: 300 });\n }, [map]);\n\n const handleLocate = useCallback(() => {\n setWaitingForLocation(true);\n if (\"geolocation\" in navigator) {\n navigator.geolocation.getCurrentPosition(\n (pos) => {\n const coords = {\n longitude: pos.coords.longitude,\n latitude: pos.coords.latitude,\n };\n map?.flyTo({\n center: [coords.longitude, coords.latitude],\n zoom: 14,\n duration: 1500,\n });\n onLocate?.(coords);\n setWaitingForLocation(false);\n },\n (error) => {\n console.error(\"Error getting location:\", error);\n setWaitingForLocation(false);\n }\n );\n }\n }, [map, onLocate]);\n\n const handleFullscreen = useCallback(() => {\n const container = map?.getContainer();\n if (!container) return;\n if (document.fullscreenElement) {\n document.exitFullscreen();\n } else {\n container.requestFullscreen();\n }\n }, [map]);\n\n if (!isLoaded) return null;\n\n return (\n <div\n className={cn(\n \"absolute z-10 flex flex-col gap-1.5\",\n positionClasses[position],\n className\n )}\n >\n {showZoom && (\n <ControlGroup>\n <ControlButton onClick={handleZoomIn} label=\"Zoom in\">\n <Plus className=\"size-4\" />\n </ControlButton>\n <ControlButton onClick={handleZoomOut} label=\"Zoom out\">\n <Minus className=\"size-4\" />\n </ControlButton>\n </ControlGroup>\n )}\n {showCompass && (\n <ControlGroup>\n <CompassButton onClick={handleResetBearing} />\n </ControlGroup>\n )}\n {showLocate && (\n <ControlGroup>\n <ControlButton\n onClick={handleLocate}\n label=\"Find my location\"\n disabled={waitingForLocation}\n >\n {waitingForLocation ? (\n <Loader2 className=\"size-4 animate-spin\" />\n ) : (\n <Locate className=\"size-4\" />\n )}\n </ControlButton>\n </ControlGroup>\n )}\n {showFullscreen && (\n <ControlGroup>\n <ControlButton onClick={handleFullscreen} label=\"Toggle fullscreen\">\n <Maximize className=\"size-4\" />\n </ControlButton>\n </ControlGroup>\n )}\n </div>\n );\n}\n\nfunction CompassButton({ onClick }: { onClick: () => void }) {\n const { isLoaded, map } = useMap();\n const compassRef = useRef<SVGSVGElement>(null);\n\n useEffect(() => {\n if (!isLoaded || !map || !compassRef.current) return;\n\n const compass = compassRef.current;\n\n const updateRotation = () => {\n const bearing = map.getBearing();\n const pitch = map.getPitch();\n compass.style.transform = `rotateX(${pitch}deg) rotateZ(${-bearing}deg)`;\n };\n\n map.on(\"rotate\", updateRotation);\n map.on(\"pitch\", updateRotation);\n updateRotation();\n\n return () => {\n map.off(\"rotate\", updateRotation);\n map.off(\"pitch\", updateRotation);\n };\n }, [isLoaded, map]);\n\n return (\n <ControlButton onClick={onClick} label=\"Reset bearing to north\">\n <svg\n ref={compassRef}\n viewBox=\"0 0 24 24\"\n className=\"size-5 transition-transform duration-200\"\n style={{ transformStyle: \"preserve-3d\" }}\n >\n <path d=\"M12 2L16 12H12V2Z\" className=\"fill-red-500\" />\n <path d=\"M12 2L8 12H12V2Z\" className=\"fill-red-300\" />\n <path d=\"M12 22L16 12H12V22Z\" className=\"fill-muted-foreground/60\" />\n <path d=\"M12 22L8 12H12V22Z\" className=\"fill-muted-foreground/30\" />\n </svg>\n </ControlButton>\n );\n}\n\ntype MapPopupProps = {\n longitude: number;\n latitude: number;\n onClose?: () => void;\n children: ReactNode;\n className?: string;\n closeButton?: boolean;\n} & Omit<PopupOptions, \"className\">;\n\nfunction MapPopup({\n longitude,\n latitude,\n onClose,\n children,\n className,\n closeButton = false,\n ...popupOptions\n}: MapPopupProps) {\n const { map } = useMap();\n const popupRef = useRef<MapLibreGL.Popup | null>(null);\n const popupOptionsRef = useRef(popupOptions);\n\n const container = useMemo(() => document.createElement(\"div\"), []);\n\n useEffect(() => {\n if (!map) return;\n\n const popup = new MapLibreGL.Popup({\n offset: 16,\n ...popupOptions,\n closeButton: false,\n })\n .setMaxWidth(\"none\")\n .setDOMContent(container)\n .setLngLat([longitude, latitude])\n .addTo(map);\n\n const onCloseProp = () => onClose?.();\n\n popup.on(\"close\", onCloseProp);\n\n popupRef.current = popup;\n\n return () => {\n popup.off(\"close\", onCloseProp);\n if (popup.isOpen()) {\n popup.remove();\n }\n popupRef.current = null;\n };\n }, [map]);\n\n useEffect(() => {\n popupRef.current?.setLngLat([longitude, latitude]);\n }, [longitude, latitude]);\n\n useEffect(() => {\n if (!popupRef.current) return;\n const prev = popupOptionsRef.current;\n\n if (prev.offset !== popupOptions.offset) {\n popupRef.current.setOffset(popupOptions.offset ?? 16);\n }\n if (prev.maxWidth !== popupOptions.maxWidth && popupOptions.maxWidth) {\n popupRef.current.setMaxWidth(popupOptions.maxWidth ?? \"none\");\n }\n\n popupOptionsRef.current = popupOptions;\n }, [popupOptions]);\n\n const handleClose = () => {\n popupRef.current?.remove();\n onClose?.();\n };\n\n return createPortal(\n <div\n className={cn(\n \"relative rounded-md border bg-popover p-3 text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95\",\n className\n )}\n >\n {closeButton && (\n <button\n type=\"button\"\n onClick={handleClose}\n className=\"absolute top-1 right-1 z-10 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2\"\n aria-label=\"Close popup\"\n >\n <X className=\"h-4 w-4\" />\n <span className=\"sr-only\">Close</span>\n </button>\n )}\n {children}\n </div>,\n container\n );\n}\n\ntype MapRouteProps = {\n coordinates: [number, number][];\n color?: string;\n width?: number;\n opacity?: number;\n dashArray?: [number, number];\n};\n\nfunction MapRoute({\n coordinates,\n color = \"#4285F4\",\n width = 3,\n opacity = 0.8,\n dashArray,\n}: MapRouteProps) {\n const { map, isLoaded } = useMap();\n const id = useId();\n const sourceId = `route-source-${id}`;\n const layerId = `route-layer-${id}`;\n\n // Add source and layer on mount\n useEffect(() => {\n if (!isLoaded || !map) return;\n\n map.addSource(sourceId, {\n type: \"geojson\",\n data: {\n type: \"Feature\",\n properties: {},\n geometry: { type: \"LineString\", coordinates: [] },\n },\n });\n\n map.addLayer({\n id: layerId,\n type: \"line\",\n source: sourceId,\n layout: { \"line-join\": \"round\", \"line-cap\": \"round\" },\n paint: {\n \"line-color\": color,\n \"line-width\": width,\n \"line-opacity\": opacity,\n ...(dashArray && { \"line-dasharray\": dashArray }),\n },\n });\n\n return () => {\n try {\n if (map.getLayer(layerId)) map.removeLayer(layerId);\n if (map.getSource(sourceId)) map.removeSource(sourceId);\n } catch {\n // ignore\n }\n };\n }, [isLoaded, map, sourceId, layerId]);\n\n // When coordinates change, update the source data\n useEffect(() => {\n if (!isLoaded || !map || coordinates.length < 2) return;\n\n const source = map.getSource(sourceId) as MapLibreGL.GeoJSONSource;\n if (source) {\n source.setData({\n type: \"Feature\",\n properties: {},\n geometry: { type: \"LineString\", coordinates },\n });\n }\n }, [isLoaded, map, coordinates, sourceId]);\n\n useEffect(() => {\n if (!isLoaded || !map || !map.getLayer(layerId)) return;\n\n map.setPaintProperty(layerId, \"line-color\", color);\n map.setPaintProperty(layerId, \"line-width\", width);\n map.setPaintProperty(layerId, \"line-opacity\", opacity);\n if (dashArray) {\n map.setPaintProperty(layerId, \"line-dasharray\", dashArray);\n }\n }, [isLoaded, map, layerId, color, width, opacity, dashArray]);\n\n return null;\n}\n\nexport {\n Map,\n useMap,\n MapMarker,\n MarkerContent,\n MarkerPopup,\n MarkerTooltip,\n MarkerLabel,\n MapPopup,\n MapControls,\n MapRoute,\n};\n","type": "registry:ui","target": "components/ui/map.tsx"}diff --git a/src/app/(home)/_components/examples.tsx b/src/app/(home)/_components/examples.tsxindex 95c810f..b8f1cc9 100644--- a/src/app/(home)/_components/examples.tsx+++ b/src/app/(home)/_components/examples.tsx@@ -10,7 +10,7 @@ import {MarkerTooltip,MapRoute,} from "@/registry/map";-import { Truck, Flame, TrendingUp, Zap } from "lucide-react";+import { Truck, Flame, TrendingUp, Zap, Coffee } from "lucide-react";import { cn } from "@/lib/utils";const analyticsData = [@@ -60,11 +60,11 @@ export function Examples() {} | null>(null);return (- <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3 mt-8 animate-fade-in delay-400">+ <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-3 animate-fade-in delay-400">{/* Widget 1: Analytics - spans 2 cols, 2 rows */}<ExampleCard- label=""- className="col-span-1 sm:col-span-2 sm:row-span-2 h-[300px] lg:h-[412px]"+ label="Analytics Dashboard"+ className="col-span-1 sm:col-span-2 sm:row-span-2 h-[300px] sm:h-[432px]"delay="delay-400">{/* Stats Panel */}@@ -139,7 +139,7 @@ export function Examples() {</ExampleCard>{/* Widget 2: EV Charging */}- <ExampleCard label="EV Charging" className="h-[200px]" delay="delay-500">+ <ExampleCard label="EV Charging" className="h-[208px]" delay="delay-500"><Map center={[-122.425, 37.777]} zoom={11.5}><MapMarker longitude={-122.4194} latitude={37.7749}><MarkerContent>@@ -215,7 +215,7 @@ export function Examples() {</ExampleCard>{/* Widget 3: Navigation */}- <ExampleCard label="Locate Me" className="h-[200px]" delay="delay-600">+ <ExampleCard label="Locate Me" className="h-[208px]" delay="delay-600"><Map center={[77.5946, 12.9716]} zoom={10.5}><MapControls showZoom={false} showLocate onLocate={setUserLocation} />{userLocation && (@@ -236,7 +236,7 @@ export function Examples() {</ExampleCard>{/* Widget 4: Delivery Tracking */}- <ExampleCard label="Delivery" className="h-[200px]" delay="delay-500">+ <ExampleCard label="Delivery" className="h-[208px]" delay="delay-500"><Map center={[-0.1076, 51.517]} zoom={11.2}><MapRoutecoordinates={[@@ -273,7 +273,7 @@ export function Examples() {</ExampleCard>{/* Widget 5: Trending Now */}- <ExampleCard label="Trending" className="h-[200px]" delay="delay-600">+ <ExampleCard label="Trending" className="h-[208px]" delay="delay-600"><Map center={[-73.99, 40.735]} zoom={9.5}><MapMarker longitude={-73.9857} latitude={40.7484}><MarkerContent>@@ -335,6 +335,62 @@ export function Examples() {</MapMarker></Map></ExampleCard>++ {/* Widget 6: Nearby Cafes */}+ <ExampleCard label="Nearby" className="h-[208px]" delay="delay-700">+ <Map center={[2.3522, 48.8566]} zoom={13}>+ <MapMarker longitude={2.3522} latitude={48.8566}>+ <MarkerContent>+ <div className="bg-amber-700 rounded-full p-1.5 shadow-lg shadow-amber-700/30">+ <Coffee className="size-3 text-white" />+ </div>+ </MarkerContent>+ <MarkerTooltip>+ <div className="text-xs space-y-0.5">+ <div className="font-medium">Café de Flore</div>+ <div className="flex items-center gap-1">+ <span className="text-amber-600">★ 4.8</span>+ <span className="text-muted-foreground">• Open now</span>+ </div>+ </div>+ </MarkerTooltip>+ </MapMarker>++ <MapMarker longitude={2.3472} latitude={48.8606}>+ <MarkerContent>+ <div className="bg-amber-700 rounded-full p-1.5 shadow-lg shadow-amber-700/30">+ <Coffee className="size-3 text-white" />+ </div>+ </MarkerContent>+ <MarkerTooltip>+ <div className="text-xs space-y-0.5">+ <div className="font-medium">Les Deux Magots</div>+ <div className="flex items-center gap-1">+ <span className="text-amber-600">★ 4.6</span>+ <span className="text-muted-foreground">• Open now</span>+ </div>+ </div>+ </MarkerTooltip>+ </MapMarker>++ <MapMarker longitude={2.3592} latitude={48.8526}>+ <MarkerContent>+ <div className="bg-amber-700 rounded-full p-1.5 shadow-lg shadow-amber-700/30">+ <Coffee className="size-3 text-white" />+ </div>+ </MarkerContent>+ <MarkerTooltip>+ <div className="text-xs space-y-0.5">+ <div className="font-medium">Coutume Café</div>+ <div className="flex items-center gap-1">+ <span className="text-amber-600">★ 4.7</span>+ <span className="text-muted-foreground">• Closes 6pm</span>+ </div>+ </div>+ </MarkerTooltip>+ </MapMarker>+ </Map>+ </ExampleCard></div>);}diff --git a/src/app/globals.css b/src/app/globals.cssindex 0b37aaf..9084b82 100644--- a/src/app/globals.css+++ b/src/app/globals.css@@ -181,6 +181,7 @@.delay-400 { animation-delay: 400ms; }.delay-500 { animation-delay: 500ms; }.delay-600 { animation-delay: 600ms; }+.delay-700 { animation-delay: 700ms; }/* Shiki dual theme support for code blocks */html .shiki,diff --git a/src/components/github-button.tsx b/src/components/github-button.tsxindex 7120387..c52bc81 100644--- a/src/components/github-button.tsx+++ b/src/components/github-button.tsx@@ -36,7 +36,11 @@ export async function StarCount() {const data = await response.json();- const formattedCount = data.stargazers_count?.toLocaleString();+ const formattedCount =+ data.stargazers_count >= 1000+ ? `${(data.stargazers_count / 1000).toFixed(1)}k`+ : data.stargazers_count;+return (<>{formattedCount && (diff --git a/src/registry/map.tsx b/src/registry/map.tsxindex b679041..e5627e1 100644--- a/src/registry/map.tsx+++ b/src/registry/map.tsx@@ -89,7 +89,10 @@ function Map({ children, styles, ...props }: MapProps) {const mapInstance = new MapLibreGL.Map({container: containerRef.current,style: mapStyle,- attributionControl: false,+ renderWorldCopies: false,+ attributionControl: {+ compact: true,+ },...props,});@@ -523,7 +526,7 @@ const positionClasses = {"top-left": "top-2 left-2","top-right": "top-2 right-2","bottom-left": "bottom-2 left-2",- "bottom-right": "bottom-2 right-2",+ "bottom-right": "bottom-10 right-2",};function ControlGroup({ children }: { children: React.ReactNode }) {