shadcn-svelte map
DocsComponentsBlocks
Get Started
  • Introduction
  • Installation
Guides
  • Theming
  • Drawing
  • Accessibility
  • Performance
  • Content Security Policy
Core
  • Map
  • Control Container
Markers
  • Marker
  • Popup
  • Point Layer
Data
  • GeoJSON
  • Shapes
  • Route
  • Arc
  • Cluster Layer
  • Heatmap
Controls
  • Zoom Control
  • Compass Control
  • Scale Control
  • Fullscreen Control
  • Locate Control
  • Basemap Control
  • Search Control
  • Draw Control
More
  • Place Autocomplete
  • Utilities

Accessibility

Previous Next

How the map works with a keyboard, a screen reader, reduced motion and right-to-left pages.

<script lang="ts">
	import {
		Map,
		MapMarker,
		MapMarkerLabel,
		MapMarkerPopup,
		MapMarkerTooltip
	} from '$lib/components/ui/map/index.js';

	const CAFES = [
		{ id: 'north', name: 'North Roast', lngLat: [13.398, 52.531], hours: '7:00 to 18:00' },
		{ id: 'river', name: 'River Bean', lngLat: [13.412, 52.514], hours: '8:00 to 20:00' },
		{ id: 'park', name: 'Park Kiosk', lngLat: [13.377, 52.517], hours: '9:00 to 17:00' }
	] satisfies { id: string; name: string; lngLat: [number, number]; hours: string }[];
</script>

<Map class="size-full" viewport={{ center: [13.395, 52.521], zoom: 13, bearing: 0, pitch: 0 }}>
	{#each CAFES as cafe (cafe.id)}
		<MapMarker lngLat={cafe.lngLat} aria-label={cafe.name}>
			<MapMarkerLabel>{cafe.name}</MapMarkerLabel>
			<MapMarkerTooltip>Open {cafe.hours}</MapMarkerTooltip>
			<MapMarkerPopup aria-label={`${cafe.name} details`}>
				<p class="font-medium">{cafe.name}</p>
				<p class="text-muted-foreground">Open {cafe.hours}</p>
			</MapMarkerPopup>
		</MapMarker>
	{/each}
</Map>

Try it: click the map above, then press Tab. Focus moves through the markers. Press Enter to open a popup and Escape to close it.

The map

The map is a landmark (role="region") named by its aria-label, which defaults to “Map”. Give each map a name that says what it shows, such as “Delivery zones”. Screen readers also hear a short hint: arrow keys pan, plus and minus zoom, and Shift with the arrow keys rotates.

When the map is loading, fails to load, or the browser has no WebGL, the map says so in text instead of showing an empty box.

Keyboard

WhereKeyWhat happens
MapArrow keysPan
Map+ and -Zoom in and out
MapShift with arrow keysRotate and tilt
MarkerTabMove to the next marker or control
MarkerEnter or SpaceOpen its popup; focus moves into the popup
MarkerFocusShow its tooltip
PopupEscapeClose it and return focus to the marker
DrawingEscapeCancel the shape you are drawing
DrawingEnterFinish a line or polygon

Markers

Each Marker is a real <button>.

  • aria-label is required. Name markers so people can tell them apart: “Cafe on Main Street”, not “Marker”. A blank label falls back to “Map marker” and logs a warning.
  • A marker’s tooltip is its description (aria-describedby), so it is read after the name.
  • A marker with a popup has aria-haspopup="dialog" and aria-expanded.
  • Popups are dialogs named by their own aria-label. Closing one with the mouse also returns focus to its marker.

Points in a Point Layer are drawn on the canvas, so they cannot take focus. If people need to reach each point, also show the points as a list next to the map, or use markers for small sets.

Controls

Every control is a labelled button: “Zoom in”, “Reset bearing to north”, “Enter fullscreen”. Toggles such as the drawing tools and locate-and-follow use aria-pressed. The scale bar reads its distance, such as “Map scale: 500 m”. Menus and the search box are shadcn-svelte components, with their keyboard support.

Reduced motion

When the system asks for reduced motion, every camera move the components make, such as zooming, flying to a search result or resetting the bearing, jumps instead of animating. Your own code gets the same behaviour by moving the camera through the map context:

const ctx = getMapContext();
ctx.easeTo({ center: [2.35, 48.86], zoom: 12 });

Marker hover effects and the locate pulse also stop.

Right-to-left pages

The buttons, menus, search box and switches around the map follow the page direction. Control positions are physical corners: top-right stays on the right in a right-to-left page, so pick the corner you want for each layout.

Colour

All map colours come from your theme, so the contrast you choose for your shadcn components carries over. Check any colours you pass yourself against the basemap in both light and dark mode.

Drawing Performance

On This Page

The mapKeyboardMarkersControlsReduced motionRight-to-left pagesColour

Handcrafted by Oggy. The source code is on GitHub.