Tokens
The components read their colors and their corner radius from the tokens of the token module, which a theme sets for the page and a style sets for one element.
Result
use hypersig::html::div;
use hypersig::style::Style;
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{
ButtonSize, ButtonVariant, Level, button, card, card_content, card_description, card_header,
card_title, select_field, switch, token,
};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Copy, PartialEq)]
pub(super) enum Scheme {
System,
Light,
Dark,
}
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Preview {
scheme: Scheme,
on: bool,
}
pub(super) fn page() -> Page<Preview> {
let initial = Preview {
scheme: Scheme::System,
on: true,
};
Page::new(
"Preview",
initial,
)
.child(
div()
.css(
stack(rem(1))
.padding(rem(1))
.background_color(token::background().var())
.color(token::foreground().var()),
)
.css_match(
Preview::scheme(),
[
(Scheme::Light, hypersig_ui::light()),
(Scheme::Dark, hypersig_ui::dark()),
],
)
.child(
div()
.css(Style::new().max_width(rem(12)))
.child(select_field(
"Theme",
Preview::scheme(),
[
(
Scheme::System,
"System",
),
(
Scheme::Light,
"Light",
),
(
Scheme::Dark,
"Dark",
),
],
)),
)
.child(
card()
.child(
card_header()
.child(card_title(Level::H2).text("Notifications"))
.child(card_description().text("Choose what reaches your inbox.")),
)
.child(
card_content()
.css(stack(rem(1)))
.child(switch(
"Product updates",
Preview::on(),
))
.child(div().child(
button(ButtonVariant::Default, ButtonSize::Sm).text("Save"),
)),
),
),
)
}A token
Each function of the token module returns a Token: a CSS custom property with a name and a default of a type. The colors are Token<Color> and the radius is Token<Length>:
pub fn primary() -> Token<Color>
pub fn radius() -> Token<Length>
impl<T> Token<T> {
pub fn var(&self) -> T
}var() returns a value of the token's own type, so a Token<Color> goes wherever a color goes and a Token<Length> wherever a length goes. It writes var() with the light value as the fallback, so a page that sets no token still shows the light theme:
background-color: var(--muted, #f4f4f5);
border-radius: var(--radius, 0.625rem);Every token
The names and the light values follow shadcn/ui's zinc theme. The custom property of each is its function's name with _ written as -, such as --muted-foreground for muted_foreground():
| Function | Light | Dark | What reads it |
|---|---|---|---|
background | #ffffff | #09090b | The page background |
foreground | #09090b | #fafafa | Text on background |
card | #ffffff | #18181b | The surface of a card |
card_foreground | #09090b | #fafafa | Text on card |
popover | #ffffff | #18181b | The surface of a menu, a popover, a listbox, and the open list of a select |
popover_foreground | #09090b | #fafafa | Text on popover |
primary | #18181b | #e4e4e7 | A default button, a checked box, an on switch, the slider's fill |
primary_foreground | #fafafa | #18181b | Text and marks on primary |
secondary | #f4f4f5 | #27272a | A secondary button |
secondary_foreground | #18181b | #fafafa | Text on secondary |
muted | #f4f4f5 | #27272a | A quiet fill, such as the slider's track or a tab list |
muted_foreground | #6b6b74 | #a1a1aa | A description, a placeholder, secondary text |
accent | #f4f4f5 | #3f3f46 | The fill under the pointer, and a toggle that is on |
accent_foreground | #18181b | #fafafa | Text on accent |
destructive | #e7000b | #ff6467 | A destructive button, an error message, an invalid field's border |
destructive_foreground | #ffffff | #09090b | Text on destructive |
border | #e4e4e7 | #ffffff1f | The border of a card, a menu, and a separator |
input | #8b8b94 | #70707a | The border of a text field, a checkbox, a radio, a toggle, and an off switch |
ring | #18181b | #d4d4d8 | The focus outline |
chart_1 | #155dfc | #51a2ff | The first series of a chart |
chart_2 | #009689 | #00d5be | The second series |
chart_3 | #f54900 | #ff8904 | The third series |
chart_4 | #9810fa | #c27aff | The fourth series |
chart_5 | #bb4d00 | #ffd230 | The fifth series |
radius | 0.625rem | 0.625rem | The corners of a card, a dialog, a menu, and a button |
Each name links to its rustdoc entry from the token module page, such as token::primary and token::radius.
Where the values differ from shadcn/ui
Three values differ from shadcn/ui's, where shadcn/ui falls short of WCAG 2.2 AA:
inputis dark enough for the 3:1 contrast that a control's boundary needs (1.4.11): 3.3:1 or more onbackgroundand oncardin both schemes.muted_foregroundkeeps 4.5:1 onmutedas well as onbackground: 4.8:1 or more on both in both schemes, so a description inside a muted panel stays readable.ringis drawn as a solid 2px outline rather than a translucent shadow (2.4.7), so forced-colors mode, which drops shadows, still shows focus.
The five chart colors keep 3:1 or more on background, card, and muted in both schemes, the contrast that a graphical object needs.
In the dark scheme, border is white at 12% opacity. It shows as #27272a, the same as muted, on background, and lighter than the fill on muted itself, so an outline badge in a selected table row keeps its border.
Setting a token
Theme::set sets a token for the whole page, and Style::token sets it for one element and those inside it. Both take the token and a value that converts into the token's type:
impl Theme {
pub fn set<T: TokenValue>(self, token: &Token<T>, value: impl Into<T>) -> Self
}
impl Style {
pub fn token<T: TokenValue>(self, token: &Token<T>, value: impl Into<T>) -> Self
}use hypersig::html::div;
use hypersig::style::value::{Color, px, rem};
use hypersig::style::{Media, Style, Theme};
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, button, token};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct App {}
// A blue primary color, lighter in the dark theme, and rounder corners.
let brand = Theme::new()
.set(&token::primary(), Color::hex(0x0025_63eb))
.set(&token::radius(), rem(1))
.on(Media::Dark, Theme::new().set(&token::primary(), Color::hex(0x0060_a5fa)));
// An element of your own that reads the same tokens as the components.
let note = div::<App>()
.css(
Style::new()
.padding(rem(1))
.border_width(px(1))
.border_color(token::border().var())
.border_radius(token::radius().var())
.background_color(token::muted().var())
.color(token::muted_foreground().var()),
)
.text("Drafts are kept for 30 days.");
let html = Page::new("App", App {})
.global(hypersig_ui::global())
.theme(hypersig_ui::theme())
// Added after `theme()`, so its dark `primary` replaces the one from `theme()`.
.theme(brand)
.child(note)
.child(
button::<App>(ButtonVariant::Default, ButtonSize::Default)
// A green primary for this button and what is inside it.
.css(Style::new().token(&token::primary(), Color::hex(0x0016_a34a)))
.text("Save"),
)
.render();The brand theme writes --primary and --radius on :root, and the button's style writes --primary on the button alone:
:root { --primary: #2563eb; --radius: 1rem }
@media (prefers-color-scheme: dark) { :root { --primary: #60a5fa } }
/* on the button */
--primary: #16a34a;The value converts into the token's type, so a color given to radius(), a Token<Length>, has no conversion and fails to compile with E0277:
use hypersig::style::Theme;
use hypersig::style::value::Color;
use hypersig_ui::token;
// The radius is a length, and a color is given.
let theme = Theme::new().set(&token::radius(), Color::hex(0x0025_63eb));What the types leave unchecked
- Nothing checks the contrast of a value you set. A
primarytoo close toprimary_foregroundleaves a button's text unreadable, so set the pair together. - The page writes a theme's values without a condition before those under
Media::Dark. A color set only without a condition therefore gives way to the dark value fromtheme()while the system prefers dark, and the dark scheme shows zinc where the light one shows your color. Set the dark value underMedia::Darkas well, as the example does. For one token under the same condition, the theme added later replaces the earlier value. - A token of your own made with
Token::newunder one of these names, such as"primary", is the same custom property to the browser, and setting it changes the components too.