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

Popup

Previous Next

A popup with your own content at any position on the map.

<script lang="ts">
	import { Button } from '$lib/components/ui/button/index.js';
	import { Map, MapPopup } from '$lib/components/ui/map/index.js';

	const STATIONS = [
		{
			id: 'central',
			name: 'Central Station',
			lngLat: [4.9003, 52.3791],
			note: 'Trains every 5 minutes'
		},
		{ id: 'zuid', name: 'Zuid', lngLat: [4.8718, 52.3389], note: 'Metro and airport trains' },
		{ id: 'amstel', name: 'Amstel', lngLat: [4.9178, 52.3463], note: 'Buses to the east' }
	] satisfies { id: string; name: string; lngLat: [number, number]; note: string }[];

	let current = $state(STATIONS[0]);
	let open = $state(true);

	function show(station: (typeof STATIONS)[number]) {
		current = station;
		open = true;
	}
</script>

<div class="relative size-full">
	<Map class="size-full" viewport={{ center: [4.895, 52.36], zoom: 12, bearing: 0, pitch: 0 }}>
		<MapPopup
			lngLat={current.lngLat}
			bind:open
			focusAfterOpen={false}
			aria-label={`${current.name} details`}
		>
			<p class="font-medium">{current.name}</p>
			<p class="text-muted-foreground">{current.note}</p>
		</MapPopup>
	</Map>
	<div class="absolute top-3 left-3 flex flex-wrap gap-2">
		{#each STATIONS as station (station.id)}
			<Button size="sm" variant="secondary" onclick={() => show(station)}>{station.name}</Button>
		{/each}
	</div>
</div>

MapPopup shows content at a coordinate, without a marker. Use it for things like search results or a clicked spot on the map. To attach a popup to a marker, use MapMarkerPopup from Marker instead.

Usage

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

	let open = $state(true);
</script>

<Map class="h-96">
	<MapPopup lngLat={[13.405, 52.52]} bind:open aria-label="Meeting point">
		<p>Meet at the fountain.</p>
	</MapPopup>
</Map>

The content goes in a role="dialog" element. Give it an aria-label so screen readers can name it.

Opening and closing

open is true by default. Bind it to hear when the user closes the popup with the close button, Escape or a click on the map. Set it to true again to reopen it. The content stays mounted while the popup is closed.

Change lngLat and the popup moves. It is not rebuilt.

Focus

When the popup opens, focus moves into it. If your popup opens on page load, set focusAfterOpen={false} so the page does not scroll to it.

Read once

anchor, closeButton, closeOnClick and focusAfterOpen are read when the popup is created. Changing them later has no effect. offset, maxWidth and class update the live popup.

API Reference

MapPopup

Props

NameTypeDescription
lngLat required LngLatLikeWhere the popup points.
open bindablebooleanOpen state. Bind it to hear about the close button, Escape and map clicks. Defaults to true.
offset PopupOffsetDistance from `lngLat` in pixels, or per-anchor offsets. Defaults to 0.
maxWidth stringCSS max-width of the popup. Defaults to DEFAULT_POPUP_MAX_WIDTH.
anchor PositionAnchorPin the popup to one side. Read once; leave unset to fit the view.
closeButton booleanShow a close button. Read once. Defaults to true.
closeOnClick booleanClose when the map is clicked. Read once. Defaults to true.
focusAfterOpen booleanMove focus into the popup when it opens. Read once. Defaults to true.
ref bindablePopup | nullThe MapLibre popup instance. Defaults to null.

Events

NameTypeDescription
onopen () => voidCalled when the popup opens.
onclose () => voidCalled when the popup closes.

Snippets

NameTypeDescription
children Snippet

Other attributes go to the underlying <div>.

Marker Point Layer

On This Page

UsageOpening and closingFocusRead onceAPI Reference

Handcrafted by Oggy. The source code is on GitHub.