shadcn-map
repository·main·Indexed 43 days ago
https://github.com/tonghohin/shadcn-mapA specialized map component library for shadcn/ui built with Leaflet and React Leaflet. Version 0.1.0 provides a suite of components including Map, MapTileLayer with automatic theme switching via next-themes, MapMarker with clustering, and various drawing and control tools like MapZoomControl, MapFullscreenControl, and MapSearchControl.
What's inside shadcn-map
- shadcn/ui Map is a map component library built using Leaflet and React Leaflet. It is specifically designed to integrate seamlessly with the shadcn/ui ecosystem, providing a map interface that follows shadcn's design language and component patterns.
Manage multiple layers with MapLayers and MapLayersControl
mainTo allow users to switch between different tile layers or toggle layer groups, you must use the
MapLayerscontext and theMapLayersControlUI component.MapLayers (Context Provider)
Required when using multiple tile layers or layer groups together, especially if you want to use
MapLayersControl.defaultTileLayer: Thenameof theMapTileLayerto select by default.defaultLayerGroups: An array ofnames of theMapLayerGroups to activate by default.
MapLayersControl (UI Component)
Renders a UI for selecting the active tile layer and toggling layer groups.
- Note: This component is only visible if there are multiple tile layers or layer groups registered.
tileLayersLabel: Custom label for the tile layers section (Defaults toMap Type).layerGroupsLabel: Custom label for the layer groups section (Defaults toLayers).position: Custom position (Defaults totop-1 right-1).- Must be used inside a
MapLayerscomponent.
<Map center={[51.5, -0.09]}> <MapLayers defaultTileLayer="Satellite"> <MapTileLayer name="Street" url="..." /> <MapTileLayer name="Satellite" url="..." /> <MapLayersControl /> </MapLayers> </Map>Group layers with MapLayerGroup and MapFeatureGroup
mainUse these components to manage the visibility of multiple layers.
MapLayerGroup
Used for grouping multiple map layers to control their visibility as a single unit.
name(Required): A unique name for the group.disabled: Iftrue, prevents users from toggling this group in the layer control.
MapFeatureGroup
Similar to
MapLayerGroup, but provides extra capabilities:- You can bind popups or tooltips to all child layers at once.
- Events on the group automatically propagate to its child layers.
name(Required): A unique name for the group.disabled: Iftrue, prevents users from toggling this group in the layer control.
Use MapControlContainer to prevent unintended map interactions
mainWhen adding custom UI elements (controls) on top of the map, wrap them in theMapControlContainercomponent. This prevents user interactions with your UI—such as double-clicking, dragging, or scrolling—from being passed through to the map and triggering unintended actions like panning or zooming.Toggle visibility of Layer Groups with MapLayersControl
mainTo group related markers or features and allow users to toggle their visibility, wrap
MapLayerGroupcomponents inside aMapLayerscontainer and include theMapLayersControlcomponent.Use the
defaultLayerGroupsprop on theMapLayerscontainer to specify which groups should be visible when the map first loads.<MapLayers defaultLayerGroups={['group-id-1']}> <MapLayerGroup id="group-id-1"> {/* Markers or features here */} </MapLayerGroup> <MapLayersControl /> </MapLayers>Handle location success and error callbacks in Locate Control
mainYou can pass optional callback functions to the
LocateControlto react when the user's location is successfully retrieved or when an error occurs (e.g., if the user denies permission).// Example pattern for using callbacks with Locate Control <LocateControl onLocationSuccess={(location) => { console.log('User location:', location); }} onLocationError={(error) => { console.error('Failed to get location:', error); }} />Customize map control positions using the `position` prop
mainBy default, map control components have predefined positions. To override these defaults and place controls anywhere on the map, pass an optionalpositionprop to the control component. This allows you to use CSS to achieve flexible or complex control layouts.Toggle visibility of Feature Groups with MapLayersControl
mainMapFeatureGroupfunctions similarly toMapLayerGroup. Wrap your feature groups in aMapLayerscontainer and includeMapLayersControlto provide a UI for toggling their visibility.Use DrawControl to draw shapes on the map
mainTheDrawControlcomponent allows users to draw shapes directly on the map. You can customize which shapes are available by including only specific shape controls inside theMapDrawControlcomponent.Install shadcn-map component
mainTo add the map component to your project, use the shadcn CLI. This component is built using Leaflet and React Leaflet, following shadcn/ui design patterns.
Prerequisite: You must have shadcn/ui already installed and configured in your project.
npx shadcn@latest add @shadcn-map/mapHandle selected places with onPlaceSelect
mainTo react when a user selects a specific place from the autocomplete results, pass an
onPlaceSelectcallback to the search control.Important: Because
useMapcan only be used inside aMapcontainer, you must wrap the search control in a client-side wrapper component if you intend to perform map actions (like panning, zooming, or adding markers) based on the selection.// Example of a client-side wrapper component const MapSearchWrapper = () => { const map = useMap(); const handlePlaceSelect = (place: any) => { // Perform map actions like panning to the selected location map.panTo(place.location); // Or add a marker }; return ( <SearchControl onPlaceSelect={handlePlaceSelect} /> ); };Access search results with onResultsChange
mainYou can use the
onResultsChangecallback to receive the list of search results as the user types. This is useful for displaying markers on the map for every result returned by the search engine.<SearchControl onResultsChange={(results) => { // Handle results, e.g., displaying markers for each result console.log(results); }} />