<ml-map> & <ml-scrollytelling> reference
The two elements @maplibre-yaml/core/register defines (the CDN’s
register.js is the same module). For how to use them, see
HTML pages: the <ml-map> element and
Story maps in plain HTML. The generated
MLMap API reference
has the same members with their TypeScript types.
Registering the elements
Section titled “Registering the elements”Importing @maplibre-yaml/core/register (or loading the CDN’s register.js)
defines both elements at once. To decide when that happens, import from
@maplibre-yaml/core/components instead and call registerMLMap() (defines
<ml-map>) or registerMLScrollytelling() (defines <ml-scrollytelling> and
the <ml-map> it renders). In TypeScript, MLMap, MLMapEventMap,
MLScrollytelling and MLScrollytellingEventMap come from
@maplibre-yaml/core/register, and document.querySelector("ml-map") is
typed as MLMap without a cast.
<ml-map>
Section titled “<ml-map>”Draws one type: map document.
<ml-map> attributes
Section titled “<ml-map> attributes”| Attribute | Value | What it does |
|---|---|---|
src | URL | The document to load: YAML, JSON or TOML, by its extension or content type (JSON & TOML). Changing it fetches the new document and applies it to the running map in place (Updating the document) |
config | string | The document as text: YAML, JSON or TOML. Changing it applies the new document to the running map in place |
base | path | Where the paths in src and in the document start, for a site under a subpath ("/my-repo/"). Full https:// URLs are never changed (Sites under a subpath) |
camera | document (default) or host | Who owns the camera when the document changes. document: a changed center, zoom, bearing or pitch eases the map there, and an added or re-targeted fitTo frames its source once. host: an update never moves the camera, and a basemap rebuild keeps the current view, for a page whose viewport is per-user |
debug | boolean | Show author hints on the page wherever it is served, not only while developing (Reading what the map tells you) |
quiet | boolean | Keep the notice (“Part of this map couldn’t load”, author hints) and the zero-height hint off the page. The error card and “Loading map…” still show; the console keeps every message |
The element reads its document from the first of these it finds: the
config property, an inline <script type="text/yaml"> child, the src
attribute, the config attribute. Each takes a format v1 or
format v2 document. An inline document goes in a
<script type="text/yaml"> child, starting on the line after the opening tag:
<ml-map style="display: block; height: 400px;"> <script type="text/yaml">type: mapid: inline-mapconfig: mapStyle: "https://tiles.openfreemap.org/styles/liberty" center: [-74.006, 40.7128] zoom: 12 </script></ml-map>type="application/yaml" works too, type="application/json" takes a
document written as JSON, and type="application/toml" one written as
TOML. Errors carry line numbers in every format:
<ml-map style="display: block; height: 400px;"> <script type="application/json"> { "type": "map", "id": "inline-json", "config": { "mapStyle": "https://tiles.openfreemap.org/styles/liberty", "center": [-74.006, 40.7128], "zoom": 12 } } </script></ml-map>An element with no document yet waits until the end of the next frame before
it reports “No configuration provided.”, so a config assigned just after the
element upgrades (from a script or a framework effect) renders without a
passing error, and a mapReady() called in between waits for it.
The element has no lang attribute of its own: it uses the nearest lang
(on the element, an ancestor, or <html>) to format numbers in popups and the
legend, and for the label language of a Protomaps basemap
(Accessibility & languages).
<ml-map> properties
Section titled “<ml-map> properties”| Property | Type | What it does |
|---|---|---|
config | object or document text (set); object or null (get) | The document, as an object or as YAML, JSON or TOML text. Set before or after the element connects, it is validated like every other path; a value JSON-equal to the one shown is a no-op, and a different one is applied to the running map in place (Updating the document). Get returns the document being shown |
base | string or null | Reflects the base attribute; setting it re-resolves the document’s paths and applies the result as an update |
camera | "document" or "host" | Reflects the camera attribute. Can be changed on a running map; it governs the next update onward, not one already in flight |
debug | boolean | Reflects the debug attribute |
quiet | boolean | Reflects the quiet attribute |
MLMap.fitToWaitMs (static) | number | How long a map with fitTo on a url: source waits for the data before its first paint, so it opens already framed. Default 1500 (ms); 0 turns the wait off. Set it before maps render |
<ml-map> methods
Section titled “<ml-map> methods”| Method | Returns | What it does |
|---|---|---|
mapReady() | Promise<maplibregl.Map> | Resolves with the MapLibre map once the document has loaded (at once if it already has); rejects when the document fails (ml-map:error). During a basemap rebuild it waits for the new map |
getMap() | maplibregl.Map | null | The MapLibre map, or null before a render. The same object through every update; only a basemap change (mapStyle) builds a new one, and ml-map:load fires again when it does, so a page that keeps a reference refreshes it on that event |
getRenderer() | MapRenderer | null | The renderer behind the element (JavaScript API) |
isLoaded() | boolean | Whether the map has finished loading |
update(doc) | Promise<UpdateResult> | Apply a document (an object, or its text in YAML, JSON or TOML) to the running map in place: what setting config does, with the outcome. Resolves { outcome: "applied", summary } once the map shows the document, "rejected" with errors (the map untouched), "fallback" when a basemap change rebuilt the map (or there was no map yet), or "superseded" when a later document replaced this one before it applied. The trust the map was built with (the renderer’s capabilities) applies to the updated document: an update neither raises nor lowers it (Updating the document) |
getDocument() | MapBlock | null | The document the running map shows: the same shape config returns and update() takes. (getRenderer().getDocument() returns the renderer’s normalized model instead, which is not the element’s update() input) |
setState(values) | void | Write state: values. Each goes through the map’s global state, the params panel follows, and one ml-map:state-change fires. Declared keys only: a key the document’s state: block does not declare throws, and nothing is written. Needs maplibre-gl 5.6 or later (State and parameters) |
getState() | Record<string, unknown> | The current state: values: the document’s defaults, the panel’s seeds and every write since. {} with no running map |
reload() | Promise<void> | Fetch src (or re-read the inline YAML) again and apply what changed to the running map as an update. Sources whose last load failed are retried even when the document is unchanged, and ml-map:update-applied answers either way (with an empty summary when nothing changed). From an error card, a valid document renders fresh. Slot children are kept |
addLayer(layer) | Promise<void> | Add a layer of your own (the same shape as a document layer). An update never moves or removes it |
removeLayer(layerId) | void | Remove a layer |
setLayerVisibility(layerId, visible) | void | Show or hide a layer |
updateLayerData(layerId, geojson) | void | Replace a GeoJSON layer’s data |
getGrid(sourceId) | GridField | undefined | The decoded field of a type: grid source, or undefined before it is decoded (and for any other source). field.sample(lngLat) returns a number, { u, v, speed } or null (Grid sources) |
gridReady(sourceId) | Promise<GridField> | Resolves with the field once the grid is decoded (at once if it already is). Rejects when the document has no grid source of that id, when its file fails to load (a later reload() retries), or when the source is removed |
hasGrid(sourceId) | boolean | Whether the applied document declares a grid source of this id, regardless of decode state — true even before gridReady() resolves, false for no such source. A synchronous way to tell “not yet decoded” from “no such source” (Grid sources) |
uiString(key, params?) | string | The document’s text for a UI string key (maplibre-yaml.Legend.Title, NavigationControl.ZoomIn, …): its locale: translation, or English. For controls you draw next to the map (Accessibility & languages) |
Live data refreshes on the document’s own refreshInterval; there is no
method to pause it or force a refresh. Each refresh fires
ml-map:layer-data-loading and ml-map:layer-data-loaded
(Live data).
Updating the document
Section titled “Updating the document”A different document, whether assigned to config, loaded by a changed
src or base, fetched by reload() or passed to update(), is applied to
the running map in place. The element validates the whole document first,
then changes only what differs from the document the map shows: layers and
sources are matched by id, a changed paint, layout, filter or zoom value goes
through MapLibre’s setter, a change MapLibre cannot apply live replaces the
layer in document position (its type, source or source-layer) or the
source and its layers (a source’s kind, or an option fixed at construction
such as cluster or promoteId), inline data is swapped with setData, and
the legend, params panel, markers, popups and controls follow. The map, its camera and your listeners stay as
they were. ml-map:update-applied reports what changed, by id.
- The document owns only the keys it declares. Layers you added through
addLayer()orgetMap().addLayer()are never moved or removed, and keep their absolute position in the stack; a document layer that moves does not carry them with it. A paint or layout value you set on a document layer throughgetMap()is overwritten only when the document’s own value for that property changed. A document source still used by one of your layers is left in place when the document drops it, and listed insummary.failures; so is a document layer whose id collides with one of yours. To re-apply changes of your own after an update, do it onml-map:update-applied;summary.propertiesSetnames the affected layers. - The camera follows the document only when the document’s camera
changed: a changed
center,zoom,bearingorpitcheases the map there, and an added or re-targetedfitToframes its source once (the samefitToover new data does not re-fit). A camera key the new document drops causes no move.camera="host"turns this off for a page whose viewport is per-user. - An invalid document keeps the map. A document that fails validation,
a
srcthat doesn’t load, or an update engine file that doesn’t download firesml-map:update-rejectedwith the diagnostics and leaves the map, its camera and the document it shows untouched; while you develop, the notice shows a hint. The error card replaces the map only when there is no running map to keep. - A basemap change rebuilds. A changed
mapStyle(or a map option MapLibre cannot change after construction, such as an interaction handler) builds a new map at the current camera, carries the currentstate:values and the panel’s layer toggles across, and swaps it in once it has loaded: the page never shows a blank map.ml-map:loadfires again for the new map,getMap()returns the new object from then on, and the update’ssummary.rebuildistrue. If the new basemap never loads, the old map stays and the update is rejected withkind: "style". - Last document wins. One update applies at a time. A document that
arrives while one is being applied waits, and only the newest waiting one
runs; the ones it replaced resolve
supersededand fire no event. - Trust is fixed per map. The element always renders a document
untrusted (it never passes
capabilitiesto the renderer), and an update neither raises nor lowers that. A host that renders documents from more than one author throughMapRendererconstructs it untrusted too. getMap().setStyle()is MapLibre’s. Calling it withouttransformStyleremoves the document’s layers and sources, as it does any runtime-added layer; that is MapLibre’s behaviour and the element does not put them back. The library reapplies only the document’sstate:values after any style load, soglobal-stateexpressions in the new style keep working. To change the basemap, changemapStylein the document instead.
The update engine is a separate file next to the script (chunks/update.js),
downloaded on the first update (Hosting the files yourself).
<ml-map> events
Section titled “<ml-map> events”Every event bubbles, and carries its payload on event.detail. In
TypeScript, MLMapEventMap (from @maplibre-yaml/core/register) types each
listener.
| Event | Detail | Fires when |
|---|---|---|
ml-map:loading | { url } | A src document fetch begins |
ml-map:load | {} | The document is fully loaded — basemap and every layer |
ml-map:error | { error, fatal } or { errors } | Initialization or runtime failure. fatal: true aborts the document (a basemap style that won’t load, a worker that can’t start); fatal: false is a problem the map survives (a rejected layer, a tile that 404’d). A document that can’t be fetched, parsed or validated carries errors — the structured { path, message }[] the error card shows — instead |
ml-map:layer-added | { layerId } | A layer was added to the map |
ml-map:layer-removed | { layerId } | A layer was removed |
ml-map:layer-data-loading | { layerId } | A layer’s data fetch begins |
ml-map:layer-data-loaded | { layerId, featureCount } | A layer’s data landed |
ml-map:layer-data-error | { layerId, error } | A layer’s data fetch failed |
ml-map:source-loaded | { sourceId } | A type: grid source was decoded and getGrid(sourceId) answers: on the first load and again after every swap (a changed url, range or geometry) (Grid sources) |
ml-map:layer-click | { layerId, feature, lngLat } | A feature on an interactive layer was clicked |
ml-map:layer-hover | { layerId, feature, lngLat } | A feature on an interactive layer was hovered |
ml-map:markers-added | { count } | The markers: pins were added |
ml-map:marker-click | { index, at } | A standalone marker was clicked |
ml-map:marker-icon-error | { index, icon } | A marker icon: failed (unsafe scheme or load error); the default pin was substituted |
ml-map:image-error | { name, url } | An images: entry failed (unsafe scheme, CORS, load or register error) |
ml-map:parameter-change | { key, value } | A params-panel control wrote a state key |
ml-map:layer-visibility | { layerId, visible } | A params-panel checkbox toggled a layer |
ml-map:camera-fit | { source, bounds } | The fitTo camera framed its source’s data (once, on the first data load for fetched sources) |
ml-map:layer-classified | { layerId, classify } | A classify: layer’s classes as drawn — once when the layers settle, and again when fetched data computes or refreshes them. classify is the record the emitted style carries in metadata["maplibre-yaml:classify"] (breaks, colours, stats) |
ml-map:update-applied | { summary } | A changed document was applied to the running map (Updating the document). summary names what changed by id: added, removed, moved and replaced layers, propertiesSet as { layerId, property } records, sourcesReplaced, cameraMoved, and failures as { op, message } records; rebuild is true when a basemap change rebuilt the map (then added names every document layer, and ml-map:load has fired for the new map) |
ml-map:update-rejected | { kind, errors } | A changed document was refused and the running map kept as it was. kind is validation, fetch (a src that didn’t load), chunk (the update engine’s file didn’t download) or style (a new basemap that never loaded); errors carries the diagnostics, each { kind, message, path? } |
ml-map:state-change | { values, changed } | The document’s state: values changed: setState(), a params-panel write, or an update whose document declares new defaults. values is every current value by key; changed lists the keys that changed (a key the new document no longer declares is in changed and gone from values). Fires once per write, alongside one ml-map:parameter-change per key for a panel write |
<ml-map> slots
Section titled “<ml-map> slots”Children with one of these slot values are placed over the map
(Slots: your own chrome over the map):
| Slot | Placement |
|---|---|
top-left, top-right, bottom-left, bottom-right | In that corner, stacked after the built-in legend and the params panel |
legend | Replaces the built-in legend, at the document’s legend.position (default top-left) |
- Stacking. Pieces sharing a corner stack in this order: the built-in legend, the params panel, then slot children in document order. Bottom corners grow upward, and a corner steps past a MapLibre control already in it.
- Mounted on load. Slot children are hidden until the map loads, then moved into their corner. They stay the same elements, so listeners you attached keep working.
- They survive a new document and a new map. An in-place update leaves slot children where they are. A basemap rebuild and an error card park them back on the element, and the next render mounts them again. Slot children added after load are picked up on the next render.
- Pointer events. Empty corner areas pass gestures through to the map; your slot content receives clicks.
- An unknown slot name logs a warning, and the child is left where it is.
Slots are page markup, not part of the map document: an
export to style.json doesn’t include them. An inline
<script type="text/yaml"> child holds the document instead (above). Any
other child is left where it is.
<ml-map> styling hooks
Section titled “<ml-map> styling hooks”| Hook | What it is |
|---|---|
aria-busy="true" | Set on the element while the map starts; removed when the basemap draws or an error card replaces the map |
.ml-map-loading | The “Loading map…” text, shown in the middle of the map if it hasn’t drawn after about a second (role="status"). Hide it with ml-map .ml-map-loading { display: none; } when the page has its own loading indicator |
.ml-map-error | The error card that replaces a map that can’t be drawn |
.ml-map-notice | The notice over the map (“Part of this map couldn’t load”, author hints) |
--ml-map-notice-top, --ml-map-notice-left, --ml-map-notice-transform, --ml-map-notice-max-width | Custom properties that place the notice (defaults 10px, 50%, translateX(-50%), min(480px, calc(100% - 20px))). A story sets them to keep the notice clear of its pinned legend |
.ml-map-height-hint | The note shown while the element has no height |
The element has no size of its own: give it display: block and a height.
Before maplibre-yaml’s script has loaded, the browser doesn’t know
<ml-map> yet and shows nothing inside it. A rule for the not-yet-defined
element covers that moment too:
ml-map:not(:defined) { display: grid; place-items: center; background: #f3f4f6; color: #4b5563; font: 13px system-ui, sans-serif;}ml-map:not(:defined)::before { content: "Loading map…"; }Host events (click.emit) under <ml-map>
Section titled “Host events (click.emit) under <ml-map>”A layer’s click.emit interaction does nothing under <ml-map>: the element
registers no host handlers, so every emit is dropped. The other
interactions (popup, flyTo, zoomToFeature, hover.highlight) work as
usual. To act on a click in page code, listen for ml-map:layer-click.
emit is for maps you build yourself with attachInteractions
(Export to style.json).
Trust and attribution
Section titled “Trust and attribution”Attribution text (a source’s attribution:, customAttribution on the
attribution control, and the attribution a basemap style or TileJSON brings)
is shown by MapLibre’s attribution control as HTML. maplibre-yaml sanitizes
it first, under every trust policy, because MapLibre’s own sanitizer can be
bypassed before maplibre-gl 6.4.1
(GHSA-jrc7-96c5-q579).
What survives is plain text, character references such as ©, and
<a> links whose href is http:, https: or mailto: (keeping only
href, target and rel). Other markup, such as <b>, is shown as literal
text rather than dropped, so the credit stays readable. An
export applies the same rule to the style.json it writes,
with a contract warning naming each source whose attribution changed.
<ml-scrollytelling>
Section titled “<ml-scrollytelling>”Shows one type: scrollytelling document as a story: a sticky map, drawn by
an <ml-map> inside it, with the chapters scrolling past.
<ml-scrollytelling> attributes
Section titled “<ml-scrollytelling> attributes”| Attribute | Value | What it does |
|---|---|---|
src | URL | The story file: YAML, JSON or TOML |
config | string | The story as text: YAML, JSON or TOML |
base | path | Where the story’s own paths start, for a site under a subpath: src, every path in its map document and chapter image / video paths (Sites under a subpath). Passed on to the story’s map |
debug | boolean | Outline each chapter (green when active), and show the story’s author hints (a chapter naming a layer that doesn’t exist, a key under config: that belongs at the top level) wherever the page is served. Without it, hints show only while you develop (localhost, a file:// page, a bundler dev build); failures always show. Passed on to the story’s map |
quiet | boolean | Hide the on-page notes for a finished site: the map’s “Part of this map couldn’t load” notice and a chapter’s “image didn’t load” note. Every message stays in the browser console; a story that can’t load at all still shows its error card, and a slow map still shows “Loading map…”. Passed on to the story’s map |
The element reads its story from the first of these it finds: the story (or
config) property, an inline <script type="text/yaml"> child, the src
attribute, the config attribute. As on <ml-map>, the inline script may
also be type="application/yaml", type="application/json" or
type="application/toml".
<ml-scrollytelling> properties and methods
Section titled “<ml-scrollytelling> properties and methods”| Property / method | What it does |
|---|---|
story (alias config) | Set: the story as an object or as YAML, JSON or TOML text, validated like every other path (a value JSON-equal to the one shown is a no-op). Get: the validated story, or null |
base | Reflects the base attribute |
activeChapterId | The active chapter’s id, or null |
mapElement | The story’s own <ml-map>, once the story has rendered: its methods and events work as on any <ml-map> |
mapReady() | A promise of the MapLibre map once it has loaded; rejects if the story or its map fails |
getMap() | The MapLibre map, or null before it loads |
getGrid(sourceId) | The story map’s decoded type: grid field, or undefined before it is decoded (forwards to the story’s <ml-map>) |
gridReady(sourceId) | A promise of that field once decoded; rejects as <ml-map>’s gridReady() does |
hasGrid(sourceId) | Whether the story’s map declares a grid source of this id, regardless of decode state (forwards to the story’s <ml-map>) |
goToChapter(id) | Scroll to a chapter and make it active (what the chapter rail does) |
<ml-scrollytelling> events
Section titled “<ml-scrollytelling> events”All bubble. In TypeScript, MLScrollytellingEventMap (from
@maplibre-yaml/core/register) types each listener.
| Event | Detail | Fires when |
|---|---|---|
ml-scrollytelling:chapter | { chapterId, previousChapterId, index, callback, map } | A chapter became active. callback is the chapter’s callback: value |
ml-scrollytelling:action | { action, chapterId, phase, map } | A custom chapter action ran (phase is "enter" or "exit") |
ml-scrollytelling:error | { errors, warnings, source } | The story could not be loaded or validated. source is the src URL, when there is one |
map is the story’s MapLibre map (null when it failed to load). The events
of the story’s map itself (ml-map:load, ml-map:layer-click, …) bubble up
through the story too.
<ml-scrollytelling> slots
Section titled “<ml-scrollytelling> slots”| Slot | Placement |
|---|---|
top-left, top-right, bottom-left, bottom-right | Handed to the story’s map, in that corner, under the chapter cards |
legend | Fills the story’s pinned legend panel instead of the built-in entries |
<ml-scrollytelling> styling hooks
Section titled “<ml-scrollytelling> styling hooks”| Hook | What it is |
|---|---|
data-active-chapter | Attribute on the element: the active chapter’s id |
--ml-scrolly-map-height | Custom property: the sticky map’s height (default 100vh) |
--ml-scrolly-map-top | Custom property: where the map sticks, below a fixed header (default 0px) |
.scrolly-map-container | The sticky map |
.scrolly-chapters | The chapter column |
.scrolly-chapter | One chapter; .is-active while active, and .align-left, .align-right, .align-center or .align-full |
.chapter-content | A chapter’s card |
.chapter-markers, .chapter-marker | The chapter rail and its dots (showMarkers: true) |
.scrolly-footer | The story’s footer |
Examples of overriding them are in Styling.