Ambient Pack
Lets a site look slightly like today. The pack resolves a season, a time of day and calendar occasions for the author's place, writes them as attributes on <html>, and maps them to decorative tokens. It never asks for or guesses the visitor's location.
| Type | Functional |
| Version | 0.2.0 |
| CSS | packs/ambient.full.css (about 2.8 KB gzipped, icons included) |
| JS | packs/ambient.core.js (about 3 KB gzipped) |
| Inline head script | packs/ambient.head.js (under 1.25 KB gzipped) |
| Resolvers for builds | packs/ambient.resolve.js |
Season, time of day, weather from a feed you serve, occasions with built-in calendars, tint tokens, motif icons, the visitor opt-out and the preview are in. Animated atmosphere effects are not built yet.
How it works
- Configure. The page lists the signals it wants in
data-ambient and, optionally, gives a time zone and a calendar in a JSON block.
- Resolve. Pure functions turn the clock and the config into a state.
- Reflect. The state is written to attributes on
<html>: by the inline head script before first paint, then kept current by the module.
- Respond. The pack's CSS maps the attributes to tokens. Your CSS and your theme can select on the same attributes.
Ambient adds tokens. It does not change --color-primary, text colours, borders or focus styles, so contrast for text and controls stays the theme's own. Without data-ambient on <html> the pack does nothing.
Loading
<html lang="en" data-ambient="season daypart occasion"><head> <script type="application/json" id="ambient-config"> { "tz": "America/Los_Angeles", "hemisphere": "north" } </script> <script></script> <link rel="stylesheet" href="/cdn/vanilla-breeze.css"> <link rel="stylesheet" href="/cdn/packs/ambient.full.css"> <script type="module" src="/cdn/vanilla-breeze.js"></script> <script type="module" src="/cdn/packs/ambient.core.js"></script></head>
The page is correct at each stage of loading:
| Stage | Sets | Without it |
| Attributes written by your build | data-season, data-occasion and data-occasion-icon as of build time | No state; the plain theme |
| Inline head script | Season, time of day and occasions from the clock, and the weather the module cached earlier in the session, before first paint. Removes all state if the visitor opted out. | Built values stand until the module runs |
| Module | Fetches the weather feed once, at idle. Keeps the state current: it wakes at the next boundary and when the tab becomes visible again. Wires the opt-out control. | No weather; state is right at load and does not update |
The head script must come after #ambient-config. It does not throw, wait on the network or touch layout. With JavaScript off, the attributes your build wrote are used as they are.
Attributes on <html>
| Attribute | Values | Written by | Notes |
data-ambient | Word list of signals: season daypart weather occasion | Author | The opt-in switch. Absent, or off, means the pack does nothing. |
data-season | spring, summer, autumn, winter, or a custom label | Pack | Meteorological seasons by month. |
data-daypart | dawn, day, dusk, night | Pack | Dawn and dusk are the 45 minutes either side of sunrise and sunset. |
data-weather | clear, cloudy, fog, rain, snow, storm, wind | Pack | From the weather feed, a pin or a preview. Absent when there is no fresh feed. |
data-weather-intensity | light, heavy | Pack | Absent means moderate. |
data-occasion | Word list of occasion ids, highest priority first, at most two | Pack | Match with ~=, like data-theme. |
data-occasion-day | Boolean | Pack | Present on the day itself, not during the lead-up. |
data-occasion-icon | An icon name | Pack | The icon the active occasion's calendar entry names. |
data-ambient-level | tint, motif (default) | Author | tint turns motif icons off and leaves the colour tokens. |
data-ambient-debug | Boolean | Author | Allows the ?ambient= preview outside localhost. |
The pack writes a state attribute only for a signal you enabled and a value it knows. State attributes are a public contract: select on them freely.
Two attributes go on other elements. data-ambient-surface gives an element, usually <body>, the washed background. data-ambient="off" on any element resets the tokens for that subtree.
Configuration
All keys are optional. Without tz the visitor's own clock is used.
| Key | Meaning |
tz | IANA time zone of the author's place, for example America/Los_Angeles. |
hemisphere | north (default) or south, which shifts the season by two. |
seasons | A month (1–12) to label map for places without four seasons. Custom labels get no built-in tint; style them yourself. |
occasions | Dated windows written by a build with expandCalendar(). The head script reads these. |
calendars | Names of built-in sets to use: markers, geek, civic-us. Default ["markers"]. |
calendar | Your own entries, added to the built-in sets. An entry with the id of a built-in one replaces it. |
feed | URL of the weather feed, for example /ambient.json. Without it the pack makes no request. |
pin | Fixes signals for a page: { "season": "winter", "weather": "snow" }. |
Calendar rules
{ "id": "halloween", "rule": "10-31", "lead": 7, "trail": 0, "priority": 50 }
| Rule form | Example | Meaning |
| Fixed date | "10-31" | Month and day, every year |
| Range | "12-24..01-01" | Inclusive; may wrap the year |
| Nth weekday | "11:4:thu", "5:-1:mon" | Fourth Thursday of November; last Monday of May |
| Day of year | "doy:256" | The 256th day |
| Explicit dates | { "dates": ["2026-02-17"] } | Movable observances, written by your build |
An occasion is active from lead days before its date to trail days after. Active occasions are sorted by priority, then by how near their own day is, and the first two are kept. An entry may name an icon from the icon list.
Built-in calendars
| Set | Occasion ids | Default |
markers | new-year, march-equinox, june-solstice, september-equinox, december-solstice, leap-day | On |
geek | web-birthday, pi-day, css-naked-day, may-the-4th, towel-day, programmers-day, ada-lovelace-day | Off |
civic-us | US federal holidays by their statutory names (mlk-day, presidents-day, memorial-day, juneteenth, independence-day, labor-day, columbus-day, veterans-day, thanksgiving, christmas-day, new-year), plus valentines-day and halloween | Off |
Solstice and equinox dates are the usual ones, not astronomical calculations. Religious and cultural observances are not built in: add them as your own entries, with explicit dates for the movable ones.
The head script reads only dated windows (occasions), which a build writes. On a page that gives no occasions, the module fetches packs/ambient.resolve.js once and applies occasions after load.
import { calendarFor, expandCalendar, resolveSeason } from 'vanilla-breeze/ambient-resolve' const year = new Date().getFullYear()const site = { calendars: ['markers', 'geek'], calendar: [{ id: 'blog-birthday', rule: '03-01', icon: 'cake' }] }const config = { tz: 'America/Los_Angeles', occasions: expandCalendar(calendarFor(site), [year - 1, year, year + 1]),}const season = resolveSeason(new Date(), config)
Weather feed
Weather comes from one URL that you serve, named by feed in the config. The pack knows nothing about weather providers and the visitor's browser contacts only your site. Add weather to data-ambient to use it.
{ "v": 1, "asOf": "2026-11-15T20:00:00-08:00", "ttl": 1800, "weather": "rain", "intensity": "light", "tempC": 17, "sunrise": "06:18", "sunset": "16:47", "attribution": "Weather data by Open-Meteo.com"}
| Field | Meaning |
v | Schema version, 1. Anything else is ignored. |
asOf | ISO time of the observation. Required. |
ttl | Seconds the observation is good for. Default 1800. |
weather, intensity | Values of data-weather and data-weather-intensity. An unknown value is dropped. |
tempC | Carried in Ambient.state.tempC for your own use, such as a status line. It has no attribute and no styling. |
sunrise, sunset | HH:MM in the place. They replace the fixed sun times for dawn and dusk. |
attribution | For you to show; the pack does not read it. |
- Stale weather is never shown. Past
asOf plus twice the ttl, the pack ignores the feed and removes data-weather, in an open tab too.
- Failure is quiet. A missing, malformed or slow feed (over 3 seconds) leaves
data-weather absent. There is no retry within the page view.
- One request. The feed is fetched at idle with low priority and kept in
sessionStorage for its ttl, so the next page makes no request and the head script applies the weather before paint. An open tab asks again when the ttl ends.
- No request when the visitor opted out, when neither
weather nor daypart is enabled, with Save-Data, or with prefers-reduced-data: reduce.
Producing the feed
Anything that can write that JSON will do: a Worker, a serverless function or a scheduled build. The resolver file has two helpers for Open-Meteo, which needs no key. Your coordinates stay in the producer and never reach a page.
import { openMeteoUrl, feedFromOpenMeteo } from 'vanilla-breeze/ambient-resolve' export async function onRequestGet({ request, waitUntil }) { const cached = await caches.default.match(request) if (cached) return cached const upstream = await fetch(openMeteoUrl({ lat: 32.72, lon: -117.16 })) const feed = upstream.ok ? feedFromOpenMeteo(await upstream.json(), { ttl: 1800 }) : null if (!feed) return new Response('{}', { status: 503, headers: { 'content-type': 'application/json' } }) const response = Response.json(feed, { headers: { 'cache-control': 'public, max-age=900' } }) waitUntil(caches.default.put(request, response.clone())) return response}
Open-Meteo's data is CC BY 4.0, so show the attribution somewhere on your site, and its free tier is for non-commercial use: check the terms for your case. For another provider, map its conditions to the vocabulary above; mapWeatherCode(code, windKmh) does that for WMO codes.
Tokens
| Token | Set by | Use |
--color-ambient | Derived | Decorative marks, ornaments, chart accents |
--color-ambient-surface | Derived | Page background, through data-ambient-surface |
--ambient-hue | Season | Absolute oklch hue of the season colour |
--ambient-chroma | Weather | Chroma multiplier, 0.6 to 1.1 |
--ambient-warmth | Time of day | Share of a warm hue mixed in at dawn and dusk |
--ambient-wash | Weather | Share of the accent in the surface, at most 4% |
--ambient-blend | Theme | Share of the theme's primary kept in a season colour (default 25%) |
A season has a hue of its own: winter is a cool blue-grey, spring a soft green, summer a clear blue, autumn a warm brown. The season colour takes its lightness from the theme's primary and keeps a share of it, so it still belongs to the theme. Without a season, --color-ambient is the theme's primary, adjusted by weather and time of day.
At night the surface is left alone on a light page and washed at 4% on a dark one. The time of day never changes the visitor's light or dark mode.
@layer bundle-theme { [data-theme~="swiss"][data-season="winter"] { --ambient-hue: 230; --ambient-season-chroma: 0.5; }}
The treatments reset under forced colours and in print. Token changes transition over 600 ms, and not at all under reduced motion.
Motifs
A motif is a small static icon that appears only where you place a slot. The current icon is --ambient-icon, chosen in this order: the occasion's icon, then the weather, then the season. It is drawn as a mask filled with --color-ambient.
| Slot | Markup | Result |
| Inline mark | <span data-ambient-slot="mark" aria-hidden="true"></span> | The current icon at 1em. Empty, and no space taken, when there is no icon. |
| Rule ornament | <hr data-ornament data-ambient-slot> | The ornament becomes the current icon. With no state the rule keeps its normal ornament. |
| Surface | <body data-ambient-surface> | The background uses --color-ambient-surface. |
| Your own | [data-ambient-slot="name"] | Unstyled hook for site CSS. |
Icon names: snowflake, sprout, sun, leaf, moon, cloud, drops, fog, wind, storm, sparkles, cake, heart, ghost, star, flag, pi, gift. Each is also a token, --ambient-icon-name, so site CSS can use one directly. They come from the Lucide set.
Seasons give a snowflake, sprout, sun or leaf. Weather gives a sun (a moon on a clear night), cloud, fog, drops, storm, snowflake or wind. Motifs are hidden in forced colours and in print, and with data-ambient-level="tint". Add aria-hidden="true" to a mark: it carries no information.
JavaScript API
import { Ambient } from '/cdn/packs/ambient.core.js' Ambient.state Ambient.enabled = false Ambient.override({ season: 'winter', daypart: 'night' }) Ambient.refresh() document.addEventListener('ambient:change', (e) => { e.detail.state e.detail.changed })
The pure resolvers are in packs/ambient.resolve.js and have no DOM access: resolveSeason(date, config), resolveDaypart(date, config, sun), resolveOccasions(date, calendar, config), calendarFor(config), expandCalendar(calendar, years), mapWeatherCode(code, windKmh), and for the feed readFeed(feed), feedFresh(feed, nowMs), openMeteoUrl(place) and feedFromOpenMeteo(data, options).
Visitor control and preview
<button type="button" data-ambient-toggle aria-pressed="true">Ambient styling</button>
The module wires any [data-ambient-toggle] button or checkbox and keeps aria-pressed or checked in step. <settings-panel> shows the same switch in its Environment section on a page that uses the pack. The choice is one localStorage key, vb-ambient.
To preview a state, add ?ambient=winter,night,snow,halloween to the URL. It is honoured only on localhost or when <html> carries data-ambient-debug.
Privacy
The pack sets no cookie and never asks for location. It makes no network request unless you name a weather feed, and then one request to that URL per session and ttl; by default that is your own site, so the browser contacts no third party. It stores two things: the visitor's opt-out in localStorage and the last feed in sessionStorage. The season and time are the author's, by the configured time zone, which is why a visitor elsewhere sees the author's evening and not a guess about their own.
When to Use
Load the ambient pack on a personal or editorial site where a returning reader should notice the site feels like today. Leave it out where colour carries meaning that must not drift, such as a data dashboard with a fixed palette.