Story maps in Astro
A story map is a map that moves as the reader scrolls: chapters of text and
images scroll over a sticky map, and each chapter flies the camera somewhere,
shows or hides layers, and can run actions (this is often called
scrollytelling). In an Astro site, the <Scrollytelling> component from
@maplibre-yaml/astro shows a story written as one YAML file.
This page assumes the package is installed (Astro: Install). How to write the story file is on Writing a story file.
Quick start
Section titled “Quick start”A story is one YAML file: the map (config: and layers:, as on any map)
and the chapters: the reader scrolls through. Save this one as
src/stories/earthquakes.yaml; its data is inline, so it works as it is:
type: scrollytellingid: earthquake-storytheme: dark # chapter cards: light (default) or darkshowMarkers: true # a dot per chapter at the right edgemarkerColor: "#3FB1CE"
# The map: its camera when the page opens, and its layers.config: mapStyle: "https://tiles.openfreemap.org/styles/liberty" center: [-118.2437, 34.0522] zoom: 9
layers: - id: earthquake-circles type: circle source: type: geojson data: type: FeatureCollection features: - type: Feature geometry: { type: Point, coordinates: [-118.537, 34.213] } properties: { name: "Northridge, 1994", magnitude: 6.7 } - type: Feature geometry: { type: Point, coordinates: [-118.40, 34.42] } properties: { name: "San Fernando, 1971", magnitude: 6.6 } - type: Feature geometry: { type: Point, coordinates: [-118.08, 34.06] } properties: { name: "Whittier Narrows, 1987", magnitude: 5.9 } - type: Feature geometry: { type: Point, coordinates: [-117.76, 33.95] } properties: { name: "Chino Hills, 2008", magnitude: 5.4 } paint: circle-radius: 8 circle-color: "#ff0000" circle-stroke-width: 2 circle-stroke-color: "#ffffff"
chapters: - id: intro title: "Los Angeles earthquakes" description: "<p>Exploring seismic activity in Southern California.</p>" center: [-118.2437, 34.0522] zoom: 9 alignment: center
- id: detail title: "Major fault lines" description: "<p>The San Andreas Fault runs through this region.</p>" center: [-118.0, 34.0] zoom: 11 pitch: 45 bearing: 30 alignment: left onChapterEnter: - action: setFilter layer: earthquake-circles filter: [">=", ["get", "magnitude"], 6] onChapterExit: - action: setFilter layer: earthquake-circles filter: null
- id: conclusion title: "Preparing for the future" description: "<p>Understanding earthquake patterns helps us prepare.</p>" center: [-118.2437, 34.0522] zoom: 9 alignment: full
footer: "<p>Data source: USGS</p>"Load it at build time and pass it to <Scrollytelling> as config, on a
page of its own such as src/pages/story.astro:
---import { Scrollytelling, loadScrollytellingConfig } from '@maplibre-yaml/astro';const story = await loadScrollytellingConfig('./src/stories/earthquakes.yaml');---<html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Los Angeles earthquakes</title> </head> <body style="margin: 0;"> <Scrollytelling config={story} /> </body></html>Run npm run dev and open /story. The map fills the window and the first
card sits over it; as you scroll, the map flies to each chapter, and the
second chapter shows only the two strongest earthquakes.
Loading at build time is the recommended way: the chapters’ text is in the
page’s HTML, and a mistake in the story fails astro build (and shows in
astro dev) with the file, line and a did-you-mean, instead of reaching your
site. The viewport <meta> makes phones lay the story out at their own
width, and margin: 0 lets the map reach the edges. The other snippets on
this page show only the component: put them on a page like this one, or in a
layout.
Common tasks
Section titled “Common tasks”Write the chapters
Section titled “Write the chapters”Each chapter needs an id, a title, and a camera: center and zoom, and
optionally pitch, bearing and animation (flyTo, the default,
easeTo or jumpTo). Then add what the chapter changes:
- Text:
descriptionis HTML, shown as written. Markdown is not converted, so write<strong>bold</strong>and<a href="…">a link</a>. - Pictures and video:
image:orvideo:above the text, as a fullhttps://URL or the address of a file inpublic/(/images/fault.jpgforpublic/images/fault.jpg). Images load only as the reader reaches them; keep them small. - Card position:
alignment: left | right | center | full. The camera is offset so the chapter’scenterisn’t hidden behind the card. - Layers:
layers: { show: [...], hide: [...] }by layer id. - Actions in
onChapterEnterandonChapterExit:setFilter,setPaintProperty,setLayoutProperty,flyTo,easeTo,fitBounds, andcustomfor your own script (below). Enter actions run after the chapter’s camera move and layer changes; an action naming a layer the map doesn’t have is skipped with a warning.
Writing a story file explains each key and action, with a complete example.
Load the story in the browser (src)
Section titled “Load the story in the browser (src)”To have the story fetched in the browser instead, save it in public/ and
pass its address as src:
<Scrollytelling src="/stories/cascadia.yaml" />The element fetches, validates and renders the story there; if it doesn’t
validate, the errors show in place of the chapters. Use this when the story
must change without a rebuild. src needs @maplibre-yaml/astro 0.5 or
later.
Stories as a content collection
Section titled “Stories as a content collection”Keep several stories in a content collection and build one page per story.
getScrollytellingSchema() validates each file with the same rules as
loadScrollytellingConfig, at build time and in astro dev:
import { defineCollection } from 'astro:content';import { glob } from 'astro/loaders';import { getScrollytellingSchema } from '@maplibre-yaml/astro/utils';
const stories = defineCollection({ loader: glob({ pattern: '*.yaml', base: './src/stories' }), schema: getScrollytellingSchema(),});
export const collections = { stories };---import { getCollection } from 'astro:content';import { Scrollytelling } from '@maplibre-yaml/astro';
export async function getStaticPaths() { const stories = await getCollection('stories'); return stories.map((story) => ({ params: { id: story.id }, props: { story } }));}
const { story } = Astro.props;---<Scrollytelling config={story.data} />Each file in src/stories/ is one whole story (type: scrollytelling at the
top, as in the quick start). Import the schema from
/utils in the content config (why).
React to the story from a script
Section titled “React to the story from a script”The story element (<ml-scrollytelling>) dispatches bubbling events:
ml-scrollytelling:chapteron every chapter change, withdetail: { chapterId, previousChapterId, index, callback, map }(callbackis the chapter’scallback:name);ml-scrollytelling:actionfor eachcustomaction, withdetail: { action, chapterId, phase, map };ml-scrollytelling:errorwhen the story can’t load or validate, withdetail: { errors, warnings, source }.
It also carries the active chapter’s id in data-active-chapter. The example
site uses a custom action to show a banner:
const banner = document.getElementById("story-banner")!;document.addEventListener("ml-scrollytelling:action", (event) => { const { action } = (event as CustomEvent).detail; banner.textContent = String(action.options?.banner ?? "");Methods such as mapReady() and goToChapter(id) are in the
<ml-scrollytelling> reference.
Style the story
Section titled “Style the story”class adds a class to the story’s container. The styles shipped with the
element come first, so your own CSS overrides them:
<Scrollytelling config={story} class="my-story" />
<style is:global> .my-story .chapter-content { max-width: 520px; font-family: Georgia, serif; }</style>The class names and custom properties (such as --ml-scrolly-map-top for a
page with a fixed header) are listed under
Styling. Corner slots work as on
a map (Corner slots); a slot="legend"
child fills the story’s pinned legend.
Check chapter positions with debug
Section titled “Check chapter positions with debug”<Scrollytelling config={story} debug />debug outlines each chapter (green while active, red dashed otherwise) so
you can see where it starts and ends. It also shows the story’s author hints,
such as a chapter naming a layer that doesn’t exist, on a deployed site;
without it they show only in astro dev and on localhost. quiet hides
the on-map notices on a finished site.
Lay out chapters yourself
Section titled “Lay out chapters yourself”Chapter renders one chapter card, styled exactly like a story’s own (it uses
the same renderer and stylesheet). Use it on pages that place chapters by
hand. Import it from the /components subpath; the package root doesn’t
export it:
---import { Chapter } from '@maplibre-yaml/astro/components';---<Chapter id="intro" title="Introduction" description="<p>Welcome to our story about climate data.</p>" image="/images/hero-landscape.jpg" alignment="left" theme="dark"/>A standalone Chapter is shown as active (full opacity), since nothing on
the page would activate it; pass isActive={false} for the dimmed look.
hidden renders a map-only chapter with no card. Images and videos take the
same paths as in a story file.
How a story behaves
Section titled “How a story behaves”Which chapter is active, what runs on load, the chapter rail, reduced motion and the pinned legend are the same on every page that shows a story: see How a story behaves.
Reference
Section titled “Reference”Scrollytelling component API
Section titled “Scrollytelling component API”interface ScrollytellingProps { src?: string; // The story file's address on the site (a file in public/) config?: ScrollytellingBlock; // Or a story loaded at build time. One of the two is required class?: string; // A class on the story's container debug?: boolean; // Chapter outlines, and author hints wherever the page is served base?: string | false; // Where the story's paths start. Default: your site's base // (import.meta.env.BASE_URL); false = relative to the page quiet?: boolean; // Hide the on-map notices (default false)}<Scrollytelling> renders core’s <ml-scrollytelling>
element, the same one a plain HTML page uses, so a story file behaves the
same in both. With config, the chapters and the map’s document are rendered
into the page at build time and the element takes them over in the browser.
@maplibre-yaml/astro 0.7 needs @maplibre-yaml/core 0.9 or later.
Chapter component API
Section titled “Chapter component API”interface ChapterProps { id: string; // Required title: string; // Required description?: string; // HTML image?: string; // A full URL or the address of a file in public/ video?: string; // The same alignment?: 'left' | 'right' | 'center' | 'full'; // Default 'center' hidden?: boolean; // A map-only chapter, with no card isActive?: boolean; // Default true (full opacity) theme?: 'light' | 'dark'; // Default 'light' base?: string | false; // Where image and video paths start}The story file
Section titled “The story file”- Writing a story file: every part of a story, explained, with chapter actions, the rail and the pinned legend.
- YAML reference for stories: every key with its type and default.
Troubleshooting
Section titled “Troubleshooting”Problems every map shares are in Troubleshooting.
- The map and text come out tiny on a phone. The page is missing
<meta name="viewport" content="width=device-width, initial-scale=1">. - The map scrolls away with the chapters.
position: stickydoesn’t work inside an element withoverflow: hidden,autoorscroll. Look for one on a wrapper around the story. **bold**shows literally in a chapter.descriptionis HTML; write<strong>bold</strong>.- With
src, the chapters stay on Loading story… and the console says Failed to resolve module specifier ‘@maplibre-yaml/core’: upgrade to@maplibre-yaml/astro0.5 or later, or useconfig. - A layer doesn’t appear or disappear. Check the chapter’s
layersids against the layers’ids; the map’s notice names a missing one while you develop.
Alternatives
Section titled “Alternatives”- Without a build step: the same story file in a plain HTML page, with
<ml-scrollytelling>from a CDN: Story maps in plain HTML.