@motion-proto/live-tokens 0.60.1 → 0.61.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/dist-plugin/{chunk-XWXIMTWZ.js → chunk-7TQQTHI6.js} +10 -1
- package/dist-plugin/{chunk-LW4SR7AZ.js → chunk-O6GQ6GBH.js} +1 -1
- package/dist-plugin/{chunk-E5QYON4L.js → chunk-OIOXU7FR.js} +33 -2
- package/dist-plugin/generateColorsAndType/index.js +2 -2
- package/dist-plugin/index.cjs +135 -1
- package/dist-plugin/index.js +99 -4
- package/dist-plugin/tokensCssMigrations/index.cjs +32 -1
- package/dist-plugin/tokensCssMigrations/index.js +2 -2
- package/package.json +6 -1
- package/src/editor/component-editor/ImageLightboxEditor.svelte +2 -1
- package/src/editor/docs/Docs.svelte +1 -0
- package/src/editor/docs/chapters.ts +1 -0
- package/src/editor/docs/content/light-and-dark.md +90 -0
- package/src/editor/docs/content.generated.ts +1 -0
- package/src/live-tokens/data/themes/autumn.json +1 -0
- package/src/live-tokens/data/themes/halloween.json +1 -0
- package/src/live-tokens/data/themes/midnight-study.json +1 -0
- package/src/live-tokens/data/themes/ocean.json +1 -0
- package/src/live-tokens/data/themes/royal-velvet.json +1 -0
- package/src/live-tokens/data/themes/sketchy.json +1 -0
- package/src/live-tokens/data/themes/spring-meadow.json +1 -0
- package/src/live-tokens/data/themes/sunset.json +1 -0
- package/src/live-tokens/data/tokens.generated.css +5 -0
- package/src/system/backdrop/backdrop.ts +197 -0
- package/src/system/backdrop/index.ts +16 -0
- package/src/system/backdrop/watch.ts +139 -0
- package/src/system/components/FloatingTokenTags.svelte +1 -1
- package/src/system/components/ImageLightbox.svelte +4 -3
- package/src/system/styles/tokens.css +20 -0
- package/src/system/internal/backgroundContrast.ts +0 -62
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Light and dark
|
|
2
|
+
|
|
3
|
+
Some things on a page cannot be written as a token. A wordmark drawn in white
|
|
4
|
+
disappears on a pale theme. Ink that multiplies onto paper vanishes on a dark
|
|
5
|
+
one. A photograph behind a headline is dark no matter what the palette says.
|
|
6
|
+
|
|
7
|
+
Each of those needs the same fact first: which way does the surface behind this
|
|
8
|
+
thing lean? One attribute carries it.
|
|
9
|
+
|
|
10
|
+
## The attribute
|
|
11
|
+
|
|
12
|
+
`data-backdrop` is either `light` or `dark`, and it does two things at once: it
|
|
13
|
+
selects, so a rule can key on it, and it sets `color-scheme`, so every
|
|
14
|
+
`light-dark()` under it resolves the half that reads.
|
|
15
|
+
|
|
16
|
+
```css
|
|
17
|
+
.title {
|
|
18
|
+
color: light-dark(var(--color-black), var(--color-white));
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
That line is right on both sides of the theme, and it is right inside a dark
|
|
23
|
+
band on a pale page, because the nearest `color-scheme` wins.
|
|
24
|
+
|
|
25
|
+
## Stating it
|
|
26
|
+
|
|
27
|
+
Put it in the markup when the surface knows its own tone — a hero over a
|
|
28
|
+
photograph, a plate that stays pale in every theme:
|
|
29
|
+
|
|
30
|
+
```svelte
|
|
31
|
+
<div class="hero-panel" data-backdrop="dark">
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
A stated tone beats any measurement, and it inherits, so everything inside the
|
|
35
|
+
panel resolves against it.
|
|
36
|
+
|
|
37
|
+
## Measuring it
|
|
38
|
+
|
|
39
|
+
Where the tone is a property of the theme rather than of the markup, let it be
|
|
40
|
+
measured:
|
|
41
|
+
|
|
42
|
+
```svelte
|
|
43
|
+
<script>
|
|
44
|
+
import { backdrop } from '@motion-proto/live-tokens/backdrop';
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<section use:backdrop>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The action reads whatever actually paints behind the element — the nearest
|
|
51
|
+
ancestor with an opaque fill, averaged across its gradient stops, falling back
|
|
52
|
+
to the theme's `--page-bg` — and stamps the answer. It re-reads when the theme
|
|
53
|
+
changes, which the editor does by rewriting custom properties with no reload,
|
|
54
|
+
so the stamp follows a live edit.
|
|
55
|
+
|
|
56
|
+
The page itself is stamped for you: the build bakes the production theme's
|
|
57
|
+
polarity into `tokens.generated.css`, so the first paint is already right, and
|
|
58
|
+
`syncDocumentBackdrop()` keeps `<html>` current as themes switch.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { syncDocumentBackdrop } from '@motion-proto/live-tokens/backdrop';
|
|
62
|
+
|
|
63
|
+
syncDocumentBackdrop();
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Reading it from JavaScript
|
|
67
|
+
|
|
68
|
+
Anything that paints outside CSS — a canvas, a WebGL uniform, an `<img>` that
|
|
69
|
+
comes in two versions — asks the same question through the same module:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { isLightBackdrop, watchBackdrop, cssColorToHex } from '@motion-proto/live-tokens/backdrop';
|
|
73
|
+
|
|
74
|
+
const stop = watchBackdrop(logoEl, {
|
|
75
|
+
stamp: false,
|
|
76
|
+
onChange: (polarity) => (src = polarity === 'light' ? darkMark : lightMark),
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`isLightBackdrop(el)` answers once. `watchBackdrop` keeps answering and returns
|
|
81
|
+
a stop function. `cssColorToHex` resolves any CSS colour — including the
|
|
82
|
+
`oklch()` a token holds — to a hex a non-CSS consumer can take.
|
|
83
|
+
|
|
84
|
+
## What it does not do
|
|
85
|
+
|
|
86
|
+
Polarity is a property of a surface, not of a component, so nothing is stamped
|
|
87
|
+
for you below `<html>`: a section that needs an answer either states one or asks
|
|
88
|
+
for one. And a measurement reads the paint at the moment it runs — an element
|
|
89
|
+
that scrolls from a pale band onto a dark one keeps the answer it was given.
|
|
90
|
+
State the tone on each band instead.
|
|
@@ -6,6 +6,7 @@ export const docContent: Record<string, string> = {
|
|
|
6
6
|
"creating-components": "# Creating components\n\nThe package ships about 25 editable components. When you need one it doesn't\nhave, you can make your own Svelte component editable, so anyone using the\neditor can re-point its colours, type, and spacing without touching code.\n\nThe simplest way is to ask Claude. The package bundles a Claude Code skill that\nknows the conventions, writes the files, and checks the result for you.\n\n## Install the skills\n\n```bash\nnpx @motion-proto/live-tokens setup-claude\n```\n\nThis copies the bundled skills into your project's `.claude/skills/`. Once\nthey're there, Claude Code picks them up automatically.\n\n## Ask for a component\n\nDescribe what you want in plain English. Phrases like these trigger the skill:\n\n- \"Add a Toggle component to live-tokens\"\n- \"Make this Svelte component editable in the live-tokens editor\"\n- \"Create a Stat component with a value and a label\"\n\nClaude asks any clarifying questions it needs (which variants, which states,\nwhich parts), then writes the component, registers it with the editor, and runs\nits verification checklist. When it finishes, open `/live-tokens/components` to see your new\ncomponent in the editor and confirm everything works.\n\n## What you get\n\n- A runtime component whose editable properties default to your theme tokens.\n- An editor entry that appears under **Custom** in the `/live-tokens/components` view.\n- The naming and wiring handled for you, so the component fits the system.\n\nAdvanced authors who want to write a component by hand can read the naming and\nstate-model conventions shipped in the package\n(`src/system/styles/CONVENTIONS.md` and the skill's own `SKILL.md`).\n",
|
|
7
7
|
"editing-tokens": "# Editing tokens\n\nA tour of the editor. The page behind it repaints on every change; saving\nwrites a theme file you can reload later.\n\nThe editor has four views:\n\n- **Tokens**: the design-system primitives (colour, type, spacing, and so on).\n They apply everywhere your site uses them.\n- **Color Wheel**: the harmony wheel, the palette curves, and the story your colours\n tell across a page.\n- **Components**: per-component editors. Re-Assign what tokens a component uses\n without changing the underlying system.\n- **Sketch Style**: an effect layer that redraws the page by hand. See\n [Sketch mode](sketch-mode.md).\n\nThis page covers **Tokens**. For components, see\n[Creating components](creating-components.md).\n\n## Palettes\n\nMost colour work happens here. Each palette (Brand, Accent, Neutral, Canvas,\nSuccess, Warning, Info, Danger, and a few more) has:\n\n- **Base colour.** Pick a hex; the palette derives an 11-step ramp (100 to 950)\n from it.\n- **Curves.** Three curves shape the ramp, in stack order: Hue, Saturation,\n Lightness. Drag the handles to bias it warmer or cooler, more or less\n saturated, darker or lighter. Hue drifts the ramp's temperature without\n moving contrast, because OKLCH hue rotation is close to lightness-preserving.\n It holds ±45 degrees; a bigger shift belongs on the base colour or the\n harmony axis.\n- **Overrides.** Lock a single step to a hand-picked hex when the curve doesn't\n land where you want.\n\nEditing a palette base ripples through every colour that depends on it, in real\ntime. Colours use OKLCH, so the ramp stays perceptually even across hues\nwithout muddy mid-tones.\n\n## Type\n\n- **Fonts.** Add sources from Google Fonts, Adobe (Typekit), a CSS URL, or an\n inline `@font-face`. The font loads in the page as soon as you add it.\n- **Stacks.** Named font cascades you reference by token, such as a display\n stack and a body stack.\n- **Sizes and weights.** A t-shirt scale (xs, sm, md, lg, xl, 2xl…) for size and\n a numeric scale (100 to 900) for weight.\n\n## Spacing, radius, shadow\n\nNumeric scales with a slider per step.\n\n- **Spacing**: the padding, gap, and margin scale.\n- **Radius**: none through full.\n- **Shadow**: colour, offset, blur, spread, and opacity per step, with stacked\n shadows supported.\n\nChange a step and every element using it repaints.\n\n## Overlays and gradients\n\n- **Overlays** are translucent tints layered over surfaces, like the subtle\n tint a card gets on hover. Set a colour and opacity per state.\n- **Gradients** are reusable gradient tokens with a stop list and direction, for\n hero panels and accent backgrounds.\n\n## Columns\n\nThe page-grid overlay. Set column count, gutter, and outer margin, and toggle\nthe visual guide with `Cmd/Ctrl+G`. Pages built on the column system reflow\nlive.\n\n## Saving\n\nThe editor saves to your browser continuously, so work survives a reload\nmid-edit. Writing a file is a separate step: the **Theme** panel at the foot of\nthe sidebar has **Save**, **Save As**, and **Load**, and each theme is one JSON\nfile under `src/live-tokens/data/themes/`.\n\nThe header gives you undo/redo (`Cmd/Ctrl+Z`, `Cmd/Ctrl+Shift+Z`). You can keep\nmany themes side by side; one is open at a time, and only **Adopt** publishes\none. See [Themes](themes-workflow.md) for the full lifecycle.\n",
|
|
8
8
|
"getting-started": "# Getting started\n\nScaffold a live token site in a moments. You need Node 20 or later, a\npackage manager (npm, pnpm, or yarn), and a browser. Open claude code in your repo and start building.\n\n## Scaffold a new app\n\n```bash\nnpm create @motion-proto/live-tokens@latest my-app\ncd my-app\nnpm install\nnpm run dev\n```\n\nOpen the URL Vite prints (usually `http://localhost:5173`). You get a\none-page Svelte + Vite app that depends on the published package, with the\neditor wired up and the full component set ready to import.\n\n`npx @motion-proto/live-tokens create my-app` runs the same scaffold without\nthe initialiser package.\n\n### What the scaffold gives you\n\nEvery editable file lives under `src/` and is committed, so `npm install` and\nversion upgrades never touch your styles. The package code stays in\n`node_modules`.\n\n| Path | What it is |\n|------|------------|\n| `src/pages/Home.svelte` | The starter page. Replace it with your own content. |\n| `src/App.svelte` | Your routes. `<LiveTokensRouter>` adds dev-only routes under a reserved `/live-tokens/*` namespace: `/live-tokens/editor`, `/live-tokens/components`, and `/live-tokens/docs`. |\n| `src/system/styles/tokens.css` | Your base token vocabulary, hand-authored. |\n| `src/styles/site.css` | Themed page typography, yours to edit. |\n\n## Your first edit\n\n1. Run `npm run dev` and open the home page.\n2. Click **Open Token Editor**, or visit `/live-tokens/editor`. The editor opens beside\n the page.\n3. Open **Palettes**, pick **Brand**, and change the base hex. The page\n repaints as you type.\n4. In the **Theme** panel at the foot of the sidebar, choose **Save As**. Your\n theme appears as JSON under `src/live-tokens/data/themes/`.\n5. Reload. The editor reopens on your theme, so the page returns as you left\n it.\n\n## What you just changed\n\nEvery edit sets a CSS custom property on `:root`. Your components read those\nproperties through `var(--...)`. There is no token build step and no\npreprocessor rewriting your code: the page renders against plain CSS variables\nthe editor swaps live.\n\nTo ship, click **Adopt** in the Theme panel. That saves the open theme and bakes\nit into `src/live-tokens/data/tokens.generated.css`, which your build bundles\nalongside `tokens.css`. Adopt is the only action that changes what your site\nships, so try any look you like first. The editor itself never reaches\nproduction.\n\nAlready have a Svelte 5 + Vite app? The\n[README](https://github.com/motionproto/live-tokens#readme) covers installing\ninto an existing project.\n\n## Where to go next\n\n- **[Editing tokens](editing-tokens.md)**: a tour of the editor.\n- **[Themes](themes-workflow.md)**: save, switch, and ship.\n- **[Creating components](creating-components.md)**: make your own component\n editable.\n",
|
|
9
|
+
"light-and-dark": "# Light and dark\n\nSome things on a page cannot be written as a token. A wordmark drawn in white\ndisappears on a pale theme. Ink that multiplies onto paper vanishes on a dark\none. A photograph behind a headline is dark no matter what the palette says.\n\nEach of those needs the same fact first: which way does the surface behind this\nthing lean? One attribute carries it.\n\n## The attribute\n\n`data-backdrop` is either `light` or `dark`, and it does two things at once: it\nselects, so a rule can key on it, and it sets `color-scheme`, so every\n`light-dark()` under it resolves the half that reads.\n\n```css\n.title {\n color: light-dark(var(--color-black), var(--color-white));\n}\n```\n\nThat line is right on both sides of the theme, and it is right inside a dark\nband on a pale page, because the nearest `color-scheme` wins.\n\n## Stating it\n\nPut it in the markup when the surface knows its own tone — a hero over a\nphotograph, a plate that stays pale in every theme:\n\n```svelte\n<div class=\"hero-panel\" data-backdrop=\"dark\">\n```\n\nA stated tone beats any measurement, and it inherits, so everything inside the\npanel resolves against it.\n\n## Measuring it\n\nWhere the tone is a property of the theme rather than of the markup, let it be\nmeasured:\n\n```svelte\n<script>\n import { backdrop } from '@motion-proto/live-tokens/backdrop';\n</script>\n\n<section use:backdrop>\n```\n\nThe action reads whatever actually paints behind the element — the nearest\nancestor with an opaque fill, averaged across its gradient stops, falling back\nto the theme's `--page-bg` — and stamps the answer. It re-reads when the theme\nchanges, which the editor does by rewriting custom properties with no reload,\nso the stamp follows a live edit.\n\nThe page itself is stamped for you: the build bakes the production theme's\npolarity into `tokens.generated.css`, so the first paint is already right, and\n`syncDocumentBackdrop()` keeps `<html>` current as themes switch.\n\n```ts\nimport { syncDocumentBackdrop } from '@motion-proto/live-tokens/backdrop';\n\nsyncDocumentBackdrop();\n```\n\n## Reading it from JavaScript\n\nAnything that paints outside CSS — a canvas, a WebGL uniform, an `<img>` that\ncomes in two versions — asks the same question through the same module:\n\n```ts\nimport { isLightBackdrop, watchBackdrop, cssColorToHex } from '@motion-proto/live-tokens/backdrop';\n\nconst stop = watchBackdrop(logoEl, {\n stamp: false,\n onChange: (polarity) => (src = polarity === 'light' ? darkMark : lightMark),\n});\n```\n\n`isLightBackdrop(el)` answers once. `watchBackdrop` keeps answering and returns\na stop function. `cssColorToHex` resolves any CSS colour — including the\n`oklch()` a token holds — to a hex a non-CSS consumer can take.\n\n## What it does not do\n\nPolarity is a property of a surface, not of a component, so nothing is stamped\nfor you below `<html>`: a section that needs an answer either states one or asks\nfor one. And a measurement reads the paint at the moment it runs — an element\nthat scrolls from a pale band onto a dark one keeps the answer it was given.\nState the tone on each band instead.\n",
|
|
9
10
|
"sketch-mode": "# Sketch mode\n\nSketch mode redraws your whole page as if it had been drawn by hand. Every\ncomponent keeps its own colours, spacing and corners; what changes is the line\nthey are drawn with.\n\nIt is an effect layer, not a set of token values. It reads nothing from your\ntheme and writes nothing back, so it never touches a token, never lands in a\ntheme file, and never reaches the CSS you ship. Turn it off and every trace of\nit goes.\n\nOpen the **Sketch Style** view in the editor and switch **Sketch mode** on. The effect\napplies to the page behind the editor as well as to the preview, so what you see\nin context is what it does.\n\n## What it draws\n\nEach component's fill and outline are repainted from the tokens that component\nalready owns. The real background and border are hidden behind them, then both\nare pushed around one shared field of noise. Because every component samples the\nsame field, the whole page reads as one drawing rather than as a set of\nseparately wobbled boxes.\n\n## The presets\n\nSeven looks ship with the package, and each is a complete set of dials rather\nthan a style name:\n\n- **Pencil.** Two graphite passes on their own seeds, so the outline disagrees\n with itself the way a hand coming back round does.\n- **Marker.** A broad translucent nib gone round twice on the same line, so the\n overlap darkens and the ink pools where it slows.\n- **Whiteboard.** The fattest nib on glass, with a mask that streaks the fill\n like a half-wiped board.\n- **Hatched.** An etching. The fill is angled shading and the outline a single\n hard-edged scratch.\n- **Dashed.** A drafting outline: one slow drift along the ruler, broken into\n strokes. The clean pole.\n- **Napkin.** Ballpoint in a hurry. Everything loose at once.\n- **Dry marker.** Ink that ran out. One scratchy pass over a mostly eaten fill.\n\nPick one, then move whatever you like. **Save** keeps your dials under a name of\nyour own, alongside the shipped seven, as a file under\n`src/live-tokens/data/sketch-presets/`.\n\n## The dials\n\n- **Border.** How far the outline travels and how long its wave is, then its\n width, ink, pressure and pooling. A second pass either copies the first line a\n few pixels off or runs it through the pen again on its own seed.\n- **Fill.** Solid or hatched, how far the fill's edge travels, and how far each\n instance is offset, rotated and scaled from its neighbours. **Ink coverage**\n thins the fill with a field of blotches: set their size, how many levels of\n detail, how pale and how dense they go, and how soft their edges are.\n- **Shape.** **Corner spread** rounds each corner by its own share of the dial,\n so no two match. **Corner travel** leans the drawn box into a quadrilateral\n with no two sides parallel. This is the dial that stops a component reading as\n a rectangle.\n- **Icons and SVG.** Glyph travel and wavelength on their own scale. A glyph is\n all curves already, so it needs more travel than a card's long straight edge\n before the wobble reads at all.\n- **Noise.** The shared field itself: its wavelength, how many layers of detail\n sit on it, and the shape of its wave. A square wave sends nearly every edge to\n full travel, which is what makes the effect stronger rather than bigger.\n\n## Where the settings live\n\nThe dials you are moving live in your browser, so the effect follows you across\nreloads and stays off everyone else's screen. **Save** writes a named preset to\n`src/live-tokens/data/sketch-presets/`, which is the only thing that reaches\ndisk.\n\nSketch mode is a tool for looking at the page, not a layer the page can ship.\nNothing is written into a theme, `tokens.generated.css` never sees it, and a\nproduction build has no sketch layer in it at all.\n\n## Drawing your own elements\n\nThe layer draws a fixed set of parts: the shipped components, and four classes\nit reserves for you. Nothing else is touched, so a page element or a\nconsumer-authored component is left crisp until it carries one of them.\n\n| Class | For |\n|---------------------|-----------------------------------------------------------|\n| `sketch-surface` | A box. The default treatment. |\n| `sketch-container` | A large box. Tilts less, so the type inside stays readable. |\n| `sketch-chip` | A small box. Finer fill mask, more rotation, less travel. |\n| `sketch-rule` | A line rather than a box. No rotation, no rounded ends. |\n\nPick by size, not by kind: a card and a modal both take `sketch-container`, a\nbadge and a pill both take `sketch-chip`.\n\nThe class opts the element in; it names no colours, so the element states its\nown. `--sketch-fill`, `--sketch-stroke`, `--sketch-hatch-color`,\n`--sketch-radius` and `--sketch-shadow` name the fill, the outline, the hatching\nink, the corners and the shadow for one element and everything inside it. The\nlayer blanks the real background and border, so an element whose fill matters\nunder Sketch mode has to name it here as well as paint it.\n\nThe layer also paints on the element's `::before` and `::after`, forces its\n`overflow` visible, and gives it a stacking context of its own. Keep the class\noff anything that owns a pseudo-element, clips its content, or is positioned\nabsolutely, and put it on a wrapper instead.\n\n```css\n.my-callout {\n background: var(--surface-brand-lowest);\n border: var(--border-width-1) solid var(--border-brand);\n border-radius: var(--radius-xl);\n\n --sketch-fill: var(--surface-brand-lowest);\n --sketch-stroke: var(--border-brand);\n --sketch-radius: var(--radius-xl);\n}\n```\n\nA gradient is a valid fill: the shorthand's last layer takes a colour or an\nimage, so `--sketch-fill` accepts either. States work the same way, since\nnothing is competing with you for the value:\n\n```css\n.my-callout:hover { --sketch-stroke: var(--border-brand-strong); }\n```\n\n## Images inside a drawn part\n\nA drawn part's `overflow` is forced visible, because the fill and outline are\npainted on pseudo-elements that travel past the box and would otherwise be cut\noff at its edge. A background that bleeds is the effect working. An image that\nbleeds is not: it keeps its square corners while the card around it turns.\n\nMedia that runs to a part's edge therefore has to carry that part's corners\nitself. `--sketch-radius` is the radius the layer drew, and it inherits, so a\nchild can read it and fall back to its own value when Sketch mode is off:\n\n```css\n.cover {\n overflow: hidden;\n border-top-left-radius: var(--sketch-radius, var(--card-default-radius));\n border-top-right-radius: var(--sketch-radius, var(--card-default-radius));\n}\n```\n\nCorner spread is per-corner and per-instance, so at high spread the crop is the\nmean rather than an exact trace of the drawn edge.\n\nA rule made from a `border` is not a box and cannot be displaced. Make it an\nelement, give it `sketch-rule`, and name its ink:\n\n```html\n<div class=\"rule sketch-rule\"></div>\n```\n```css\n.rule {\n height: var(--border-width-2);\n background: var(--border-brand);\n --sketch-fill: var(--border-brand);\n}\n```\n\nIcons and inline SVG take the wobble directly, since a glyph has no box to\nredraw. Body type is left alone: an icon is a shape and survives a wobble, a\nparagraph is not.\n\n`--sketch-icon-off` names what a subtree's glyphs are drawn with instead. It\ninherits, so one declaration covers everything under it, and it takes the ink\nmask off as well as the wobble:\n\n```css\n/* Crisp. Chrome, a logo, anything that has to stay exact. */\n.app-bar { --sketch-icon-off: none; }\n\n/* Drawn back rather than off, at a third of the travel. Small artwork, and\n type set as an SVG, which the layer reads as one large glyph. */\n.wordmark { --sketch-icon-off: var(--sketch-icon-soft); }\n```\n\nThe **Blotch size** dial under Icons and SVG is a share of the glyph rather than\na px size, because no px size is right for both a 16px icon and a page-wide\ndrawing. At 100% every glyph gets one period of the field across it whatever its\nsize. Below that the field repeats inside the glyph and the blotches get finer.\nAbove it a glyph reads part of one blotch, so the mask thins the whole glyph\nunevenly instead of breaking it up. The fill's blotches stay in px, since a\ncomponent does have a size to state one against.\n",
|
|
10
11
|
"themes-workflow": "# Themes\n\nSave your work, switch between looks, and ship one to production.\n\n## The Theme panel\n\nThe **Theme** panel at the foot of the editor sidebar holds the whole look:\ncolors, type, and a setting for every component, in one file. It carries the\nname the look ships under, whether production is running it, and **Adopt**.\nTwo parts sit under it, each a read-out rather than a file to manage.\n\n- **Colors & Type** holds the design tokens. Components read those tokens to\n define their appearance. It names the two faces the page is showing.\n- **Components** counts how many components have an unsaved edit that has not\n been saved into the theme, and opens the component editors.\n\nA theme holds its own copy of every part, so one theme can never break another.\n\n## How themes work\n\nA theme is a document, and the editor works the way any editor does.\n\n- **A theme** is a named JSON file in `src/live-tokens/data/themes/`. It carries\n the whole look: the colors and type plus a setting for every component.\n- **The open theme** is the one the editor is working on, named in\n `themes/_active.json`. One at a time.\n- **Your unsaved edits** are what the page shows right now. The editor keeps\n them in your browser as you work and writes them to a buffer, `_working.json`,\n one slot per part of the look. **Save** captures that buffer into the open\n theme.\n- **The production theme** is the one your site ships, named in\n `themes/_production.json`. **Adopt** changes it; saving a preset in the Theme\n Picker performs that Adopt for you.\n\nAbsence is the answer for anything untouched: a buffer exists only where the\nlive look diverges from the active theme, so a newly opened theme has none.\n\n## Fonts\n\nType is part of the look, so it saves, loads and ships with the theme rather\nthan on its own. Four named stacks carry it:\n\n| Stack | Used by |\n|---|---|\n| `--font-display` | headings |\n| `--font-sans` | body text and most UI |\n| `--font-serif` | anywhere you ask for it |\n| `--font-mono` | code |\n\nEach stack is a family followed by its fallbacks, so a page still reads while a\nweb font loads, and still reads if it never does. **Project fonts**, in the\nColors and type editor, is where families come from: type a Google Fonts family\nname and the editor checks it, or paste a fonts URL, an embed tag, or your own\n`@font-face` rules. Removing a family puts the stack back on its fallbacks.\n\nYou can also set both faces at once from the command line:\n\n```bash\nnpx live-tokens set-fonts fonts.json\n```\n\nwith a brief naming the families:\n\n```json\n{ \"display\": \"Fraunces\", \"body\": \"Nunito Sans\" }\n```\n\nIt checks each family against Google Fonts, works out the weights that family\nactually has, and binds it to its stack. Like every other edit, the result lands\nin the buffer, so **Save** keeps it. In Claude Code, asking for a font pairing in\nplain English runs the same command.\n\nA font is only requested by the browser once something on the page uses it, so\ncarrying a family you no longer reference costs nothing at load. Adopting is\nwhat writes the font imports your site ships, into `fonts.css`.\n\n## Saving\n\nIn the Theme panel:\n\n- **Save** captures the look on screen into the open theme. Your colors and type\n go in as part of it, so there is nothing to save first.\n- **Save As** names a new theme. Use it for your first save and for forking.\n\nComponent editors keep their own unsaved state. If one or more components are\nwaiting when you use **Save**, **Save As**, or **Adopt**, the Theme panel offers\nto save all of them before continuing. You can accept once instead of visiting\neach component, or cancel to review them individually. A component editor's\n**Save As** creates a reusable component preset.\n\nNames are tidied to lowercase with hyphens, so \"My Brand!\" becomes `my-brand`,\nand a leading underscore is dropped: those names are reserved for the buffer.\n**Motion Proto** is the built-in theme and is read-only. You can always return\nto it, and the editor never overwrites it, so start your own with **Save As**.\n\n## Switching\n\n**Load**—or clicking the active theme's name—opens the Theme Picker. Picking a\ntheme shows it on the page as a preview with nothing written to disk, so you can\ntry each look and compare. **Save** in that window opens and adopts the previewed\ntheme in one step: the active pointer changes, the buffers clear, the editor\nworks on it, and production ships it. **Cancel** returns you to where you were.\nPreviewing alone never changes what your site ships.\n\n**Colors and type only. Keep my shapes.** narrows the load to the palette and\nthe fonts: your component settings stay as they are and the theme you have open\nstays open. Saved colors and type files are listed there too, marked *colors &\ntype*, and picking one is always that narrower load.\n\n## Shipping\n\n**Adopt**, in the Theme panel, is the \"ship it\" step, and it ships the whole\nlook. It saves the open theme, then bakes that theme into\n`src/live-tokens/data/tokens.generated.css`, which your build bundles alongside\n`tokens.css`: the colors and type plus every component the theme carries. Fonts\nregenerate to match. The line under the theme name says whether production is\nrunning this theme.\n\nProduction is one saved theme, so nothing else publishes. Trying a look, moving\na token, saving a theme: all of it leaves the generated CSS alone until you\nAdopt. A component editor's Adopt runs the same whole-look step, because a\ncomponent never ships alone. Adopting while Motion Proto is open saves your look\nas a theme of your own first, since the built-in one is read-only.\n\nProduction builds (`npm run build`) ship only that plain CSS and your\ncomponents. No editor, no JSON loading, no runtime indirection.\n\n## Keeping your work safe\n\nEverything under `src/live-tokens/data/` is plain JSON, so commit it. Themes show\nup as readable diffs you can review per branch, and the buffer shows up as the\nwork you have not saved into a theme yet. Nothing is backed up anywhere else:\ngit is your safety net. To experiment freely, **Save As** a new name first, then\nedit.\n\n## Where to go next\n\n- **[Where themes live](where-themes-live.md)**: the files behind all of this,\n and what writes each one.\n- **[Creating components](creating-components.md)**: make your own components\n editable in the same editor.\n",
|
|
11
12
|
"where-themes-live": "# Where themes live\n\nEverything the editor writes is plain JSON and CSS inside your project. There\nis no database and no hidden state: the files are the storage, and git is the\nhistory.\n\n## The data tree\n\n```\nsrc/live-tokens/data/\n themes/\n _active.json names the theme the editor has open\n _production.json names the theme your site ships\n default.json Motion Proto, the built-in look, rewritten at boot\n my-brand.json a saved theme: the whole look in one file\n colors-and-type/\n _working.json unsaved colors and type edits\n component-configs/\n button/\n default.json Button's shipped settings, derived at boot\n _working.json unsaved Button edits\n my-button.json a preset you saved from the Button editor\n tokens.generated.css the baked CSS your production build ships\nsrc/system/styles/\n tokens.css your token vocabulary, hand-authored, never written\n fonts.css font imports, rewritten when you Adopt\n```\n\nA saved theme carries the whole look by value: the colors and type plus a\nsetting for every component. It depends on no other file, so deleting\nanything else never breaks it.\n\n## What writes when\n\n- **Editing** changes the page through CSS variables. The editor keeps your\n edits in the browser as you work and writes them to the `_working.json`\n buffers when you save a component. When the Theme panel finds several dirty\n components, **Save all** writes those buffers together.\n- **Save** captures the buffers into the open theme's file. That file is the\n durable copy of your look; matching buffers are then removed.\n- **Load** clears the buffers and points `themes/_active.json` at the theme you\n picked. Live reads fall through to that file. Nothing else changes, so trying\n looks is free and ordinary switching changes only the pointer.\n- **Adopt** points `themes/_production.json` at the open theme, bakes it into\n `tokens.generated.css`, and rewrites `fonts.css` to match. It is the only\n action that changes what your site ships.\n\nThe `default.json` files are the shipped baseline. The editor derives them at\nboot and refreshes them when the package updates; it never saves your work\nover them.\n\nProjects upgraded from 0.48 may initially contain working files copied from the\nactive theme. On the first dev-server boot, exact copies are removed\nautomatically. Any file that differs is kept as unsaved work, so no migration\ncommand is required.\n\n## What to commit\n\nAll of it. The data tree is designed to live in git: themes diff readably, the\ntwo pointers say what is open and what ships, and a `_working.json` in a diff\nis exactly the work you have not yet saved into a theme. Nothing is backed up\nanywhere else.\n\n## Where to go next\n\n- **[Themes](themes-workflow.md)**: the workflow built on these files: saving,\n loading, and shipping.\n",
|
|
@@ -3166,6 +3166,7 @@
|
|
|
3166
3166
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3167
3167
|
"updatedAt": "2026-08-19T22:39:32.785Z",
|
|
3168
3168
|
"aliases": {
|
|
3169
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3169
3170
|
"--imagelightbox-tile-radius": "--radius-4xl",
|
|
3170
3171
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3171
3172
|
"--imagelightbox-tile-border-width": "--border-width-0",
|
|
@@ -3205,6 +3205,7 @@
|
|
|
3205
3205
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3206
3206
|
"updatedAt": "2026-08-19T22:39:32.785Z",
|
|
3207
3207
|
"aliases": {
|
|
3208
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3208
3209
|
"--imagelightbox-tile-radius": "--radius-none",
|
|
3209
3210
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3210
3211
|
"--imagelightbox-tile-border-width": "--border-width-2",
|
|
@@ -3212,6 +3212,7 @@
|
|
|
3212
3212
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3213
3213
|
"updatedAt": "2026-08-19T22:39:32.785Z",
|
|
3214
3214
|
"aliases": {
|
|
3215
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3215
3216
|
"--imagelightbox-tile-radius": "--radius-2xl",
|
|
3216
3217
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3217
3218
|
"--imagelightbox-tile-border-width": "--border-width-0",
|
|
@@ -3165,6 +3165,7 @@
|
|
|
3165
3165
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3166
3166
|
"updatedAt": "2026-08-19T22:39:32.785Z",
|
|
3167
3167
|
"aliases": {
|
|
3168
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3168
3169
|
"--imagelightbox-tile-radius": "--radius-4xl",
|
|
3169
3170
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3170
3171
|
"--imagelightbox-tile-border-width": "--border-width-0",
|
|
@@ -3196,6 +3196,7 @@
|
|
|
3196
3196
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3197
3197
|
"updatedAt": "2026-08-19T22:39:32.785Z",
|
|
3198
3198
|
"aliases": {
|
|
3199
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3199
3200
|
"--imagelightbox-tile-radius": "--radius-2xl",
|
|
3200
3201
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3201
3202
|
"--imagelightbox-tile-border-width": "--border-width-1",
|
|
@@ -3179,6 +3179,7 @@
|
|
|
3179
3179
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3180
3180
|
"updatedAt": "2026-08-25T01:13:11.520Z",
|
|
3181
3181
|
"aliases": {
|
|
3182
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3182
3183
|
"--imagelightbox-tile-radius": "--radius-none",
|
|
3183
3184
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3184
3185
|
"--imagelightbox-tile-border-width": "--border-width-1",
|
|
@@ -3152,6 +3152,7 @@
|
|
|
3152
3152
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3153
3153
|
"updatedAt": "2026-08-19T22:39:32.785Z",
|
|
3154
3154
|
"aliases": {
|
|
3155
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3155
3156
|
"--imagelightbox-tile-radius": "--radius-3xl",
|
|
3156
3157
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3157
3158
|
"--imagelightbox-tile-border-width": "--border-width-0",
|
|
@@ -3204,6 +3204,7 @@
|
|
|
3204
3204
|
"createdAt": "2026-05-26T02:03:38.863Z",
|
|
3205
3205
|
"updatedAt": "2026-08-19T22:39:32.785Z",
|
|
3206
3206
|
"aliases": {
|
|
3207
|
+
"--imagelightbox-tile-surface": "--color-transparent",
|
|
3207
3208
|
"--imagelightbox-tile-radius": "--radius-3xl",
|
|
3208
3209
|
"--imagelightbox-tile-border": "--color-transparent",
|
|
3209
3210
|
"--imagelightbox-tile-border-width": "--border-width-0",
|
|
@@ -347,3 +347,8 @@
|
|
|
347
347
|
--font-mono: "fira-code", ui-monospace, "SF Mono", Menlo, Consolas, monospace, monospace;
|
|
348
348
|
--font-editorial: "Manrope", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, sans-serif;
|
|
349
349
|
}
|
|
350
|
+
|
|
351
|
+
/* Page polarity, from --page-bg */
|
|
352
|
+
:where(:root) {
|
|
353
|
+
color-scheme: dark;
|
|
354
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What paints behind a thing, and which way it leans.
|
|
3
|
+
*
|
|
4
|
+
* A theme is not light or dark by declaration — nothing in a saved theme
|
|
5
|
+
* records it — so polarity is either stated on the markup (`data-backdrop`) or
|
|
6
|
+
* measured from the colours that actually paint. Both answers arrive through
|
|
7
|
+
* `polarityOf`, and every consumer that used to hand-roll one of the two reads
|
|
8
|
+
* it from here.
|
|
9
|
+
*/
|
|
10
|
+
import { parseOklchCss, oklchToRgb255 } from '../internal/oklch';
|
|
11
|
+
|
|
12
|
+
export type Polarity = 'light' | 'dark';
|
|
13
|
+
export type ContrastColorToken = '--color-black' | '--color-white';
|
|
14
|
+
|
|
15
|
+
export const BACKDROP_ATTRIBUTE = 'data-backdrop';
|
|
16
|
+
|
|
17
|
+
/** Relative luminance at or above which a backdrop counts as light. Sits
|
|
18
|
+
between the palest shipped dark theme (~0.03) and the darkest light one
|
|
19
|
+
(~0.6), so no shipped look lands near the line. */
|
|
20
|
+
export const LIGHT_BACKDROP = 0.2;
|
|
21
|
+
|
|
22
|
+
/** A fill this see-through says nothing about what shows through it, so the
|
|
23
|
+
walk keeps looking up the tree. */
|
|
24
|
+
const OPAQUE = 0.98;
|
|
25
|
+
|
|
26
|
+
type Rgba = { r: number; g: number; b: number; a: number };
|
|
27
|
+
|
|
28
|
+
const HEX_RE = /#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})\b/gi;
|
|
29
|
+
const OKLCH_RE = /oklch\(\s*[\d.]+%?\s+[\d.]+%?\s+[\d.]+(?:deg)?\s*(?:\/\s*([\d.]+%?)\s*)?\)/gi;
|
|
30
|
+
const RGB_RE = /rgba?\(\s*([\d.]+%?)\s*[, ]\s*([\d.]+%?)\s*[, ]\s*([\d.]+%?)(?:\s*[,/]\s*([\d.]+%?))?\s*\)/gi;
|
|
31
|
+
/** Anything the parsers above miss — `color-mix()`, a named colour, `lab()`.
|
|
32
|
+
Carries no nested parens in any value a computed style hands back. */
|
|
33
|
+
const EXOTIC_RE = /\b(?:color-mix|hwb|lab|lch|oklab|color)\([^()]*\)/gi;
|
|
34
|
+
|
|
35
|
+
function channel(value: string): number {
|
|
36
|
+
const parsed = Number.parseFloat(value);
|
|
37
|
+
return value.endsWith('%') ? parsed * 2.55 : parsed;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function alpha(value: string | undefined): number {
|
|
41
|
+
if (value === undefined) return 1;
|
|
42
|
+
const parsed = Number.parseFloat(value);
|
|
43
|
+
return value.endsWith('%') ? parsed / 100 : parsed;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function parseHex(hex: string): Rgba {
|
|
47
|
+
const digits = hex.slice(1);
|
|
48
|
+
const full = digits.length <= 4 ? digits.split('').map((part) => part + part).join('') : digits;
|
|
49
|
+
return {
|
|
50
|
+
r: Number.parseInt(full.slice(0, 2), 16),
|
|
51
|
+
g: Number.parseInt(full.slice(2, 4), 16),
|
|
52
|
+
b: Number.parseInt(full.slice(4, 6), 16),
|
|
53
|
+
a: full.length >= 8 ? Number.parseInt(full.slice(6, 8), 16) / 255 : 1,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Every colour a background value names, gradient stops included. */
|
|
58
|
+
export function colorsIn(background: string): Rgba[] {
|
|
59
|
+
const colors: Rgba[] = [];
|
|
60
|
+
for (const match of background.matchAll(HEX_RE)) colors.push(parseHex(match[0]));
|
|
61
|
+
for (const match of background.matchAll(OKLCH_RE)) {
|
|
62
|
+
const parsed = parseOklchCss(match[0]);
|
|
63
|
+
if (parsed) colors.push({ ...oklchToRgb255(parsed.l, parsed.c, parsed.h), a: alpha(match[1]) });
|
|
64
|
+
}
|
|
65
|
+
for (const match of background.matchAll(RGB_RE)) {
|
|
66
|
+
colors.push({
|
|
67
|
+
r: channel(match[1]),
|
|
68
|
+
g: channel(match[2]),
|
|
69
|
+
b: channel(match[3]),
|
|
70
|
+
a: alpha(match[4]),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
for (const match of background.matchAll(EXOTIC_RE)) {
|
|
74
|
+
const resolved = viaCanvas(match[0]);
|
|
75
|
+
if (resolved) colors.push(resolved);
|
|
76
|
+
}
|
|
77
|
+
return colors;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function linearChannel(value: number): number {
|
|
81
|
+
const srgb = Math.max(0, Math.min(255, value)) / 255;
|
|
82
|
+
return srgb <= 0.04045 ? srgb / 12.92 : ((srgb + 0.055) / 1.055) ** 2.4;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function luminance({ r, g, b }: Rgba): number {
|
|
86
|
+
return 0.2126 * linearChannel(r) + 0.7152 * linearChannel(g) + 0.0722 * linearChannel(b);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const SEED = '#010203';
|
|
90
|
+
let probe: CanvasRenderingContext2D | null | undefined;
|
|
91
|
+
|
|
92
|
+
/** The browser as a colour parser: a 2D canvas resolves anything it renders,
|
|
93
|
+
* including the `color-mix()` a computed style can still hand back, and
|
|
94
|
+
* reports failure rather than resolving an unknown value to black. */
|
|
95
|
+
function viaCanvas(value: string): Rgba | null {
|
|
96
|
+
if (typeof document === 'undefined') return null;
|
|
97
|
+
if (probe === undefined) {
|
|
98
|
+
probe = document.createElement('canvas').getContext?.('2d', { willReadFrequently: true }) ?? null;
|
|
99
|
+
}
|
|
100
|
+
if (!probe) return null;
|
|
101
|
+
probe.fillStyle = SEED;
|
|
102
|
+
probe.fillStyle = value;
|
|
103
|
+
if (probe.fillStyle === SEED) return null;
|
|
104
|
+
probe.clearRect(0, 0, 1, 1);
|
|
105
|
+
probe.fillRect(0, 0, 1, 1);
|
|
106
|
+
const [r, g, b, a] = probe.getImageData(0, 0, 1, 1).data;
|
|
107
|
+
return { r, g, b, a: a / 255 };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Any CSS colour the browser can render, as `#rrggbb` — for consumers that
|
|
111
|
+
* cannot read CSS themselves: a canvas, a WebGL uniform, an SVG attribute.
|
|
112
|
+
* Null for a value that is not a colour, or is see-through enough that a flat
|
|
113
|
+
* hex would lie about it. */
|
|
114
|
+
export function cssColorToHex(value: string): string | null {
|
|
115
|
+
const color = viaCanvas(value.trim());
|
|
116
|
+
if (!color || color.a < OPAQUE) return null;
|
|
117
|
+
const packed = (Math.round(color.r) << 16) | (Math.round(color.g) << 8) | Math.round(color.b);
|
|
118
|
+
return `#${packed.toString(16).padStart(6, '0')}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Mean luminance of a background value, or null when it names no colour or
|
|
122
|
+
* nothing opaque enough to hide what sits behind it. */
|
|
123
|
+
export function backgroundLuminance(background: string): number | null {
|
|
124
|
+
const opaque = colorsIn(background).filter((color) => color.a >= OPAQUE);
|
|
125
|
+
if (!opaque.length) return null;
|
|
126
|
+
return opaque.reduce((total, color) => total + luminance(color), 0) / opaque.length;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Which way a background value leans, or null when it names no opaque colour. */
|
|
130
|
+
export function polarityOfBackground(background: string): Polarity | null {
|
|
131
|
+
const measured = backgroundLuminance(background);
|
|
132
|
+
if (measured === null) return null;
|
|
133
|
+
return measured >= LIGHT_BACKDROP ? 'light' : 'dark';
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Pick the invariant black/white token with the stronger minimum contrast
|
|
138
|
+
* against every colour in a solid or gradient background. Alpha is ignored:
|
|
139
|
+
* the caller passes an authored value, not a computed one.
|
|
140
|
+
*/
|
|
141
|
+
export function contrastTokenForBackground(background: string): ContrastColorToken {
|
|
142
|
+
const colors = colorsIn(background);
|
|
143
|
+
if (colors.length === 0) return '--color-white';
|
|
144
|
+
const luminances = colors.map(luminance);
|
|
145
|
+
const blackMinimum = Math.min(...luminances.map((value) => (value + 0.05) / 0.05));
|
|
146
|
+
const whiteMinimum = Math.min(...luminances.map((value) => 1.05 / (value + 0.05)));
|
|
147
|
+
return blackMinimum >= whiteMinimum ? '--color-black' : '--color-white';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Elements a watcher stamped. Its own stamp is an answer, not a declaration,
|
|
151
|
+
* so the next measurement must not read it back as one. */
|
|
152
|
+
const stamped = new WeakSet<Element>();
|
|
153
|
+
|
|
154
|
+
export function markStamped(el: Element): void {
|
|
155
|
+
stamped.add(el);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function clearStamped(el: Element): void {
|
|
159
|
+
stamped.delete(el);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** The polarity the markup states: the nearest `data-backdrop` on or above the
|
|
163
|
+
* element that a watcher did not write. */
|
|
164
|
+
export function declaredPolarity(el: Element | null): Polarity | null {
|
|
165
|
+
for (let node: Element | null = el; node; node = node.parentElement) {
|
|
166
|
+
if (stamped.has(node)) continue;
|
|
167
|
+
const stated = node.getAttribute?.(BACKDROP_ATTRIBUTE);
|
|
168
|
+
if (stated === 'light' || stated === 'dark') return stated;
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Mean luminance of whatever actually paints behind `el` — the nearest
|
|
174
|
+
* ancestor with an opaque fill, averaged across its gradient stops, falling
|
|
175
|
+
* back to the theme's page canvas. */
|
|
176
|
+
export function backdropLuminance(el: Element | null): number {
|
|
177
|
+
if (typeof document === 'undefined') return 0;
|
|
178
|
+
for (let node: Element | null = el ?? document.body; node; node = node.parentElement) {
|
|
179
|
+
const style = getComputedStyle(node);
|
|
180
|
+
const measured = backgroundLuminance(`${style.backgroundImage} ${style.backgroundColor}`);
|
|
181
|
+
if (measured !== null) return measured;
|
|
182
|
+
}
|
|
183
|
+
const page = getComputedStyle(document.documentElement).getPropertyValue('--page-bg');
|
|
184
|
+
return backgroundLuminance(page) ?? 0;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Which way the backdrop behind `el` leans. A `data-backdrop` on or above it
|
|
188
|
+
* is the answer; otherwise the paint is measured. */
|
|
189
|
+
export function polarityOf(el: Element | null): Polarity {
|
|
190
|
+
return declaredPolarity(el) ?? (backdropLuminance(el) >= LIGHT_BACKDROP ? 'light' : 'dark');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export const isLightBackdrop = (el: Element | null): boolean => polarityOf(el) === 'light';
|
|
194
|
+
|
|
195
|
+
/** The black/white token that reads against `el`'s backdrop. */
|
|
196
|
+
export const contrastTokenFor = (el: Element | null): ContrastColorToken =>
|
|
197
|
+
polarityOf(el) === 'light' ? '--color-black' : '--color-white';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
BACKDROP_ATTRIBUTE,
|
|
3
|
+
LIGHT_BACKDROP,
|
|
4
|
+
backdropLuminance,
|
|
5
|
+
backgroundLuminance,
|
|
6
|
+
contrastTokenFor,
|
|
7
|
+
cssColorToHex,
|
|
8
|
+
contrastTokenForBackground,
|
|
9
|
+
declaredPolarity,
|
|
10
|
+
isLightBackdrop,
|
|
11
|
+
polarityOf,
|
|
12
|
+
polarityOfBackground,
|
|
13
|
+
type ContrastColorToken,
|
|
14
|
+
type Polarity,
|
|
15
|
+
} from './backdrop';
|
|
16
|
+
export { backdrop, syncDocumentBackdrop, watchBackdrop, type BackdropOptions } from './watch';
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping a stamp current.
|
|
3
|
+
*
|
|
4
|
+
* A page's polarity moves when the theme changes, and the editor changes a
|
|
5
|
+
* theme by writing inline custom properties on `:root` — no reload, no class
|
|
6
|
+
* flip. One observer watches for that and tells every live watcher to measure
|
|
7
|
+
* again, so a consumer never writes its own.
|
|
8
|
+
*/
|
|
9
|
+
import {
|
|
10
|
+
BACKDROP_ATTRIBUTE,
|
|
11
|
+
clearStamped,
|
|
12
|
+
markStamped,
|
|
13
|
+
polarityOf,
|
|
14
|
+
polarityOfBackground,
|
|
15
|
+
type Polarity,
|
|
16
|
+
} from './backdrop';
|
|
17
|
+
|
|
18
|
+
export interface BackdropOptions {
|
|
19
|
+
/** Called with each new answer, including the first. */
|
|
20
|
+
onChange?: (polarity: Polarity) => void;
|
|
21
|
+
/** Write `data-backdrop` on the element, so CSS can key on it and
|
|
22
|
+
`light-dark()` inside it resolves the right half. Default true. */
|
|
23
|
+
stamp?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
type Watcher = () => void;
|
|
27
|
+
|
|
28
|
+
const watchers = new Set<Watcher>();
|
|
29
|
+
let themeObserver: MutationObserver | null = null;
|
|
30
|
+
let resizeBound = false;
|
|
31
|
+
|
|
32
|
+
function notify(): void {
|
|
33
|
+
for (const watcher of watchers) watcher();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function startListening(): void {
|
|
37
|
+
if (typeof document === 'undefined') return;
|
|
38
|
+
if (!themeObserver) {
|
|
39
|
+
// The editor writes the live theme as inline vars on <html>; a switch to a
|
|
40
|
+
// different stylesheet lands in <head>.
|
|
41
|
+
themeObserver = new MutationObserver(notify);
|
|
42
|
+
themeObserver.observe(document.documentElement, { attributeFilter: ['style', 'class'] });
|
|
43
|
+
themeObserver.observe(document.head, { childList: true });
|
|
44
|
+
}
|
|
45
|
+
if (!resizeBound) {
|
|
46
|
+
window.addEventListener('resize', notify, { passive: true });
|
|
47
|
+
resizeBound = true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function stopListening(): void {
|
|
52
|
+
if (watchers.size) return;
|
|
53
|
+
themeObserver?.disconnect();
|
|
54
|
+
themeObserver = null;
|
|
55
|
+
if (resizeBound) {
|
|
56
|
+
window.removeEventListener('resize', notify);
|
|
57
|
+
resizeBound = false;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Track the polarity behind `el` until the returned function is called. A
|
|
63
|
+
* `data-backdrop` stated on or above the element wins over measurement, which
|
|
64
|
+
* is what lets a section declare a tone the paint cannot show — art directed
|
|
65
|
+
* over a photograph, or a band whose fill has not rendered yet.
|
|
66
|
+
*/
|
|
67
|
+
export function watchBackdrop(el: Element, options: BackdropOptions = {}): () => void {
|
|
68
|
+
const { onChange, stamp = true } = options;
|
|
69
|
+
let last: Polarity | null = null;
|
|
70
|
+
|
|
71
|
+
const measure = () => {
|
|
72
|
+
const polarity = polarityOf(el);
|
|
73
|
+
if (polarity === last) return;
|
|
74
|
+
last = polarity;
|
|
75
|
+
if (stamp) {
|
|
76
|
+
markStamped(el);
|
|
77
|
+
el.setAttribute(BACKDROP_ATTRIBUTE, polarity);
|
|
78
|
+
}
|
|
79
|
+
onChange?.(polarity);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
watchers.add(measure);
|
|
83
|
+
startListening();
|
|
84
|
+
measure();
|
|
85
|
+
|
|
86
|
+
return () => {
|
|
87
|
+
watchers.delete(measure);
|
|
88
|
+
if (stamp) {
|
|
89
|
+
clearStamped(el);
|
|
90
|
+
el.removeAttribute(BACKDROP_ATTRIBUTE);
|
|
91
|
+
}
|
|
92
|
+
stopListening();
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Svelte action: `<section use:backdrop>` stamps the element and keeps the
|
|
97
|
+
* stamp current. Pass `{ onChange }` when JS needs the answer too. */
|
|
98
|
+
export function backdrop(el: Element, options: BackdropOptions = {}) {
|
|
99
|
+
let stop = watchBackdrop(el, options);
|
|
100
|
+
return {
|
|
101
|
+
update(next: BackdropOptions = {}) {
|
|
102
|
+
stop();
|
|
103
|
+
stop = watchBackdrop(el, next);
|
|
104
|
+
},
|
|
105
|
+
destroy() {
|
|
106
|
+
stop();
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Stamp `<html>` from the theme's page canvas, so a rule can key on
|
|
113
|
+
* `[data-backdrop='dark']` anywhere on the page and a section that declares its
|
|
114
|
+
* own tone still overrides it. Call once at boot; returns a stop function.
|
|
115
|
+
*/
|
|
116
|
+
export function syncDocumentBackdrop(): () => void {
|
|
117
|
+
if (typeof document === 'undefined') return () => {};
|
|
118
|
+
const root = document.documentElement;
|
|
119
|
+
const stated = root.getAttribute(BACKDROP_ATTRIBUTE);
|
|
120
|
+
// A root the page states for itself is the page's answer; leave it alone.
|
|
121
|
+
if (stated === 'light' || stated === 'dark') return () => {};
|
|
122
|
+
const measure = () => {
|
|
123
|
+
const page = getComputedStyle(root).getPropertyValue('--page-bg');
|
|
124
|
+
const polarity = polarityOfBackground(page);
|
|
125
|
+
if (polarity) root.setAttribute(BACKDROP_ATTRIBUTE, polarity);
|
|
126
|
+
};
|
|
127
|
+
// The root's own stamp is an answer, not a declaration — without this every
|
|
128
|
+
// measurement below it would short-circuit on the page's polarity and no
|
|
129
|
+
// local band could ever read as its own.
|
|
130
|
+
markStamped(root);
|
|
131
|
+
watchers.add(measure);
|
|
132
|
+
startListening();
|
|
133
|
+
measure();
|
|
134
|
+
return () => {
|
|
135
|
+
watchers.delete(measure);
|
|
136
|
+
clearStamped(root);
|
|
137
|
+
stopListening();
|
|
138
|
+
};
|
|
139
|
+
}
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
<script lang="ts">
|
|
36
36
|
import MenuSelect from './MenuSelect.svelte';
|
|
37
37
|
import { SvelteMap } from 'svelte/reactivity';
|
|
38
|
-
import { contrastTokenForBackground } from '../
|
|
38
|
+
import { contrastTokenForBackground } from '../backdrop';
|
|
39
39
|
// `.ftt-tag` is hand-rolled (not Badge) so editing badge-* tokens doesn't
|
|
40
40
|
// repaint the playground. The dropdown uses MenuSelect on purpose.
|
|
41
41
|
import './FloatingTokenTags.css';
|
|
@@ -630,7 +630,7 @@
|
|
|
630
630
|
class="image-lightbox-stage"
|
|
631
631
|
class:shadow-box={shadow === 'box'}
|
|
632
632
|
class:shadow-content={shadow === 'content'}
|
|
633
|
-
|
|
633
|
+
role="presentation"
|
|
634
634
|
tabindex="-1"
|
|
635
635
|
onclick={onStageClick}
|
|
636
636
|
onpointerdown={onPointerDown}
|
|
@@ -729,6 +729,7 @@
|
|
|
729
729
|
<style>
|
|
730
730
|
:global(:root) {
|
|
731
731
|
/* thumbnail (closed inline) + animated modal stage */
|
|
732
|
+
--imagelightbox-tile-surface: var(--color-transparent);
|
|
732
733
|
--imagelightbox-tile-radius: var(--radius-2xl);
|
|
733
734
|
--imagelightbox-tile-border: var(--color-transparent);
|
|
734
735
|
--imagelightbox-tile-border-width: var(--border-width-0);
|
|
@@ -759,7 +760,7 @@
|
|
|
759
760
|
cursor: zoom-in;
|
|
760
761
|
border: var(--imagelightbox-tile-border-width) solid var(--imagelightbox-tile-border);
|
|
761
762
|
border-radius: var(--imagelightbox-tile-radius);
|
|
762
|
-
background:
|
|
763
|
+
background: var(--imagelightbox-tile-surface);
|
|
763
764
|
overflow: hidden;
|
|
764
765
|
transition: transform 250ms ease;
|
|
765
766
|
}
|
|
@@ -818,7 +819,7 @@
|
|
|
818
819
|
cursor: zoom-out;
|
|
819
820
|
border: var(--imagelightbox-tile-border-width) solid var(--imagelightbox-tile-border);
|
|
820
821
|
border-radius: var(--imagelightbox-tile-radius);
|
|
821
|
-
background:
|
|
822
|
+
background: var(--imagelightbox-tile-surface);
|
|
822
823
|
}
|
|
823
824
|
|
|
824
825
|
.image-lightbox-clip {
|