<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
| Name | Type | Description |
|---|---|---|
lngLat required | LngLatLike | Where the popup points. |
open bindable | boolean | Open state. Bind it to hear about the close button, Escape and map clicks. Defaults to true. |
offset | PopupOffset | Distance from `lngLat` in pixels, or per-anchor offsets. Defaults to 0. |
maxWidth | string | CSS max-width of the popup. Defaults to DEFAULT_POPUP_MAX_WIDTH. |
anchor | PositionAnchor | Pin the popup to one side. Read once; leave unset to fit the view. |
closeButton | boolean | Show a close button. Read once. Defaults to true. |
closeOnClick | boolean | Close when the map is clicked. Read once. Defaults to true. |
focusAfterOpen | boolean | Move focus into the popup when it opens. Read once. Defaults to true. |
ref bindable | Popup | null | The MapLibre popup instance. Defaults to null. |
Events
| Name | Type | Description |
|---|---|---|
onopen | () => void | Called when the popup opens. |
onclose | () => void | Called when the popup closes. |
Snippets
| Name | Type | Description |
|---|---|---|
children | Snippet |
Other attributes go to the underlying <div>.