Searching Puzzle Pieces

Type to search Puzzle Pieces.

Theming

Every piece styles itself through the same set of semantic tokens — bg-surface, text-ink, border-border, shadow-panel — and never a colour of its own. The package ships four palettes and four styles, sixteen appearances in all, and nothing inside a piece changes when you switch between them: the token names stay put, only their values move.

The model

Two attributes on <html> decide the appearance, and they are independent of each other.

data-scheme
The palette — cool, warm or void. The default palette needs no attribute at all, because the @theme block is its values. Writing the attribute with the value default is still valid, and is how a preview restates the default palette inside a page that is currently in another one.
data-theme
The style — dark, dim, mixed or light. With the attribute absent the page follows the OS between light and dark; dim and mixed are never chosen by the OS, only by a person. (The runtime calls this field mode; the values are the styles.)

Every token is one light-dark() pair, so a style block only has to set color-scheme and both halves resolve for free. Having color-scheme follow the style is also what keeps native UI in step — form controls, scrollbars and dialog backdrops agree with the page, with no duplicate dark-mode class list.

Dim sits between light and dark — lower contrast, more subtle. It runs on color-scheme: dark, so every pair already resolves to its dark half, then lifts the grounds to dark grey of the palette’s own hue and softens the type. Each file’s dim block therefore lists only the tokens whose dim value differs from dark.

Mixed is the darker sidebar with a white main panel. It runs on color-scheme: light, so every pair resolves to its light half, and its block re-declares only the frame: surface-frame and the bar-* roles take their dark values (the rail-* roles alias them). Anything drawn on the frame must use those roles, never the panel’s ink, or it disappears into the dark sidebar.

/* The style sets color-scheme; every token is one light-dark() pair. */
:root                { color-scheme: light dark; }  /* no attribute: the OS decides */
[data-theme='light'],
[data-theme='mixed'] { color-scheme: light; }
[data-theme='dark'],
[data-theme='dim']   { color-scheme: dark; }

@theme {
  --color-surface: light-dark(<light>, <dark>);
  --color-ink:     light-dark(<light>, <dark>);
  /* …every token, one pair… */
}

/* Dim already resolves to the dark half, so its block re-declares only the
   tokens whose dim value differs from dark: grounds lifted, type softened. */
[data-theme='dim'] {
  --color-surface: <lifted dark grey>;
  --color-ink:     <softer ink>;
}

/* Mixed resolves to the light half and re-declares only the frame, dark. */
[data-theme='mixed'] {
  --color-surface-frame: <the dark frame>;
  --color-bar-ink:       <the dark bar ink>;
  /* …bar-muted, bar-hover, bar-line… */
}

/* A palette overrides values under its own attribute — unanchored, so the
   same rules apply on the root and on any scoped subtree. */
[data-scheme='warm'] {
  --color-surface: light-dark(<light>, <dark>);
}

Install

The package exports the four stylesheets and the two runtime files:

  • @magic-spells/puzzle-pieces/themes/default.cssthe default palette and the @theme block — always imported
  • @magic-spells/puzzle-pieces/themes/cool.cssCool — a little more blue, after the Observatory scheme
  • @magic-spells/puzzle-pieces/themes/warm.cssWarm — browns and oranges with a clay accent
  • @magic-spells/puzzle-pieces/themes/void.cssVoid — very black, dark greys, white type
  • @magic-spells/puzzle-pieces/appearancethe runtime: boot, set, current, subscribe
  • @magic-spells/puzzle-pieces/pre-paintthe anti-flash snippet, inlined in the shell head

Always import the default stylesheet. It is registry/theme/pieces.css, it holds the @theme block every utility is generated from, and the other three only override its values. Add a palette file for each palette you want available; a palette is inert until data-scheme selects it, so importing one costs nothing but its bytes.

@import "tailwindcss";

/* Always: the default palette, and the @theme block every utility comes from. */
@import "@magic-spells/puzzle-pieces/themes/default.css";

/* Then a palette per scheme you want available — inert until data-scheme picks it. */
@import "@magic-spells/puzzle-pieces/themes/warm.css";

Copy a palette with puzzle add theme <name>. The default lands in app/styles/pieces.css; the others land in app/styles/themes/. The command records hashes in pieces.lock and prints the import to add, leaving your stylesheet for you to edit. An unmodified older copy is refreshed; a locally edited copy needs a hand merge or --overwrite.

Switching at runtime

