swatchbook

swatchbook

A tiny <swatch-book> web component that presents design variants one at a time as a carousel, instead of stacking them down the page.

Built for reviewing block and section design options — put two or three candidate heroes side-by-side-in-time and click through them. The whole idea in one line: the script's presence is the switch. Include it and you get a carousel; omit it in production and the book collapses to your chosen default, leaving no trace of the tool.

<swatch-book label="Hero">
  <section data-swatch="Bold">…candidate A…</section>
  <section data-swatch="Minimal" data-default>…candidate B…</section>
  <section data-swatch="Playful">…candidate C…</section>
</swatch-book>

<script type="module" src="https://unpkg.com/swatchbook"></script>

Demo

Three hero candidates in one book. Use the arrows, the dots, or the / keys while focus is inside it. Your pick is remembered across reloads.

Ship faster.

The no-nonsense platform for teams that move.

Get started

Less, but better.

Everything you need. Nothing you don't.

Start free

Let's build something fun.

Bright ideas deserve a bright home.

Jump in

Use it with a coding agent

Generating the candidates is the slow part, so the shortest path is to let your agent write them and wire up the book in one go:

Give me three candidate designs for the pricing section, and wrap them in a
swatchbook so I can flip between them.

For something more deliberate, this template spells out the constraints that otherwise fail silently:

Use swatchbook to show me N candidate designs for [section]. Don't install it —
it's throwaway scaffolding. Load it from the CDN instead, by adding both tags to
whichever page or layout renders [section]:

  <script type="module" src="https://unpkg.com/swatchbook"></script>
  <link rel="stylesheet" href="https://unpkg.com/swatchbook/swatchbook.css" />

- Wrap the candidates in one <swatch-book label="[unique name]">.
- Each candidate is a DIRECT child with data-swatch="[Name]" — no wrapper
  element in between, or the book silently renders them stacked.
- Mark the one we'd ship with data-default.
- Keep the candidates' markup static; the component moves its own children.

Working in a repo where you'll do this often? Drop swatchbook's constraints into your agent's always-on context, so you don't have to restate them:

curl -fL https://unpkg.com/swatchbook/AGENTS.md >> AGENTS.md

Agents reading rather than writing: this page is available as markdown at /index.md and /llms.txt, or use the copy as markdown button up top.

Install

<script type="module" src="https://unpkg.com/swatchbook"></script>
<link rel="stylesheet" href="https://unpkg.com/swatchbook/swatchbook.css" />
pnpm add swatchbook
import "swatchbook";     // registers <swatch-book>
import "swatchbook/css"; // optional script-less fallback

Usage

Wrap your candidates in <swatch-book>. Each direct child with a data-swatch attribute is one swatch, and the attribute's value is its display name.

<swatch-book label="Pricing section">
  <div data-swatch="Cards">…</div>
  <div data-swatch="Table" data-default>…</div>
  <div data-swatch="Slider">…</div>
</swatch-book>

That's it. The component renders a small floating control — previous / next, dots, and a Label · Name · 2 / 3 readout — shows one swatch at a time, and remembers your last pick per label.

The two-file model

FileWhat it doesWhen you need it
swatchbook.js Defines <swatch-book> and turns it into a carousel. Whenever you want the review UI. Its presence is the on/off switch.
swatchbook.css A :not(:defined) fallback that shows only the default swatch when the script is absent. On any page that ships <swatch-book> markup but might load without the script (production, or pre-hydration). Recommended.

To ship a page without the tool, just don't include the script. With the CSS present, the page renders your data-default swatch as a normal, finished design.

Gotchas

When a book can't do its job it goes inert: it leaves your markup alone, so every candidate renders stacked down the page — the exact thing you installed this to avoid, looking for all the world like a CSS bug. Each case below logs a console.warn explaining itself, so check the console if a book does nothing.

Swatches must be direct children

This is the easy one to get wrong:

<!-- inert: the wrapper hides the swatches -->
<swatch-book label="Hero">
  <div class="grid gap-4">
    <section data-swatch="Bold">…</section>
    <section data-swatch="Minimal" data-default>…</section>
  </div>
</swatch-book>

A book needs at least two swatches

With zero or one there's nothing to flip between, so the component leaves your markup untouched.

Children must exist when the element upgrades

The book reads its swatches once, on connect. Swatches rendered in later — behind async data, say — are never picked up.

label must be unique per page

It doubles as the localStorage key, so two books sharing a label overwrite each other's remembered swatch.

Persistence overrides data-default

Once you've clicked through a book, the remembered swatch wins on reload. That's the point during a review, but it does mean data-default looks broken when you're testing it — clear the key first:

localStorage.removeItem("swatchbook:Hero");

Framework notes

<swatch-book> is a plain custom element, so it drops into any stack. Two things are worth knowing before you reach for one inside a component tree.

Keep a book's children static

Only the active swatch stays in the DOM — the component calls .remove() and .append() on its own light-DOM children (see how it works). Those children are exactly the nodes React, Vue, or Svelte believe they own. Static candidates are fine, since the framework never touches them after mount; but anything that re-renders a book's children will fight the component over the same nodes, and can throw when the framework tries to patch a swatch that's currently detached. Design candidates are usually static markup, so this rarely bites — but keep state, lists, and conditionals inside a swatch rather than around it.

TypeScript and JSX

The typings ship inside the package, so a script-tag setup has none, and <swatch-book> won't typecheck in .tsx. If you're writing JSX, add the package for its types and keep loading the runtime from the CDN:

pnpm add -D swatchbook   # types only — the script tag still does the work

That's enough for React 18 and below, Preact, Solid, and Qwik, which all read the global JSX namespace. Astro and Svelte accept hyphenated tags already and need nothing either way. React 19 moved its namespace to React.JSX and no longer reads the global one, so .tsx files there need a one-line opt-in:

// tsconfig.json
{ "compilerOptions": { "types": ["swatchbook/react"] } }

API

<swatch-book> attributes

AttributeDescription
label Optional heading shown in the control, and the localStorage key used to remember the active swatch.

Swatch attributes (on each direct child)

AttributeDescription
data-swatch="Name" Marks the element as a swatch; the value is its display name. Required on each swatch.
data-default Marks the swatch shown first, and the one the fallback CSS keeps. Falls back to the first swatch if omitted.

Interaction

Events

The element fires a swatchchange event whenever the active swatch changes:

document.querySelector("swatch-book").addEventListener("swatchchange", (e) => {
  console.log(e.detail); // { index: 1, name: "Table" }
});

Styling the control

The control lives in a shadow root, so page CSS can't accidentally break it. To restyle it intentionally, target its part or tweak the fade:

swatch-book::part(chrome) {
  /* your own pill styling */
}

swatch-book {
  --sb-fade: 400ms; /* crossfade duration; 0 to disable */
}

How it works

Browser support

Modern evergreen browsers (custom elements v1, shadow DOM, ::part, :has). No IE, and no polyfills shipped.