Three attributes set color mode, corner shape and density on any element and everything inside it.
Color mode
Set data-theme="light" or data-theme="dark" on any element. With no attribute on the page, dark follows the system setting.
<html data-theme="dark">With next-themes, use the attribute mode:
<ThemeProvider attribute="data-theme" defaultTheme="system" enableSystem>Corner shape
Set data-shape to rounded, soft or square. Every corner comes from one scale, so one attribute recuts all of them. Dots, such as a radio's indicator, stay round in every shape, because their roundness carries meaning.
Example
Example
rounded
soft
square
'use client';
import { Button, Card, Eyebrow } from '@snapshot-ui/react';
import type { ReactElement } from 'react';
const SHAPES = ['rounded', 'soft', 'square'] as const;
export default function ThemeShapes(): ReactElement {
return (
<div className="shape-samples">
{SHAPES.map((shape) => (
<div key={shape} className="shape-sample" data-shape={shape}>
<Eyebrow>{shape}</Eyebrow>
<div className="row">
<Button>Book a visit</Button>
</div>
<Card style={{ padding: 20 }}>A card, cut {shape}.</Card>
</div>
))}
</div>
);
}Density
Set data-density="compact" on any element. Controls and fields inside it are 36px tall (44px on a touch screen), and cards pad 20px. Type, colors, corners and every other space stay the same. data-density="comfortable" inside a compact element restores the defaults, and the nearest attribute wins.
Example
Example
Comfortable
Profile
How the rest of the team sees you.
Compact
Profile
How the rest of the team sees you.
import {
Button,
Card,
CardBody,
CardHeader,
CardTitle,
Eyebrow,
IconButton,
SegmentedControl,
SegmentedControlItem,
TextField,
} from '@snapshot-ui/react';
import { Search } from 'lucide-react';
import type { ReactElement } from 'react';
function Controls(): ReactElement {
return (
<>
<div className="row">
<Button>Save changes</Button>
<IconButton aria-label="Search">
<Search />
</IconButton>
</div>
<SegmentedControl aria-label="Period" defaultValue="month">
<SegmentedControlItem value="week">Week</SegmentedControlItem>
<SegmentedControlItem value="month">Month</SegmentedControlItem>
</SegmentedControl>
<TextField aria-label="Workspace name" defaultValue="Northwind Studio" />
<Card>
<CardHeader>
<CardTitle level={3}>Profile</CardTitle>
</CardHeader>
<CardBody>
<p>How the rest of the team sees you.</p>
</CardBody>
</Card>
</>
);
}
export default function DensityControls(): ReactElement {
return (
<div className="density-samples">
<div className="density-sample">
<Eyebrow>Comfortable</Eyebrow>
<Controls />
</div>
<div className="density-sample" data-density="compact">
<Eyebrow>Compact</Eyebrow>
<Controls />
</div>
</div>
);
}- Touch: compact controls are 44px, taller than comfortable ones, because 44px is the target a finger needs. Comfortable controls are 40px on every screen.
- Segmented control: the one exception. Its track is 44px, and each option is 36px, hit at 40.
Compact on phones is your site's rule, not an attribute value, because an attribute cannot know the screen width. The tokens are public, so set them in a media query:
@media (width < 768px) {
:root {
--spacing-card: 20px;
}
}This site does that for its cards. To make every control compact on a phone, set --size-control and --size-field the same way.
A popup keeps the density of what opened it, as it keeps the color mode and the corner shape. See Overlays.
Nesting
All three attributes, data-theme, data-shape and data-density, work on any element, and the nearest one wins. A light card inside a dark page takes its own colors and text color.
<main data-theme="dark">
<section data-theme="light" data-shape="square">...</section>
</main>Overlays
A dialog renders at the end of the page, so it cannot inherit attributes from the element it opened from. Instead it copies data-theme, data-shape and data-density from that element's nearest ancestors that carry them. A dialog opened from a button in a dark card is dark while the page stays light. Every other overlay in the library does the same.
- The toast region follows the page root.
- An overlay opened by a keyboard shortcut copies from whatever holds focus. With nothing focused, it takes the page's theme.
- A dialog that moves focus into a field as it opens still copies from what held focus before.
For an overlay the copy cannot reach, such as one a third-party component opens, use React Aria's UNSAFE_PortalProvider from the react-aria package. It renders every overlay inside it into a container you choose. Put the container inside the scope:
import { UNSAFE_PortalProvider } from 'react-aria';
const [container, setContainer] = useState<HTMLElement | null>(null);
<section data-theme="dark" ref={setContainer}>
<UNSAFE_PortalProvider getContainer={() => container}>{children}</UNSAFE_PortalProvider>
</section>;Four variables set the stacking order. If your app's chrome uses larger numbers, set the overlay variables higher.
--z-toast(1100): the toast region, above everything.--z-modal(1000): modal surfaces and their scrims.--z-chrome(30): fixed chrome, such as the rail and the tab bar.--z-header(20): a sticky page header.
Chart palette
Four colors for a chart whose series are peers with no meaning of their own, such as a stack of spending categories: teal, violet, olive and magenta, in that order. Each has a value chosen for its mode, not an inverted one.
--color-chart-1, teal:#029393in light and in dark.--color-chart-2, violet:#7024abin light,#af6af2in dark.--color-chart-3, olive:#697805in light,#7a8a06in dark.--color-chart-4, magenta:#910080in light,#c344aein dark.
In Tailwind they are bg-chart-1 to bg-chart-4. A test in the library measures them on every change:
- Seen: each is at least 3:1 against a card, the page and a floating panel in both modes, so a bar needs no outline. The lowest is teal on the page in light, 3.31.
- Told apart: neighbours in the order are at least 8 apart under simulated protanopia and deuteranopia and 15 for full color vision (OKLab distance times 100). The closest pair is teal and violet in dark, 13.8 for a color-blind reader.
- Never a meaning: each is at least 10 from error, success, gain, warning, clay, both signal oranges, link blue and taupe, and 30 degrees of hue from each of them that has a hue, so a series is never read as a state.
Rules for using them:
- Peers only. A series with a meaning keeps its meaning's color: money that arrived is
gain, and one series is ink. Never use the palette for a state. - Never text. Values, labels and legend names stay ink and slate; the colored mark beside them says which series it is.
- In order, and the order follows the series: pin a series to its color, so it keeps it when a filter removes another.
- Four at most. Sum the rest into an "Other" that you label, drawn in slate, which reads as the remainder rather than a fifth color.
- Neighbours only. The order is checked on adjacent pairs, the ones a stack or a group puts side by side. Where any two series can meet, such as a scatter plot, use three at most.
Tokens
Every value is a CSS variable you can read, such as var(--color-ink), var(--radius-control) or var(--spacing-peer). The same tokens are available in JavaScript, with each mode's value:
import { tokens } from '@snapshot-ui/core/tokens';
tokens.color.slate.var; // 'var(--color-slate)'
tokens.radius.pill.modes.shape.soft; // '12px'Beyond the original set, the tokens include:
--color-warning: a state that needs a look soon, with its own rule of use.--color-gain: money that arrived, with its own rule of use.--color-chart-1to--color-chart-4: the chart palette, described in Chart palette.--text-numeraland--text-numeral-sm: type steps for figures.--size-control,--size-fieldand--size-target: control sizes.--spacing-card: card padding.--spacing-inset: how far floating chrome sits from the viewport's edge.--container-sidebar: the rail's width.--container-page: the page column's width, 82.5rem with its padding, which a page header's row and aPageColumnshare. In Tailwind it ismax-w-page, ormax-w-(--container-page);p-pageis still the 48px page margin.--z-modal,--z-toast,--z-chromeand--z-header: the stacking order, described in Overlays.