The appearance export owns the two attributes; nothing else should write them. Call boot() first thing in app.js to make the module agree with the document, then set({ scheme, mode }) for changes, current() to read the choice in force, and subscribe(fn) to hear every apply — including one caused by another tab. A mode of null means follow the OS. The choice persists under one key, puzzle:appearance, as the JSON object { scheme, mode }; the 0.8 names still read — a stored palette of dim is cool now, and a stored mode of medium is dim.

import * as appearance from '@magic-spells/puzzle-pieces/appearance';

// First thing in app.js — makes the module agree with what pre-paint painted.
appearance.boot();

appearance.set({ scheme: 'warm', mode: 'mixed' });  // palette + style
appearance.set({ mode: null });                     // follow the OS
appearance.current();                               // -> { scheme, mode }

const off = appearance.subscribe(({ scheme, mode }) => update(scheme, mode));
// call off() in destroyed()

One thing has to happen earlier than any module can: painting the stored choice. Inline the whole of pre-paint in the shell’s <head>, before the stylesheet, and configure it with data attributes on the script tag itself: data-key is the storage key to read (an app that owns another key passes it here), data-default-mode the style to paint when nothing is stored — omit it and the page follows the OS — and data-default-scheme the palette for that same case. A module arriving with app.js is a frame too late: the page paints the default first, and that is the flash.

<head>
  <script data-key="puzzle:appearance" data-default-mode="dark">
    /* the whole of registry/theme/pre-paint.js, inlined here */
  </script>

  <link rel="stylesheet" href="/styles.css?v=01beceeb43">
</head>

Scoping a subtree

The palette selectors are deliberately unanchored — not tied to :root — so putting both attributes on any element renders that whole subtree in another palette and style while the page around it keeps its own. That is the entire mechanism behind this demo’s compare grid and scheme panels. Give a scoped preview an explicit ground and ink (bg-surface, text-ink) or it inherits the page’s.

<div data-scheme="void" data-theme="light" class="bg-surface text-ink">
  <!-- this subtree renders Void / light, whatever the page around it is -->
</div>

Tokens

The vocabulary is one contract shared by all four palettes: the same names, in the same groups, in every file. Text and surfaces cover content; the shell group covers the frame, bar, rail, panel and card a whole app window is built from; the alpha family holds the translucent wells, scrims and shadow colours that the composed shadows are built out of.

GroupSizeWhat it covers
Text5 tokensInk ladders, strongest to faintest. Measured on every ground.
Surfaces5 tokensThe stacked grounds content sits on.
Brand5 tokensThe one accent everything keys off.
Status12 tokensDanger, success and warning — solid, tinted, and as text.
Lines & focus4 tokensHairlines, the dashed outline and the focus ring.
Charts8 tokensEight categorical slots in fixed order, never cycled.
Shell18 tokensThe frame, the bar, the rail, the panel and the card.
Alpha family12 tokensWells, scrims and shadow colours — translucent, they tint whatever they land on.
Shadows5 tokensComposed box-shadow values (shadow-panel, shadow-card, …), built from the colours above.

Every token, with its live value and its measured contrast, is on the palette pages:

The rule that holds all of this together: never write a colour in a piece. Reach for the utility the token already generates, and the piece is right in all sixteen appearances without knowing any of them.

Adding a palette

A palette is one file plus five registrations. Copy an existing scheme file, rename its selectors to the new name, and retune the values — keeping every token, because the tests compare token sets across files.

  1. Copy a scheme file in registry/theme/ to the new name and rename its selectors — the palette block and its dim and mixed blocks.
  2. Retune the values. Keep every token name; only values change.
  3. Add { name, file, label, description } to the themes array in registry/registry.json.
  4. Add the name to SCHEMES in registry/theme/appearance.js AND to SCHEMES in registry/theme/pre-paint.js — the two lists have to agree.
  5. Add it to demo/app/lib/tokenNames.js and to test/lib/parse-theme.mjs, so the demo and the tests can see it.

Then run npm test. It enforces that every palette declares an identical token set, and that every palette clears AA in all four styles, so a half-finished palette fails loudly instead of shipping with holes.

Contrast

Every palette in every style passes WCAG 2.2 AA on every pair the token list declares — 4.5:1 for text roles against each ground they are allowed to sit on, 3:1 for non-text roles such as the focus ring and the dashed outline. The bar is part of the contract, not a review step: the tests measure it on each run, and the scheme panels in this demo show the ratios computed live from the stylesheet, so a retuned value is visibly in or out before it is committed.