Popups, hover & controls
A map from YAML is already pannable and zoomable. This guide adds the parts
that answer a reader’s “what is this?”: a popup when they click a feature, a
highlight and a tooltip while they point at one, a camera that moves to what
they chose, and the buttons around the map. It all goes in a layer’s
interactive: block or the document’s controls: block, with no JavaScript.
Complete documents (they start with type: map) run as printed. Snippets
that start with popup:, interactive: or controls: are one part of a
document.
Show a popup on click
Section titled “Show a popup on click”Give the layer an interactive: block with a click.popup. The popup is a
list of HTML elements, and each one lists what goes inside it:
type: mapid: citiesconfig: center: [-82, 41] zoom: 4 mapStyle: "https://tiles.openfreemap.org/styles/liberty"layers: - id: cities type: circle source: type: geojson data: type: FeatureCollection features: - type: Feature geometry: { type: Point, coordinates: [-74.006, 40.7128] } properties: { name: "New York", population: 8336817, nickname: "The Big Apple" } - type: Feature geometry: { type: Point, coordinates: [-87.623, 41.881] } properties: { name: "Chicago", population: 2693976 } paint: circle-radius: 10 circle-color: "#ef4444" circle-stroke-width: 2 circle-stroke-color: "#ffffff" interactive: hover: cursor: pointer click: popup: - h3: [{ property: name }] - p: - str: "Population: " - property: population format: { max-fraction-digits: 0 }Click New York and the popup reads New York over “Population:
8,336,817”. cursor: pointer turns the pointer into a hand over the circles,
so readers can tell they are clickable. One popup is open at a time; clicking
elsewhere closes it.
Choose what the popup shows
Section titled “Choose what the popup shows”- h3: [{ property: name }] becomes <h3>New York</h3>: the key is the
element, and its list holds items whose output is joined inside it.
| Item | Shows |
|---|---|
str: "Population: " | The text as written |
property: population | The clicked feature’s population property |
property: nickname with else: "none recorded" | The fallback when the property is missing or null (without else, nothing) |
property: population with format: { … } | A formatted number (below) |
href: "https://…" with text: "Read more" | A link with that text (the URL itself without text) |
src: "https://…/photo.jpg" with alt: "…" | An image |
popup: - h3: [{ property: name }] - p: - str: "Nickname: " - property: nickname else: "none recorded" - p: - href: "https://en.wikipedia.org/wiki/New_York_City" text: "Read about it on Wikipedia" - p: - src: "https://maplibre.org/img/maplibre-logo-big.svg" alt: "MapLibre logo"- Elements:
h1–h6,p,span,div,strong,em,code,pre,a,img,ul,ol,li,blockquote,hrandbr. Any other key is skipped with a console warning. Elements don’t nest: aul:can’t containli:entries. Put links and images insidep:ordiv:. - Text is always text. Property values and
strare shown as text, never as markup, so data can’t inject HTML into your page. Astrtagged!html(str: !html "<em>new</em>") is markup only on a page that trusts the document;<ml-map>doesn’t, so it shows the tags as text and logs a warning. JSON and TOML have no tags, so they write the same value as an object:{ "$html": "<em>new</em>" }in JSON,{ "$html" = "<em>new</em>" }in TOML (JSON & TOML). It is the value!htmlproduces, so the same trust rule applies. - Links take an absolute URL (
https://,mailto:,tel:) or a path on your site (/about,notes.html,#top); any other scheme, such asjavascript:, is shown as plain text. A link opens in a new tab unless you writetarget: _self(or_parent,_top). Its URL is fixed text: it can’t be built from a property.
Every popup key is in the layer reference.
Format numbers
Section titled “Format numbers”A property item with format: formats a number in the reader’s language.
The options are those of MapLibre’s
number-format
expression, the same ones a label’s text-field uses:
| Option | What it does | Example |
|---|---|---|
locale | The language to format for. Default: the page’s language, then the browser’s | de-DE |
currency | Format as money | EUR, USD, JPY |
unit | Add a unit (ignored with currency) | kilometer, percent, celsius |
min-fraction-digits | At least this many decimals | 2 |
max-fraction-digits | At most this many decimals (rounds) | 0 |
popup: - p: - str: "Population: " - property: population format: { max-fraction-digits: 0 } # 8,336,817 - p: - str: "Median rent: " - property: rent format: { currency: EUR, max-fraction-digits: 0 } # €1,240 - p: - str: "Area: " - property: area_km2 format: { unit: kilometer, max-fraction-digits: 1 } # 12.5 km- The page’s language decides. The
langon<ml-map>or on<html>comes first, then the browser’s. On a page with<html lang="de">the population reads “8.336.817” and the rent “1.240 €”. Writelocale: en-USin a format to fix one regardless of the page. - Text that reads as a number (
"54000", as CSV-derived data often has) is formatted like the number; other text ("n/a") is shown as stored. - Without
format:a property is shown exactly as stored, so years and ids (1999,10012) are never grouped. - A d3-style string such as
format: ",.0f"also works (,groups thousands,.Nfkeeps N decimals) and follows the page’s language too.
The same formatter is exported for your own code as
formatNumber(value, options, defaultLocale) from @maplibre-yaml/core.
A classified layer’s legend takes the same options.
Respond to hover
Section titled “Respond to hover”The hover: part of interactive: takes three things, alone or together:
cursor: the CSS cursor over a feature, usuallypointer.highlight: true: recolours the feature under the pointer.popup: a tooltip, in the same format asclick.popup, shown while the pointer is over a feature and gone when it leaves. It has no close button. A popup opened by a click stays, and hover tooltips wait until it is closed. Touch screens have no hover: a tap opensclick.popup.
type: mapid: statesconfig: center: [-98, 39] zoom: 3 mapStyle: "https://tiles.openfreemap.org/styles/liberty"layers: - id: states type: fill source: type: geojson url: "https://maplibre.org/maplibre-gl-js/docs/assets/us_states.geojson" promoteId: STATE_ID paint: fill-color: "#94a3b8" fill-opacity: 0.6 interactive: hover: cursor: pointer highlight: true popup: - strong: [{ property: STATE_NAME }] click: zoomToFeature: padding: 40highlight needs an id on each feature. promoteId names a property that is
different for every feature (STATE_ID here), which keeps the same id when
the data refreshes. generateId: true numbers the features instead. Leave
both out and maplibre-yaml turns on generateId for you, whether the data is
written in the YAML or loaded from url: (core 0.9.1 or later). Write
generateId: false to keep the ids your data has. Vector tiles (PMTiles,
tiles:) use the ids in the tiles, or promoteId: { <source-layer>: <property> }
when they have none (core 0.9.2 or later).
highlight changes the layer’s main colour (fill-color here), and works on
classified layers too. If that colour is already an
expression of your own, highlight leaves it alone and warns; to choose the
highlight colour yourself, use ["feature-state", "hover"] in the paint
(below). Hover state belongs to the source, so
layers that share a source share it
too. Gallery: Create a hover effect,
Display a popup on hover.
Choose the highlight colour yourself
Section titled “Choose the highlight colour yourself”Leave highlight off and write the colour as a case on the feature’s
hover state. cursor: pointer alone keeps the hover tracking on:
- id: states type: fill source: states paint: fill-color: - case - ["boolean", ["feature-state", "hover"], false] - "#2563eb" # under the pointer - "#cbd5e1" # everywhere else fill-opacity: 0.7 interactive: hover: cursor: pointerWithout highlight, ids aren’t generated for you: give the source
promoteId or generateId: true.
Make thin lines and small points easier to hit
Section titled “Make thin lines and small points easier to hit”A click or hover counts only when the pointer is on a drawn pixel of the
feature, so a 2 px trail or a 3 px dot is hard to catch. Give click: or
hover: a tolerance in pixels, and the pointer may be that far from the
feature and still count as on it:
layers: - id: trails type: line source: trails paint: { line-width: 2 } interactive: click: tolerance: 6 popup: - h3: [{ property: name }] hover: tolerance: 6 cursor: pointer highlight: trueEverything else is unchanged: the popup, highlight, cursor, camera moves and
the element’s events behave as they do for a click on the feature itself, and
the topmost feature of that layer within reach is the one picked, as on the feature itself. Leave
tolerance out (or write 0) to keep the on-the-feature behaviour. You no
longer need a second, transparent, wider copy of the layer as a hit area
(core 0.11 or later).
Move the camera to a feature
Section titled “Move the camera to a feature”Two click actions move the map to what the reader picked. They work with or without a popup; with one, the popup opens first and travels with the map.
interactive: click: flyTo: zoom: 8 # optional; center defaults to the clicked point duration: 600 # optional, millisecondsinteractive: click: zoomToFeature: padding: 40 # pixels around the feature (optional) maxZoom: 12 # don't zoom closer than this (optional) duration: 600 # optional, millisecondsflyTo centres on the clicked point (or a center: you give) at a zoom you
choose, which suits points. zoomToFeature fits the clicked feature’s own
outline in view, which suits areas and lines of different sizes.
Add map controls
Section titled “Add map controls”controls: adds MapLibre’s buttons around the map. true puts a control in
its usual corner; an object picks the corner:
type: mapid: with-controlsconfig: center: [-74.006, 40.7128] zoom: 11 mapStyle: "https://tiles.openfreemap.org/styles/liberty"controls: navigation: true # zoom in/out and compass, top-right scale: true # distance scale, bottom-left fullscreen: true # top-right geolocate: # "show my location", asks the reader's permission enabled: true position: top-left| Control | What it adds | Default corner |
|---|---|---|
navigation | Zoom buttons and a compass that resets the bearing | top-right |
scale | A distance scale | bottom-left |
fullscreen | Fullscreen toggle | top-right |
geolocate | Show the reader’s location (the browser asks first) | top-right |
globe | Globe / flat map toggle (maplibre-gl 5 or later) | top-right |
terrain | 3D terrain on/off, for a document with terrain: | top-right |
attribution | The data credits, with compact and customAttribution options | bottom-right |
The corners are top-left, top-right, bottom-left and bottom-right.
Without an attribution entry, MapLibre’s own attribution still shows in
the bottom-right corner: crediting the map data is a licensing obligation for
most map tiles.
Button labels (“Zoom in”, “Zoom out” …) come from MapLibre’s locale: keys,
so a config.locale table translates them along with everything else the
library draws (Show the map in another language).
To turn off ways of moving the map instead (scroll zoom, rotation, a fully static map), see Map documents › How readers can move the map.
Legends
Section titled “Legends”A legend explains colours; how a map gets one is in Thematic maps › The legend.
Troubleshooting
Section titled “Troubleshooting”- Clicking does nothing. Popups, hover and click actions need an
interactive:block on the layer itself. Check that it is indented under the layer, besidepaint:. - The popup shows a label but no value. The property name doesn’t match
the data: names are case-sensitive (
Nameis notname). Addelse: "…"to see when a value is missing. - Nothing highlights, and the console says “needs feature ids”. The
features have no ids: add
promoteIdorgenerateId: trueto the source (above). Core 0.9.1 and later add them for ahighlightlayer by themselves. - Highlight warns that the colour is already an expression. Choose the highlight colour yourself instead.
- A warning says
actionis deprecated.click.actionand themouseenter/mouseleaveactions were never carried out. Remove them, and listen for the element’s events instead (below).
More: Troubleshooting.
Reacting from your own JavaScript
Section titled “Reacting from your own JavaScript”Popups, highlights and camera moves need no code. To do something else when
a feature is clicked (update a chart, fill in a side panel), listen for the
element’s events. They fire for features of a layer with an interactive:
block:
const map = document.querySelector("ml-map");map.addEventListener("ml-map:layer-click", (event) => { const { layerId, feature, lngLat } = event.detail; console.log(layerId, feature.properties);});ml-map:layer-hover carries the same details for hover. Every event is in
<ml-map> events.
Host events: click.emit
Section titled “Host events: click.emit”click.emit sends a named event, with a payload built from the clicked
feature, to code on the page. It does nothing under <ml-map>, which accepts
no page handlers; there, use ml-map:layer-click above. It is for a map you
build yourself and connect with attachInteractions, under a policy that
trusts the document and with a handler registered for the event’s name
(Export to style.json).
interactive: click: emit: event: select payload: id: { property: code } label: { property: place, else: "Untitled" } kind: { str: earthquake }Each payload key is a str (fixed text) or a property of the feature,
with an optional else. A missing property gives its else, or null, so
every payload has the same keys. A payload holds data only: no formats, no
markup.
Look it up
Section titled “Look it up”- The layer reference: every
interactive:key. - The map reference: every control and its options.
- Map documents › Popups at fixed places: popups that open at fixed coordinates, with no feature under them.
- Examples: Basic map and Interactive points.
- Gallery: Display a popup on click, Show polygon information on click, Center the map on a clicked symbol, Display map navigation controls.