Themes
A Theme sets the values of tokens on :root for the whole page, with other values under media conditions such as dark mode or print.
Values for the whole page
A Theme holds values for tokens, and Page::theme, fn theme(self, theme: Theme) -> Self, writes them on :root, so every element of the page reads them in place of the tokens' defaults. A theme is built with three methods:
| Method | What it does |
|---|---|
set<T: TokenValue>(self, token: &Token<T>, value: impl Into<T>) -> Theme | Sets token to value. A later value for the same token under the same conditions replaces an earlier one. |
on(self, media: Media, theme: Theme) -> Theme | Adds the values of theme, applied only under media. |
merge(self, theme: Theme) -> Theme | Adds the values of theme, which win over this theme's own. |
use hypersig::html::p;
use hypersig::style::value::{Color, rem};
use hypersig::style::{Media, Style, Theme, Token};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Blog {}
let ink = Token::new("ink", Color::hex(0x0022_2222));
let paper = Token::new("paper", Color::WHITE);
let gap = Token::new("gap", rem(1));
// A brand's values, which a library's theme can be merged with.
let brand = Theme::new()
.set(&ink, Color::hex(0x0011_1827))
.set(&gap, rem(1.5))
.on(
Media::Dark,
Theme::new().set(&ink, Color::hex(0x00f9_fafb)).set(&paper, Color::hex(0x0011_1827)),
)
.on(Media::Print, Theme::new().set(&ink, Color::BLACK));
let html = Page::new("Blog", Blog {})
.theme(brand)
// A later call adds to the theme, and replaces `--gap` under no condition.
.theme(Theme::new().set(&gap, rem(2)))
.child(
p().css(Style::new().color(ink.var()).background_color(paper.var()).padding(gap.var()))
.text("Hello"),
)
.render();Page::theme merges each theme it is given into the earlier ones, so the second call above replaces --gap and keeps the rest. The page gets this CSS:
:root { --ink: #111827; --gap: 2rem }
@media (prefers-color-scheme: dark) { :root { --ink: #f9fafb; --paper: #111827 } }
@media print { :root { --ink: black } }Which value applies
- The values without a condition come first, then one block per set of media conditions, in the order
Medialists its variants. Where two blocks match and set the same token, the later block wins, so a printed page above gets black ink in dark mode too. - A value set on an element with
Style::tokenorEl::bind_tokenwins over the theme for that element and those inside it, because the element is closer than:root. onnests:on(Media::Dark, Theme::new().on(Media::Print, ..))applies while both hold, written@media print and (prefers-color-scheme: dark).
Only media conditions
A theme sets values for the whole document on :root, which has no container above it, so Theme::on takes a Media and nothing else. A pseudo-class is a compile error:
use hypersig::style::value::Color;
use hypersig::style::{PseudoClass, Theme, Token};
let ink = Token::new("ink", Color::hex(0x0022_2222));
// error[E0308]: mismatched types: expected `Media`, found `PseudoClass`
let theme = Theme::new().on(PseudoClass::Hover, Theme::new().set(&ink, Color::BLACK));What the types leave unchecked
- Nothing checks that a theme sets every token a page reads. A token it leaves out shows its default.
- A plain stylesheet added with
Page::stylesits outside the cascade layers of the theme, so a:rootrule in it that sets the same custom property wins over the theme.
Next
Dark mode uses a theme to follow the system's color scheme, and a style on <body> to let the visitor pick one. Theming covers the theme of hypersig_ui.