--- page: https://salty-css.dev/concepts/theming index: https://salty-css.dev/llms.txt author: [Teemu Lahjalahti](https://www.linkedin.com/in/teemulahjalahti/) lastUpdated: 2026-10-01 breadcrumbs: [Concepts](https://salty-css.dev/concepts) / [Theming](https://salty-css.dev/concepts/theming) --- # Theming ~ Structuring a design system into brand-driven CSS tokens ![Photograph of waves creating different colors and textures with the light and the rocks.](https://salty-css.dev/_astro/theming.BX_Rwaqi.jpg) Well done theming is not just about implementing the correct colors into light and dark modes. It's about structuring your design system into brand-driven options and building layers that fit different use cases. --- "Theming" is a versatile word — it carries different meanings depending on who’s using it, and most of them are fair. When some libraries and developers say it, they might mean a whole range of things: the entire design system, or simply how tokens get used across it. A good chunk of the time, though, what’s actually being described is the layer I’d call variables — CSS custom properties. (I’ll reach for "tokens" myself now and then, but I lean on the closer technical term.) That layer is real and useful, but on its own it isn’t theming the way I think about it; it’s the raw material theming is built *from*. (Variables have their own page: [Variables](/react/docs/basics/variables).) When I talk about theming, I’m almost always talking about color — specifically, color combinations. It shows up in two related forms depending on the context: a design idea and a technical structure. Really two views of the same thing. ## Theming as a design idea At its simplest, a theme is the light and dark versions of your components. (Automatic light/dark — the kind that follows the operating system’s preference — is *related* to theming, but it isn’t a one-to-one match for it; that distinction matters technically, and I come back to it below.) It goes further than light and dark, though. Theming is also how you bring a brand’s own colors into a combination. Instead of a very light grey background with black text, a theme might be a blue or green background with text chosen to keep enough contrast that it meets whatever accessibility standard the brand or client holds the project to. The theme is the *scheme* — the deliberate pairing of surfaces and the colors that have to stay legible on top of them. Worth naming what’s usually *not* in scope: type. A theme might change font families or other visual details, but most of the time it doesn’t — plenty of sites keep their fonts *theme-agnostic* and swap only colors. By theme-agnostic I mean a value that stays the same no matter which theme is active: it doesn’t take part in the switch at all. I mention it so you don’t over-build the idea into something heavier than it needs to be. ## Theming in the technical sense Technically, theming is a mapping. You take the colors actually in use — which are themselves best treated as theme-agnostic — and map them onto a new mental layer that’s about *usage*, not *value*. The colors in use are best defined as static: the literal light grey, the black, the blue, the green, each pinned to a fixed token that never moves. Theming then puts those static colors to work in different contexts, switched from a single root "switch." Concretely, you define usage-contextual variables — `theme-bg-color`, `theme-text-color`, even `button-primary-hover-color` — and have each of them resolve to one of the static brand tokens, or a variation of one (whether as another variable or a hard-typed hex). In Salty, that mapping is what the `conditional` scope is for: the token *name* stays the same while its *value* flips based on an ancestor selector. ## Atoms and molecules The way I think about it: the static brand colors are atoms, and the themed values are molecules. The atoms are the fixed palette — they don’t move. The molecules are the contextual, named theme values that combine atoms into something usable in a specific role. You build molecules out of atoms; you don’t reach for an atom directly when you’re styling a component. Atoms first — the palette, defined as static variables: ```ts import { defineVariables } from "@salty-css/core/factories"; export default defineVariables({ colors: { grey: { light: "#f0f0f0" }, black: "#0a0a0a", brand: { blue: "#0070f3", green: "#1f9d55", }, }, }); ``` Molecules next — contextual roles that point at the atoms and flip by context: ```ts import { defineVariables } from "@salty-css/core/factories"; export const themes = defineVariables({ conditional: { theme: { light: { bg: "{colors.grey.light}", text: "{colors.black}", buttonPrimaryHover: "{colors.brand.blue}", }, brand: { bg: "{colors.brand.green}", text: "{colors.grey.light}", buttonPrimaryHover: "{colors.brand.blue}", }, }, }, }); ``` Same token names in both schemes, different atoms behind them. That’s the whole trick. ## Building with the molecular layer Once the molecules exist, that’s the layer you build components against. A component should read `{theme.bg}` and `{theme.text}` — never the raw `{colors.brand.green}`. That single indirection is what lets the same component turn into a different color scheme without being touched, either by manual selection ([a toggle that sets an attribute](/react/recipes/theme-toggle)) or by an automatic light/dark snippet. ```tsx import { styled } from "@salty-css/react/styled"; export const Section = styled("section", { base: { background: "{theme.bg}", color: "{theme.text}", }, }); ``` Flip the scheme by setting the switch on an ancestor — and because it’s just markup, schemes nest: ```html
...
``` ### conditional vs responsive One distinction is worth getting right early, because `conditional` and `responsive` are not interchangeable. Which one you want depends on whether the switch is manual or automatic. - `conditional` assumes a data attribute (or class). The value flips when an ancestor selector matches — exactly what you want for a user-driven toggle, or for markup that deliberately sets a scheme. - `responsive` expects a media query. The value swaps when the query matches, with no attribute and no JavaScript in the loop at all. So for genuinely automatic light/dark — the kind that just follows the operating system’s `prefers-color-scheme` — reach for `responsive` keyed to that media query rather than `conditional`. The shape mirrors what you already saw, with a media-query key instead of a selector group: ```ts // auto light/dark — no attribute, no JS export default defineVariables({ responsive: { base: { bg: "{colors.grey.light}", text: "{colors.black}" }, "@darkScheme": { bg: "{colors.black}", text: "{colors.grey.light}" }, }, }); ``` (`@darkScheme` here is a media query you’d register with [`defineMediaQuery`](/react/docs/basics/breakpoints) — the `responsive` keys are names you define, not magic strings.) The rule of thumb: `conditional` is for "the user or the markup chose this"; `responsive` is for "the environment decided." For the more involved cases you can combine the two — an automatic default that a manual toggle is still able to override. That gets genuinely fiddly, so I’ll leave it un-exampled here rather than pretend it’s simple. ## How Salty resolves this — the technical story Everything above is authored in TypeScript and compiled away. When the compiler processes your config, the themes are emitted as native CSS custom properties and written to the global stylesheet — so your tokens are available before any component renders, and everything else in your styles is authored on the assumption that they’re already there. Because resolution lives entirely in the browser’s CSS engine, flipping a theme is just updating an attribute: ```ts document.documentElement.dataset.theme = "brand"; ``` This triggers zero framework re-renders. The attribute changes, and the browser repaints the affected elements through the native variable cascade. (To be precise: mutating the attribute still triggers the browser’s normal style recalculation for the affected elements — that’s unavoidable and cheap.) ### The provider-less part This is where the React-specific side of the story comes in. It’s a consequence of the model, not the headline — but it’s a consequence people feel sharply, so it’s worth spelling out. If a styling library needs a `` at the root of the app, that boundary has to be a client component. And because Server Components can’t consume React Context, theming that way tends to drag a `"use client"` directive across large portions of an app, pushing rendering work back to the browser and giving up much of what a server-first architecture buys you. Context-based theming also asks React to track state: toggling light to dark updates the provider, which cascades a re-render across every styled component in the tree. Salty doesn’t have a provider to place, so there’s nothing forcing that boundary. A theme is an attribute plus some CSS variables, and Server Components are perfectly happy with both. This reflects how Salty likes to work in general: when the platform can solve a problem natively, take a step back from the JavaScript state machinery and let it. ### Framework-agnostic by the same mechanism Because the switch is an attribute on `` and the variables cascade down the DOM, the model doesn’t care which framework rendered a given node — which is a real help for Astro’s islands architecture. A page might be static HTML plus a React island plus a Vue island; with a React-specific provider, your tokens can’t easily bridge to the Vue components or the raw `.astro` markup. With Salty, the `data-theme` attribute sits on `` and the custom properties cascade through every island, so a React component and a plain Astro component can both reference `var(--theme-bg)` and get the right value at the same time, with no framework-specific bridge. And if a component is purely static, its styles resolve during SSR and the client runtime stays out of the browser payload entirely. ## Preventing the flash (FOUC) Because themes resolve from an HTML attribute, there are really two things to get right before the browser’s first paint. The first is the attribute itself: the correct `data-theme` has to be present before paint, or the page renders in the wrong scheme for a frame. The upside of the attribute approach is that it sidesteps the classic CSS-in-JS flash — the swap is native, so you’re not waiting on JavaScript to run and re-style the tree. Getting the attribute there in time comes down to your setup; three ways, depending on it. 1. **Zero-JS (pure CSS).** If you don’t need a user-facing toggle and just want to respect the OS preference, skip attributes entirely and use `responsive` tokens keyed to `prefers-color-scheme`. The browser resolves the preference itself before first paint, so there’s no flash to prevent and no JavaScript runs. 2. **Server-side (best practice for a toggle).** Store the preference in a cookie or database. Because cookies ride along with the initial request, your framework can read it and write the right `data-theme` into the HTML before it leaves the server — the correct scheme is in the markup from the first byte. 3. **Pre-hydration inline script.** For a statically generated site or a pure SPA with no SSR, a small synchronous `