日本語

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

theme.rs
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():

FunctionLightDarkWhat reads it
background#ffffff#09090bThe page background
foreground#09090b#fafafaText on background
card#ffffff#18181bThe surface of a card
card_foreground#09090b#fafafaText on card
popover#ffffff#18181bThe surface of a menu, a popover, a listbox, and the open list of a select
popover_foreground#09090b#fafafaText on popover
primary#18181b#e4e4e7A default button, a checked box, an on switch, the slider's fill
primary_foreground#fafafa#18181bText and marks on primary
secondary#f4f4f5#27272aA secondary button
secondary_foreground#18181b#fafafaText on secondary
muted#f4f4f5#27272aA quiet fill, such as the slider's track or a tab list
muted_foreground#6b6b74#a1a1aaA description, a placeholder, secondary text
accent#f4f4f5#3f3f46The fill under the pointer, and a toggle that is on
accent_foreground#18181b#fafafaText on accent
destructive#e7000b#ff6467A destructive button, an error message, an invalid field's border
destructive_foreground#ffffff#09090bText on destructive
border#e4e4e7#ffffff1fThe border of a card, a menu, and a separator
input#8b8b94#70707aThe border of a text field, a checkbox, a radio, a toggle, and an off switch
ring#18181b#d4d4d8The focus outline
chart_1#155dfc#51a2ffThe first series of a chart
chart_2#009689#00d5beThe second series
chart_3#f54900#ff8904The third series
chart_4#9810fa#c27affThe fourth series
chart_5#bb4d00#ffd230The fifth series
radius0.625rem0.625remThe 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:

  • input is dark enough for the 3:1 contrast that a control's boundary needs (1.4.11): 3.3:1 or more on background and on card in both schemes.
  • muted_foreground keeps 4.5:1 on muted as well as on background: 4.8:1 or more on both in both schemes, so a description inside a muted panel stays readable.
  • ring is 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 primary too close to primary_foreground leaves 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 from theme() while the system prefers dark, and the dark scheme shows zinc where the light one shows your color. Set the dark value under Media::Dark as 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::new under one of these names, such as "primary", is the same custom property to the browser, and setting it changes the components too.

Next

Theming switches every token between its light and dark values. Tokens and Themes in the Style docs cover tokens of your own.