<script lang="ts">
import type { FeatureCollection, Polygon } from 'geojson';
import { Map, MapGeoJSON } from '$lib/components/ui/map/index.js';
function box(name: string, west: number, south: number, east: number, north: number) {
return {
type: 'Feature' as const,
properties: { name },
geometry: {
type: 'Polygon' as const,
coordinates: [
[
[west, south],
[east, south],
[east, north],
[west, north],
[west, south]
]
]
}
};
}
const ZONES: FeatureCollection<Polygon, { name: string }> = {
type: 'FeatureCollection',
features: [
box('Delivery zone A', -0.16, 51.5, -0.12, 51.525),
box('Delivery zone B', -0.115, 51.5, -0.075, 51.525),
box('Delivery zone C', -0.16, 51.475, -0.12, 51.497),
box('Delivery zone D', -0.115, 51.475, -0.075, 51.497)
]
};
let hovered = $state<string | null>(null);
</script>
<div class="relative size-full">
<Map class="size-full" viewport={{ center: [-0.1175, 51.5], zoom: 11.5, bearing: 0, pitch: 0 }}>
<MapGeoJSON
data={ZONES}
promoteId="name"
onhover={(feature) => (hovered = feature ? String(feature.properties.name) : null)}
/>
</Map>
<p
class="absolute top-3 left-3 rounded-md bg-background/90 px-2 py-1 text-xs shadow"
aria-live="polite"
>
{hovered ?? 'Hover a zone'}
</p>
</div>
MapGeoJSON draws a GeoJSON Feature or FeatureCollection. Polygons get a fill and an outline, lines get a line, and points get a circle. The hovered feature is highlighted.
Usage
<script lang="ts">
import type { FeatureCollection } from 'geojson';
import { Map, MapGeoJSON } from '$lib/components/ui/map/index.js';
let { zones }: { zones: FeatureCollection } = $props();
</script>
<Map class="h-96">
<MapGeoJSON data={zones} onclick={(feature) => console.log(feature.properties)} />
</Map>Data
data takes a GeoJSON object or a URL. With a URL, MapLibre fetches the file itself. If your site has a Content Security Policy, its connect-src must allow that host.
Change data and the source updates in place. The layers are not rebuilt.
Hover and click
onhover gets the feature under the pointer, and null when the pointer leaves. onclick gets the clicked feature and the map event.
The hovered feature is drawn with hoverFillOpacity, hoverLineWidth and hoverCircleRadius. Hover tracks features by id. By default ids are positions in the data, so the hover clears when data changes. Set promoteId to a property name, such as "name", to use that property as the id instead.
Look
Fills, lines and points use the theme’s --primary colour by default and follow the theme. Set fillColor, lineColor and circleColor to change them. The layers survive a basemap or theme swap.
Layer ids
The source id comes from id, or a unique id from $props.id(). The layers are <id>-fill, <id>-line and <id>-circle. Polygon outlines are drawn by the line layer. Pass beforeId to draw below another layer; changing it moves the layers.
Keyboard
When the layer has onclick or onhover, its features are also offered as a list for keyboard and screen reader users, one tab stop on the map. Moving through the list highlights each shape, calls onhover and pans it into view. Enter calls onclick with the feature and no mouse event. Features are named by their name property; pass featureLabel and listLabel to name them another way, or keyboard={false} to leave the list out. Data loaded from a URL is not listed.
API Reference
MapGeoJSON
Props
| Name | Type | Description |
|---|---|---|
data required | FeatureCollection | Feature | string | GeoJSON to draw, or a URL to load it from. Changing it updates the source in place. |
id | string | Source id; layers are `<id>-fill`, `<id>-line` and `<id>-circle`. Read once. |
promoteId | string | Use this feature property as the feature id for hover state. Read once. |
fillColor | string | Polygon fill. Defaults to the theme's `--primary`. |
fillOpacity | number | Polygon fill opacity, 0 to 1. Defaults to 0.2. |
hoverFillOpacity | number | Fill opacity of the hovered feature. Defaults to 0.4. |
lineColor | string | Lines and polygon outlines. Defaults to the theme's `--primary`. |
lineWidth | number | Line width in pixels. Defaults to 2. |
hoverLineWidth | number | Line width of the hovered feature. Defaults to 3. |
circleColor | string | Points. Defaults to the theme's `--primary`. |
circleRadius | number | Point radius in pixels. Defaults to 5. |
hoverCircleRadius | number | Point radius of the hovered feature. Defaults to 7. |
beforeId | string | Draw below this layer when it exists; changing it moves the layers. |
keyboard | boolean | Offer the features to keyboard and screen reader users as a list, one tab stop on the map. Only added when `onclick` or `onhover` is set and `data` is not a URL. Defaults to true. |
listLabel | string | Accessible name of the keyboard list, such as "Delivery zones". Defaults to 'Shapes'. |
featureLabel | (feature: Feature, index: number) => string | Name of a feature in the keyboard list. Defaults to its `name` property. |
Events
| Name | Type | Description |
|---|---|---|
onclick | (feature: MapGeoJSONFeature, event?: MapMouseEvent) => void | Called with the clicked feature. `event` is left out when the feature was chosen from the keyboard list. |
onhover | (feature: MapGeoJSONFeature | null) => void | Called when the pointer moves onto a feature, or keyboard focus reaches it, and with `null` when it leaves. |