Skip to content

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.

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:

cities.yaml
type: map
id: cities
config:
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 a city

Edit this map in the playground

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.

- h3: [{ property: name }] becomes <h3>New York</h3>: the key is the element, and its list holds items whose output is joined inside it.

ItemShows
str: "Population: "The text as written
property: populationThe 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, hr and br. Any other key is skipped with a console warning. Elements don’t nest: a ul: can’t contain li: entries. Put links and images inside p: or div:.
  • Text is always text. Property values and str are shown as text, never as markup, so data can’t inject HTML into your page. A str tagged !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 !html produces, 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 as javascript:, is shown as plain text. A link opens in a new tab unless you write target: _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.

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:

OptionWhat it doesExample
localeThe language to format for. Default: the page’s language, then the browser’sde-DE
currencyFormat as moneyEUR, USD, JPY
unitAdd a unit (ignored with currency)kilometer, percent, celsius
min-fraction-digitsAt least this many decimals2
max-fraction-digitsAt 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 lang on <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 €”. Write locale: en-US in 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, .Nf keeps 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.

The hover: part of interactive: takes three things, alone or together:

  • cursor: the CSS cursor over a feature, usually pointer.
  • highlight: true: recolours the feature under the pointer.
  • popup: a tooltip, in the same format as click.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 opens click.popup.
states.yaml
type: map
id: states
config:
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: 40
Point at a state to highlight it and see its name; click to zoom to it

Edit this map in the playground

highlight 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.

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: pointer

Without 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: true

Everything 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).

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, milliseconds
interactive:
click:
zoomToFeature:
padding: 40 # pixels around the feature (optional)
maxZoom: 12 # don't zoom closer than this (optional)
duration: 600 # optional, milliseconds

flyTo 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.

controls: adds MapLibre’s buttons around the map. true puts a control in its usual corner; an object picks the corner:

type: map
id: with-controls
config:
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
ControlWhat it addsDefault corner
navigationZoom buttons and a compass that resets the bearingtop-right
scaleA distance scalebottom-left
fullscreenFullscreen toggletop-right
geolocateShow the reader’s location (the browser asks first)top-right
globeGlobe / flat map toggle (maplibre-gl 5 or later)top-right
terrain3D terrain on/off, for a document with terrain:top-right
attributionThe data credits, with compact and customAttribution optionsbottom-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.

A legend explains colours; how a map gets one is in Thematic maps › The legend.

  • Clicking does nothing. Popups, hover and click actions need an interactive: block on the layer itself. Check that it is indented under the layer, beside paint:.
  • The popup shows a label but no value. The property name doesn’t match the data: names are case-sensitive (Name is not name). Add else: "…" to see when a value is missing.
  • Nothing highlights, and the console says “needs feature ids”. The features have no ids: add promoteId or generateId: true to the source (above). Core 0.9.1 and later add them for a highlight layer by themselves.
  • Highlight warns that the colour is already an expression. Choose the highlight colour yourself instead.
  • A warning says action is deprecated. click.action and the mouseenter/mouseleave actions were never carried out. Remove them, and listen for the element’s events instead (below).

More: Troubleshooting.

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.

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.