shadcn.io is an independent project built on shadcn/ui
Components
Free
Map for React Aria
A MapLibre map for React with React Aria: your own markers and popups, routes, GeoJSON and clusters, on a basemap that follows your theme.
On this page9 sections
The Map is an interactive MapLibre GL (opens in a new tab) map that people pan, zoom and turn, with your own markers and popups placed on it and routes, shapes, arcs, clusters and heat drawn into it. Reach for it when where things are is the point: the stores near someone, a delivery on its way, where your customers are.
The basemap is CARTO's, needs no API key and is drawn in your theme's colors. For numbers with no place attached, use a Chart. The parts follow mapcn (opens in a new tab) by Anmoldeep Singh (MIT), redesigned.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
MapLibre makes the map's canvas a focusable region named Map, which a locale in options can rename, and the keys below move its camera. A marker that's draggable, has a popup, or has a click handler on its MapMarkerContent is a button. Give every marker a label: it names that button, or the marker as an image when it's none of these. A MapMarkerTooltip becomes the marker's description for screen readers, but it shows only on hover or keyboard focus, which only a marker that's a button gets. The controls are labeled buttons. Routes, shapes, arcs, clusters and heat are drawn in the canvas and aren't read out, so list what they show beside the map.
KeyAction
Tab
Moves focus to the map, then to the markers that are buttons and to the controls, in the order they're written.
Arrow Up
On the map, pans the view 100 pixels up. On a draggable marker, moves the marker 10 pixels up.
Arrow Down
On the map, pans the view 100 pixels down. On a draggable marker, moves the marker 10 pixels down.
Arrow Left
On the map, pans the view 100 pixels left. On a draggable marker, moves the marker 10 pixels left.
Arrow Right
On the map, pans the view 100 pixels right. On a draggable marker, moves the marker 10 pixels right.
Shift+Arrow Left
On the map, turns it 15 degrees, the other way from Shift + ArrowRight. On a draggable marker, moves the marker 40 pixels left.
Shift+Arrow Right
On the map, turns it 15 degrees. On a draggable marker, moves the marker 40 pixels right.
Shift+Arrow Up
On the map, tilts it 10 degrees further. On a draggable marker, moves the marker 40 pixels up.
Shift+Arrow Down
On the map, tilts it 10 degrees back toward looking straight down. On a draggable marker, moves the marker 40 pixels down.
=
On the map, zooms in one level, or two with Shift held, which is the + key on a US keyboard.
-
On the map, zooms out one level, or two with Shift held.
Enter
On a marker with a popup, opens or closes the popup. Opening it moves focus into the popup. On any other marker whose content has a click handler, runs it.
Space
On a marker with a popup, opens or closes the popup. Opening it moves focus into the popup. On any other marker whose content has a click handler, runs it.
EscEscape
Closes a marker's popup while focus is on that marker or in its popup, and hands focus back to the marker. Closes a MapPopup from anywhere in the map, and hands focus to the map if the popup held it.
Demos
Markers
Lisbon in four kinds of marker: landmark pins standing on their tips with anchor at bottom, a market and a café as icons in circles, price pills with a MapMarkerTooltip for each stay, and friends' avatars. MapMarkerContent holds any element, or draws a primary dot when it's empty. MapMarkerLabel writes a name under a marker, or above it with position at top, haloed so it reads on any basemap.
Popups
Three Roman sights whose markers open a MapMarkerPopup with a description, opening hours, a price and a Save button, the Pantheon's open from the start with defaultOpen. A click on the map itself drops a pin through onMapClick and opens a MapPopup there with the point's coordinates. That popup is held open from state, with closeOnClick off so the next click moves it instead of closing it.
Controls
Midtown Manhattan, turned and tilted, with a compass and a fullscreen button in the top corner and zoom and locate in the bottom one, each MapControlGroup reading as one stack. MapCompass follows the camera, and a click turns the map back to north and looks straight down. MapLocate flies to the device's position and hands it to onLocate, which drops the You are here marker.
Draggable marker
A pickup pin in Union Square with draggable set: drag it, or focus it and press the arrow keys, 10 pixels a press or 40 with Shift. onPositionChange keeps the card's latitude and longitude in step, and the reset button moves the pin back by changing latitude and longitude. While a pointer drags it, the marker has data-dragging, which this pin uses to lift itself.
Controlled viewport
Buttons for Tokyo, New York, London and Sydney fly the camera with MapLibre's flyTo, reached through useMap. Meanwhile viewport and onViewportChange keep the camera in state, so the latitude, longitude and zoom under the map follow every frame. Hold the viewport yourself when other parts of the page read or set it, and use defaultViewport when you only need a starting point.
Camera
Six Lisbon viewpoints, framed together by MapCamera with bounds, or one at a time with center and zoom once a row in the list or a marker is picked. Each new aim flies the camera there over duration, 1200 milliseconds by default, and jumps where reduced motion is set. padding keeps 136 pixels clear at the bottom for the list, and maxZoom caps how close bounds bring the camera.
Route
A bike courier's delivery across London, from Hoxton Kitchen to 31 Drury Lane, drawn by MapRoute as a dotted line. progress says how much is done: MapRouteProgress draws that part in the primary color, and a MapRouteMarker at progress rides at its tip while two others mark the start and the end. The demo moves progress every frame, and holds it still when reduced motion is set.
Alternative routes
Three drives from Berlin Hauptbahnhof to Ostbahnhof, one of them active: it's drawn with activeColor, activeWidth and activeOpacity and raised over the others. With interactive set, a click on a line calls onRouteClick, which makes that route the active one. A line on the map answers only the pointer, so the rows in the card pick a route from the keyboard as well.
GeoJSON
Natural Earth's countries, fetched from the URL in data, on a blank map with no tiles under them. fillPaint takes a MapLibre step expression that shades each country by GDP per person, from var(--chart-1) to var(--chart-5). fillHoverPaint lights the country under the pointer, which needs the feature ids promoteId names, and onFeatureHover feeds the card.
Arcs
Eight flights out of Dubai as curved lines, each arc in data with an id, a from and a to. curvature is 0.2 by default, and this demo's negative value bows the arcs the other way. hoverPaint thickens the arc under the pointer, and onArcHover reports it, or null once the pointer leaves, so the card and the destination's marker follow.
Clusters
A month of earthquakes, 6,107 of them, fetched by MapLibre from a URL and grouped by MapClusterLayer into counted circles that split as you zoom in. A click on a cluster zooms into it. clusterThresholds turns a cluster medium at 50 and large at 300, clusterColors mixes three shades of the primary color, and onPointClick opens a MapPopup with one quake's magnitude and time.
Heatmap
Bike-share stations round Barcelona as heat that runs hotter where they crowd. With Rides pressed, weightProperty weighs each station by its rides, up to a weightMax of 500, and with Stations every point counts the same. The colors run through the theme's --chart-1 to --chart-5, and past maxZoom the heat fades into single dots.
Globe
An edge network's ten regions on a globe, set with projection at globe, which flattens into the usual map as you zoom in close. Markers on the far side hide and come back as the globe turns them into view. The demo spins it through MapLibre's map from onReady until someone presses, scrolls or touches it, and leaves it still when reduced motion is set.
Themed basemap
Amsterdam with a Theme and an Original button. With basemap at theme, the default, CARTO's basemap is redrawn in four of your tokens: the land from --background and --muted, the labels from --muted-foreground and --foreground, and roads, buildings and water in shades of the same four, the water keeping a trace of its own hue. original keeps CARTO's own colors. Switch here, then change your theme, and the map follows.
Custom layer
Midtown Manhattan with its buildings raised in 3D by a component that calls useMap() and, once ready is true, adds a MapLibre fill-extrusion layer reading each building's height from CARTO's tiles. A theme change loads the other basemap, which drops the layer, and the same effect adds it back. Its toggle is an ordinary Button in a MapControlGroup, beside zoom and the compass.
FAQ
Which basemap does the Map use, and how do I change it?
CARTO's Positron in light mode and Dark Matter in dark: vector tiles built on OpenStreetMap data, with no API key. They're free under CARTO's basemap terms, which set usage limits and ask for the credit the map shows in its bottom corner.
For a busy app in production or another look, pass styles a MapLibre style URL or style object for each theme, from OpenFreeMap, MapTiler, Stadia Maps, Protomaps or your own server. A theme you leave out stays on CARTO's, and blank draws no tiles at all.
Whichever basemap you use is redrawn in your theme's colors unless basemap is original. To lift the credit above a panel of your own, set --map-credits-bottom on the map.
How tall is the Map, and does it render on the server in React?
MapView is h-96 and as wide as its container until a class says otherwise, such as h-[480px], or h-full inside a parent with a height. On the server, and until the map first draws, it renders that box with a spinner, so the page doesn't shift when the map arrives.
MapLibre loads with the first map on the page, and the markers, popups, controls and layers render once the map exists. It needs WebGL2: where MapLibre can't start, the box says the map couldn't load instead of staying blank.
How does the Map follow dark mode and my theme's colors?
It reads the --background of the map's own box: a dark color draws Dark Matter, a light one Positron. So a .dark class or a data-theme anywhere above the map works, and so does the system setting, and a change to any of them is followed. Set theme to light or dark to choose yourself.
Every color a part takes can be a CSS variable, such as var(--chart-2) on a route or var(--primary) inside a paint expression. The parts read them on the map's box and again when the theme changes, and the Map's own routes and layers come back after the basemap swap.
How many markers can a Map show?
Each MapMarker is a DOM element the map moves on every frame of a camera move, which suits the tens of places a person picks between: stores, stops, friends.
For hundreds or thousands of points, draw them inside the map. MapClusterLayer groups them into counted circles, MapGeoJSON draws them as dots styled by circlePaint, and MapHeatmap shows where they crowd. All three take a URL in data, so MapLibre fetches a large file and your state never holds it.
To show one point's details, open a MapPopup where it was clicked, as the Clusters demo does.
Can people use the Map with a keyboard and a screen reader?
Mostly. The map is a focusable region that the arrow keys pan, the zoom keys zoom and Shift with an arrow turns or tilts. A marker with a popup, a drag or a click handler is a button, its popup opens on Enter and closes on Escape, and the Accessibility section lists every key.
What MapLibre draws in the canvas isn't read out: routes, shapes, arcs, clusters and heat. List what they show beside the map, as the route demos' cards do, and give each marker a label. A tooltip reaches screen readers as the marker's description but shows only on hover or keyboard focus, so put what people need in text beside the map too.
How do I fit the Map to show all my markers?
Render MapCamera inside MapView with bounds set to the corners round your markers, [[west, south], [east, north]], taken from the smallest and largest longitude and latitude among them. padding keeps room at the edges, 48 pixels by default, or one value per side when a panel covers part of the map.
maxZoom stops the camera from coming too close when the markers sit near each other or there's only one. When the bounds change, the camera flies to the new frame, as the Camera demo does with its All viewpoints row.
How do I use MapLibre directly inside the Map?
Call useMap() in a component inside MapView. It returns map, MapLibre's map once it exists, ready, true once the basemap has drawn, theme, and controller, whose resolveColor turns a theme variable into a color MapLibre can paint.
With map you can do anything MapLibre does, flyTo and map.on included. Add your own sources and layers in an effect that runs when ready turns true. A theme change loads the other basemap, which drops them and turns ready false until it has drawn, so the same effect puts them back, as the Custom layer demo does.
I used mapcn. What changed?
The root is MapView, since Map would hide JavaScript's own Map. The camera starts from defaultViewport or follows viewport with onViewportChange, MapLibre's other options go in options, and onReady or useMap() hands you MapLibre's map in place of a ref.
Every part's name starts with Map, such as MapMarkerContent and MapRouteProgress. MapControls takes its buttons as children, in a MapControlGroup, instead of props, and its position follows the line: bottom-end where mapcn says bottom-right. Popups open and close with open, defaultOpen and onOpenChange.
A globe needs only projection set to globe, and a drag reports its latitude and longitude. MapCamera, MapHeatmap, onMapClick and the themed basemap are new.
API Reference
MapView
PropType
viewport?Partial<MapViewport>
The camera, held where you say (with onViewportChange).
Type
Partial<MapViewport>
defaultViewport?Partial<MapViewport>
Where the camera starts, when viewport isn't given.
Type
Partial<MapViewport>
onViewportChange?(viewport: MapViewport) → void
Called on every frame the camera moves.
Parameters
viewport – MapViewport
Returns
void
basemap?MapBasemap
theme draws the basemap in your theme's colors (land, water, roads and labels from its background, muted and foreground tokens), through dark mode and preset changes; original keeps the style's own colors. @default "theme".
Default
"theme"
blank?boolean
A clear basemap with no tiles, for your own layers on the page's ground. @default false.
Default
false
loading?boolean
Shows the loading indicator over the map, as while it first draws. @default false.
Default
false
onMapClick?(position: MapLngLat) → void
Called on a click on the map itself (not on a marker or a popup), with where.
Parameters
position – MapLngLat
Returns
void
onReady?(map: MapInstance) → void
Called once the map has drawn, with MapLibre's map.
A MapLibre layer to draw under, such as the basemap's labels.
colors?string[]
From the least density to the most, any CSS colors. @default the theme's chart colors, hottest away from the background.
intensity?number
How hot the heat runs: 2 is twice as hot. @default 1.
Default
1
maxZoom?number
Past this zoom the heat fades out and each point shows instead; heat at every zoom when not given.
opacity?number
The heat's opacity, from 0 to 1. @default 0.85.
Default
0.85
radius?number
Each point's reach in pixels at zoom 10; half at zoom 5, twice at 15. @default 24.
Default
24
weightMax?number
The highest weight: a point weighing this counts fully. @default 1.
Default
1
weightProperty?string
The property each point is weighted by, from 0 to weightMax; every point counts the same when not given.
MapCamera
<span>data-slot="map-camera"
PropType
bearing?number
Rotation, in degrees clockwise from north.
bounds?[MapCoordinate, MapCoordinate]
Frames these corners, [[west, south], [east, north]], instead of center and zoom.
center?MapCoordinate
Where it looks, as [longitude, latitude].
duration?number
How long a flight to a new aim takes, in ms; none where motion is reduced. @default 1200.
Default
1200
maxZoom?number
The closest it comes when framing bounds, and how close it looks at bounds round a single point.
padding?MapPadding
Room round what it frames, in pixels: a number, or one per side (for a panel over the map), with start and end following the direction (RTL) or left and right. @default 48.
Default
48
pitch?number
Tilt, in degrees from looking straight down.
revision?number | string
Change it to fly to the same aim again, as when the place already chosen is pressed after the map was moved.