Skip to content

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.

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:

src/stories/earthquakes.yaml
type: scrollytelling
id: earthquake-story
theme: dark # chapter cards: light (default) or dark
showMarkers: true # a dot per chapter at the right edge
markerColor: "#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:

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.

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: description is HTML, shown as written. Markdown is not converted, so write <strong>bold</strong> and <a href="…">a link</a>.
  • Pictures and video: image: or video: above the text, as a full https:// URL or the address of a file in public/ (/images/fault.jpg for public/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’s center isn’t hidden behind the card.
  • Layers: layers: { show: [...], hide: [...] } by layer id.
  • Actions in onChapterEnter and onChapterExit: setFilter, setPaintProperty, setLayoutProperty, flyTo, easeTo, fitBounds, and custom for 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.

To have the story fetched in the browser instead, save it in public/ and pass its address as src:

examples/astro/site/src/pages/story-runtime.astro
<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.

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:

src/content.config.ts
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 };
src/pages/stories/[id].astro
---
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).

The story element (<ml-scrollytelling>) dispatches bubbling events:

  • ml-scrollytelling:chapter on every chapter change, with detail: { chapterId, previousChapterId, index, callback, map } (callback is the chapter’s callback: name);
  • ml-scrollytelling:action for each custom action, with detail: { action, chapterId, phase, map };
  • ml-scrollytelling:error when the story can’t load or validate, with detail: { 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:

examples/astro/site/src/pages/story.astro
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.

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.

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

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.

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.

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.

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
}

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: sticky doesn’t work inside an element with overflow: hidden, auto or scroll. Look for one on a wrapper around the story.
  • **bold** shows literally in a chapter. description is 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/astro 0.5 or later, or use config.
  • A layer doesn’t appear or disappear. Check the chapter’s layers ids against the layers’ ids; the map’s notice names a missing one while you develop.
  • Without a build step: the same story file in a plain HTML page, with <ml-scrollytelling> from a CDN: Story maps in plain HTML.