# Theming and design tokens
A look in RemedyUI comes from two things:
- a [`Theme`](crate::style::Theme): typed design tokens (color roles, the type scale, spacing,
radii, motion, density, stroke and font families);
- a [`StyleSheet`](crate::style::StyleSheet): rules that turn tokens into styles for each
widget name, variant and state.
A style comes from exactly three places, in order: the widget's defaults, the sheet's rules
for that widget, variant and states, and properties set on the element itself. There is no
selector cascade, so what an element looks like never depends on where it sits. Resolved
styles are cached, and a theme change restyles only what it has to.
Every Remedy app shares one theme set ([`themes`](crate::themes), feature `widgets`), so a
person's theme follows them from app to app.
## The tokens
```
use remedy_ui::style::Theme;
use remedy_ui::themes;
let theme: Theme = themes::ThemeChoice::default().resolve(None, false);
assert_eq!(theme.name, "Dark Forest");
let c = &theme.colors;
let _surfaces = (c.bg, c.panel, c.card);
let _text = (c.text, c.muted);
let _roles = (c.accent, c.on_accent, c.danger, c.warn, c.ok);
// Spacing is a grid unit; `s(n)` is n units.
assert_eq!(theme.spacing.s(3), theme.spacing.unit * 3.0);
let _sizes = (theme.type_scale.small, theme.type_scale.body, theme.type_scale.title);
let _corners = (theme.radii.small, theme.radii.medium, theme.radii.large);
```
Some colors are worked out from the rest by [`Theme::derive`](crate::style::Theme::derive):
hover, pressed and selected fills, the focus ring, and text colors balanced so they meet WCAG
contrast on the background. Widgets read these instead of mixing colors themselves, which is
why every widget in every Remedy palette passes contrast checks.
```
use remedy_ui::themes;
for theme in themes::all() {
let d = theme.derive();
assert!(d.text.contrast(theme.colors.bg) >= 7.0, "{}", theme.name);
assert!(d.muted.contrast(theme.colors.bg) >= 4.5, "{}", theme.name);
}
```
## The Remedy palettes
[`themes::PALETTES`](crate::themes::PALETTES) holds the twelve Remedy palettes, Dark Forest
first, including variants for deuteranopia, protanopia and tritanopia. People choose one with
a [`ThemeChoice`](crate::themes::ThemeChoice): a palette by id, or `System`, which follows
the platform between the Dark and Light palettes.
[`themes::high_contrast`](crate::themes::high_contrast) gives any theme's high-contrast
form, for the system setting.
```
use remedy_ui::themes::{self, ThemeChoice};
assert_eq!(themes::PALETTES.len(), 12);
let system = ThemeChoice::System;
let light = system.resolve(Some(false), false);
let dark = system.resolve(Some(true), false);
assert!(!light.derive().dark && dark.derive().dark);
let high = ThemeChoice::Palette("forest".into()).resolve(None, true);
assert!(high.name.ends_with("(high contrast)"));
```
An app starts styled with the first palette and the widget sheet
([`AppConfig::new`](crate::AppConfig::new)). To follow the system, listen with
[`App::on_appearance`](crate::App::on_appearance) and apply the theme with
[`App::set_theme`](crate::App::set_theme), which restyles every window:
```
# use remedy_ui::AppConfig;
# use remedy_ui::testing::TestApp;
use remedy_ui::reactive::Signal;
use remedy_ui::themes::ThemeChoice;
use remedy_ui::{App, WindowSpec};
let choice = Signal::new(ThemeChoice::System);
app.on_appearance(move |prefers_dark, high_contrast| {
a.set_theme(choice.get_untracked().unwrap_or_default().resolve(prefers_dark, high_contrast));
});
app.open(WindowSpec::new("Themed"), |_, _| {});
};
# let mut test = TestApp::new(AppConfig::new("Themed"), app);
# let Some(ui) = test.ui() else { unreachable!("the window is open") };
# assert!(!ui.theme().name.is_empty());
```
## Typed tokens
Tokens name a place in the design system instead of a number, so autocomplete shows the
design system and a typo fails to compile. They resolve through the window's theme, and again
whenever the theme changes:
| [`Space`](crate::style::Space) | Steps of the spacing unit, for gaps, padding and sizes: half steps `S0_5` … `S3_5` inside controls, `S0` … `S8` between them, `S10` … `S24` for sections and margins (the design's spacing scale) |
| [`Radius`](crate::style::Radius) (`R0` to `R4`) | Corner radii |
| [`TextRole`](crate::style::TextRole) | Text sizes and weights: `Caption`, `Body`, `BodyStrong`, `Heading`, `Title`, `Display` |
| [`ColorRole`](crate::style::ColorRole) | Color roles: `Bg`, `Panel`, `Card`, `Text`, `Muted`, `Accent`, `Danger`, ... and the theme's custom colors |
`gap`, `width`, `height` and `padding` take a [`Dp`](crate::style::Dp) (a token or plain
pixels) or a [`Pad`](crate::style::Pad); [`El::style`](crate::El::style) takes a
[`Style`](crate::style::Style), a style patch whose values may be tokens:
```
use remedy_ui::layout::Align;
use remedy_ui::style::{ColorRole, Pad, Radius, Space, States, Style, TextRole};
use remedy_ui::testing::TestApp;
use remedy_ui::widgets as w;
use std::cell::OnceCell;
use std::rc::Rc;
let slot = Rc::new(OnceCell::new());
let s = slot.clone();
let s = save.clone();
let mut app = TestApp::window(move |ui, _| {
let button = w::button(ui, "Save", || {}).variant("primary");
let _ = s.set(button.clone());
ui.root().child(w::row(ui).gap(8.0).child(w::button(ui, "Cancel", || {})).child(button));
});
let Some(ui) = app.ui() else { unreachable!("the window is open") };
let Some(style) = save.get().and_then(|b| b.style_in(States::NONE)) else { unreachable!("styled") };
assert_eq!(style.background, ui.theme().colors.accent, "primary buttons are filled with the accent");
```
[`El::style_in`](crate::El::style_in) gives the style an element would have in any states,
which is how tests check that focus, hover and selection are visible.
## Your own rules
An app adds its own widget names and rules on top of the widget sheet. The sheet is a
function of the theme, so it is rebuilt from the new tokens whenever the theme changes, and
rules written in tokens follow every palette. Later rules win over earlier ones, and rules
that need more states win over rules that need fewer.
```
# use remedy_ui::testing::TestApp;
use remedy_ui::core::Insets;
use remedy_ui::style::{States, StylePatch, StyleSheet, Theme};
use remedy_ui::{App, AppConfig, WindowSpec, themes};
use std::cell::OnceCell;
use std::rc::Rc;
/// The widget sheet plus the app's `tag` widget, in tokens only.
fn sheet(t: &Theme) -> StyleSheet {
let mut s = themes::sheet(t);
let d = t.derive();
let tag = StylePatch::new()
.background(t.colors.card)
.foreground(d.muted)
.radius(t.radii.small)
.padding(Insets::symmetric(t.spacing.s(1), t.spacing.s(2)));
s.rule("tag", None, States::NONE, tag);
s.rule("tag", Some("urgent"), States::NONE, StylePatch::new().background(t.colors.danger));
s.rule("tag", None, States::HOVER, StylePatch::new().background(d.hover));
s
}
let theme = themes::ThemeChoice::default().resolve(None, false);
let config = AppConfig::new("Tags").theme(theme, sheet);
let tag = Rc::new(OnceCell::new());
let t = tag.clone();
let urgent = ui.el("tag").variant("urgent").text("Overdue");
let _ = t.set(urgent.clone());
ui.root().child(ui.el("tag").text("Later")).child(urgent);
});
};
# let mut test = TestApp::new(config, app);
# let Some(ui) = test.ui() else { unreachable!("the window is open") };
let Some(urgent) = tag.get() else { unreachable!("built") };
let danger = ui.theme().colors.danger;
let hover = ui.theme().derive().hover;
assert_eq!(urgent.style_in(States::HOVER).map(|s| s.background), Some(hover));
```
The states a rule can ask for are the flags of [`States`](crate::style::States): `HOVER`,
`PRESSED`, `FOCUS`, `FOCUS_VISIBLE`, `DISABLED`, `SELECTED`, `CHECKED`, `INVALID`,
`DRAGGING` and `DROP_TARGET`. The view sets them; app code sets its own with
[`El::state`](crate::El::state).
## One element's own style
[`El::style`](crate::El::style) sets properties on one element, over whatever the sheet
says; given a closure, it follows signals. Use it for what belongs to
the content, not the look: a user's chosen note color, a heading made larger in one place.
Name values by their token, not as constants, so the element still fits when the theme
changes:
```
use remedy_ui::testing::TestApp;
use remedy_ui::reactive::Signal;
use remedy_ui::style::{ColorRole, States, Style};
use remedy_ui::widgets as w;
use std::cell::OnceCell;
use std::rc::Rc;
let warning = Signal::new(false);
let slot = Rc::new(OnceCell::new());
let s = slot.clone();