Theming and branding
The panel's look is set in two layers. The theme (AdminProvider::theme) is fixed in code: colors, corner radius, font, logo, favicon and the default color scheme. Runtime branding (AdminProvider::branding) lets the application change the brand name, logos and colors while it runs, for example from a Settings → Branding page, without a deploy. Use the theme for your product's look; add runtime branding when operators choose the brand.
A RustCMS LMS example: the panel with a blue theme and its own logo.
Quick start
// src/providers/admin_panel_provider.rs
use rustcms::admin::Theme;
AdminProvider::new()
.brand("Acme")
.theme(Theme::new().primary("#2563eb").primary_foreground("#ffffff"))brand(name) names the panel: the sidebar, the sign-in page and the browser tab.
Theme
The LMS example's theme: a primary color for each scheme, a radius, a logo and a favicon.
.theme(
Theme::new()
.primary("#2563eb")
.primary_foreground("#ffffff")
.dark(|dark| dark.primary("#60a5fa").primary_foreground("#0b1220"))
.radius("0.625rem")
.logo(include_bytes!("../../resources/admin/logo.png"), "image/png")
.favicon(include_bytes!("../../resources/admin/favicon.png"), "image/png"),
)Theme method | Effect |
|---|---|
Theme::new() | the panel's defaults |
color(token, "#hex") | sets a token of the light scheme (see below) |
primary(…), primary_foreground(…) | shortcuts for those two tokens |
dark(|dark| dark.primary(…)) | tokens of the dark scheme (Palette::color, primary, primary_foreground); tokens it leaves out keep the panel's dark defaults |
radius("0.625rem") | corner radius, up to 4rem or 64px |
font_family("Inter, system-ui, sans-serif") | installed fonts, best first, at most 200 characters |
logo(bytes, mime), favicon(bytes, mime) | PNG, WebP or ICO up to 512 KiB, checked against their bytes |
color_scheme(ColorScheme::…) | the default scheme: System (default), Light or Dark |
| Token | Used for |
|---|---|
background, foreground | page background and text |
card, popover, muted, selected, overlay | panels; menus and dialogs; quiet backgrounds and hovered rows; selected rows; the dimmed page behind a dialog |
muted-foreground, subtle-foreground | secondary text; the text of disabled controls |
border, input, ring | dividers; borders of inputs, checkboxes and outline buttons; focus rings |
primary, primary-foreground, primary-hover | main buttons, active links, checked boxes and switches, primary badges, the default logo mark |
secondary, secondary-foreground, secondary-hover | secondary buttons |
destructive, destructive-foreground, destructive-hover | delete buttons, errors, danger badges |
success, success-foreground, warning, warning-foreground, info, info-foreground | states: text on the page, and text on a solid background of that color |
destructive-soft, success-soft, warning-soft, info-soft, gray-soft, each with -soft-foreground | badges, notices and callouts: a pale background and its text |
The list is rustcms::admin::custom::TOKENS. Each token has a light and a dark value; the dark ones come from dark(…) only (a light value never shows in dark mode).
Hover and soft colors follow their color. Left out,
primary-hoverisprimarymoved away from the text on it (darker in light mode, lighter in dark mode, so hovering never lowers contrast),destructive-softis a paledestructive, and so on, so a theme that sets onlyprimarygets matching hover colors. Set them to choose your own.popoverfollowscard(a step lighter in dark mode) andsecondary-foregroundfollowsforeground.Contrast. The panel's own colors reach WCAG AA in both modes: 4.5:1 for text, 3:1 for input borders and focus rings. When a theme or a runtime branding sets a color that brings text under 4.5:1 (
primary-foregroundonprimary,muted-foregroundoncard, …), the panel logs a warning naming the pair, the mode and the ratio. The boot never stops for it.Colors are hex (
#rgb,#rgba,#rrggbb,#rrggbbaa).Font names are letters, digits, spaces and dashes. Each name is quoted in the stylesheet; the generic families
serif,sans-serif,monospace,system-ui,ui-sans-serif,ui-serifandui-monospacestay bare. Web fonts are not supported.Anything else stops the boot: no text of yours is copied into CSS as written. SVG logos are not supported.
The static theme has a single logo. A separate logo for dark mode needs runtime branding (
Branding::logo_dark).
Light and dark
ColorScheme::System follows the operating system and its changes. Each user can choose Light, Dark or System at the bottom of the sidebar. The choice is kept in that browser (other tabs follow), and the application's ColorScheme applies until they choose.
A RustCMS LMS example: choosing the color scheme.
A RustCMS demo application: the panel in dark mode.
Where the theme is served
The theme becomes a stylesheet and images served from {admin}/assets/brand/theme.css, …/logo, …/logo-dark (runtime branding only) and …/favicon, each with ?v={hash} and cached for good while v is current. The stylesheet loads before your extension styles. The brand name and logo also appear above the sign-in, password reset and invitation forms.
A RustCMS LMS example: the sign-in page.
CSS variables
Extension stylesheets (custom fields, script pages, React pages) use the theme through CSS variables, so they follow light and dark mode:
| Variable | |
|---|---|
--background, --foreground, --card, --popover, --muted, --muted-foreground, --subtle-foreground, --border, --input, --ring, --selected, --overlay | the tokens above |
--primary, --secondary, --destructive, --success, --warning, --info and their -foreground; --primary-hover, --secondary-hover, --destructive-hover; --*-soft and --*-soft-foreground | the tokens above |
--radius | the theme's radius; --radius-sm, --radius-md, --radius-lg (= --radius) and --radius-xl derive from it |
--font-sans | the theme's font stack |
.acme-card { border: 1px solid var(--border); border-radius: var(--radius-lg); color: var(--muted-foreground); }The panel sets data-scheme="dark" on the root element in dark mode; React pages read it with useColorScheme().
Runtime branding
A BrandingSource returns the brand while the application runs. What it returns goes over brand(…) and the Theme, field by field.
The demo shows the site's name from its settings once it has been changed:
pub struct SiteBranding;
#[async_trait]
impl BrandingSource for SiteBranding {
async fn load(&self, app: &AppState) -> AppResult<Option<Branding>> {
let site = rustcms::settings::get::<SiteSettings>(app).await?;
if site.site_name == SiteSettings::default().site_name {
return Ok(None);
}
// A name the panel refuses (over 64 characters) shows the static brand.
let Ok(branding) = Branding::new().name(&site.site_name) else { return Ok(None) };
Ok(Some(branding.primary("#7c3aed").map_err(AppError::internal)?))
}
}.brand("demo")
// The site's name, once set in Settings → Site (runtime branding).
.branding(crate::admin::branding::SiteBranding)An application with its own branding page calls invalidate after saving, so this instance shows the change at once:
app.service::<rustcms::admin::AdminBranding>().expect("AdminProvider").invalidate();
RDockPanel, a hosting panel built on RustCMS: its own Branding page, built on runtime branding (name, color, logos).
What a Branding sets
Branding method | Rules |
|---|---|
name(…) | 1–64 characters, not blank, no control characters; plain text |
logo(bytes, mime), logo_dark(bytes, mime), favicon(bytes, mime) | PNG, WebP or ICO up to 512 KiB, checked against their bytes; never SVG. logo_dark is shown when the page is dark (else logo) |
color(token, value), primary(value) | the theme's tokens and color rules |
dark(|p| p.try_color(token, value)) | tokens of the dark scheme |
radius(value) | as Theme::radius |
- Fonts and custom CSS are not available at runtime.
- Each setter returns
Result<_, BrandingError>. Validate an operator's input with the same rules first:Branding::check_name,Branding::check_colorandBranding::check_image. - Contrast is not checked.
Cache and failures
- Cache: each instance keeps what
loadreturned for[admin] branding_ttl_secs(default 30, 1–3600), one load at a time.AdminBranding::invalidate()makes the next request on this instance load again; other instances follow within that period (there is no push between instances). A load that overlaps an invalidation is not kept. - Failures:
None, an error, or a load slower than[admin] branding_timeout_ms(default 2000, 1–30000) shows the static brand and theme until the next period; the error is logged without its data. - One branding per page: the panel reads the brand, logos and favicon from the page it loaded with, until a reload. Images and the stylesheet are served by kind: a page from another instance or from before a change gets the current file (with
no-cache; cached for good only whenvmatches) while that kind exists, else a 404.
Recovery
A broken source must not lock operators out of the panel's look:
[admin] branding = "static"(orRUSTCMS_ADMIN__BRANDING=static) ignores the source; a restart applies it. The default is"runtime".rustcms admin:brandingshows the mode and, in runtime mode, loads the branding once and reports whether it is valid. It changes nothing.AdminBranding::is_runtime()(the source is used),has_source()(one is registered, even ifstaticignores it) andprobe(app)(whatadmin:brandingruns) let your own pages show the same state.
branding = "runtime" # or "static"
branding_ttl_secs = 30
branding_timeout_ms = 2000RUSTCMS_ADMIN__BRANDING=staticSecurity notes
- The brand name, logos and colors are public: the sign-in page shows them to anyone.
- Every value reaching CSS is checked (hex colors, lengths, font names); no text of yours is copied into the stylesheet as written, and the panel's Content Security Policy stays as strict as without a theme.
- Images are checked against their bytes; SVG is never served.
Related pages
- Customizing the panel: pages, slots and cells
- Configuration: the
[admin]section - Several instances: the branding cache per instance
- Production checklist