--- author: [Teemu Lahjalahti](https://www.linkedin.com/in/teemulahjalahti/) index: https://salty-css.dev/llms.txt --- # Class Names `className()` takes the same style object `styled()` takes and hands back a class string instead of a component. Variants, nesting, pseudo-classes, tokens, media queries, templates — the whole styling surface, compiled into the same static stylesheet while your project builds. What you don't get is the wrapper: nothing is rendered for you, and putting the class on an element is your job. That's the right trade in two situations. The first is inside an app you're already building with `styled`, when a piece of markup isn't yours — a third-party component that wants class names for its internal parts, or something complex enough that handing it a string is less ceremony than wrapping it. The second is when there's no component in the picture at all: a script that renders its own DOM, an element that only exists after some widget initializes, or a framework Salty doesn't publish a `styled` for. The file rules are the same as everywhere else in Salty. The definition lives in a file ending in `.css.ts` (or one of the other recognized suffixes), it has to be exported, and the compiler reads it at build time. A plain `.ts` file with identical contents compiles to nothing. ## Deconstructing className parameters Every `className(...)` is one call with one options object. Here's a button with base styles, two variant axes, and a stable class name alongside the generated hash: ```ts import { className } from "@salty-css/astro/class-name"; export const buttonClass = className({ // Optional. A class of your choosing, appended next to the generated hash — // a stable hook for DevTools, tests, or an analytics selector. className: "btn", // Styles every use of the class gets. Full CSS-in-JS: pseudos, nesting with &, // media queries, tokens, templates. base: { display: "inline-flex", alignItems: "center", gap: "0.5em", padding: "0.6em 1.2em", borderRadius: "6px", border: "1px solid transparent", cursor: "pointer", "&:hover": { filter: "brightness(1.1)" }, "&:disabled": { opacity: 0.5, pointerEvents: "none" }, }, // Named branches. Each one is opted into at the call site, not via a prop. variants: { tone: { solid: { background: "{colors.brand.main}", color: "{colors.paper}" }, ghost: { background: "transparent", borderColor: "currentColor" }, }, size: { small: { fontSize: "0.85rem" }, large: { fontSize: "1.15rem" }, }, }, }); ``` Using it is a matter of reading the export: ```ts import { buttonClass } from "./styles/button.css"; buttonClass; // → "eyjPN btn" buttonClass.variant("tone", "solid").variant("size", "large"); // → "eyjPN btn tone-solid size-large" ``` `.variant()` returns a **new** instance with `"-"` appended, so it chains and the original is never mutated. Boolean variants work the same way, with the value passed as a string: `buttonClass.variant("warning", "true")`. Two things about that value are worth knowing before they surprise you. **It's a `String` object, not a string primitive** — that's how it carries `.variant()` around with it. Almost everywhere this is invisible: it concatenates, interpolates, and compares as text like anything else. But if something in your stack does a strict `typeof x === "string"` check, coerce it first with `String(...)` or a template literal. **`defaultVariants` is accepted, but `className` doesn't apply it for you.** `.variant()` is the only thing that appends classes, so a bare call gets you the base class and nothing else. When you want defaults, wrap the class in a small function: ```ts export const button = ({ tone = "solid", size = "small", }: { tone?: "solid" | "ghost"; size?: "small" | "large"; } = {}) => buttonClass.variant("tone", tone).variant("size", size); ``` ```ts button(); // → base + tone-solid + size-small button({ tone: "ghost" }); // → base + tone-ghost + size-small ``` The remaining options — `compoundVariants`, `anyOfVariants`, `priority`, `displayName` — behave exactly as they do on `styled` and are listed on the [`className` reference page](/astro/docs/reference/class-name). `element`, `as`, `passProps` and `defaultProps` are accepted by the type but do nothing here; there's no element for them to act on. ## Example 1 — Handing a class to markup you don't own You can wrap a third-party component with `styled` as long as it accepts a `className` prop, and often that's the nicer answer. It stops being the nicer answer when the component has *several* class-name slots, because a wrapper only ever reaches the outer one. `react-select` is the everyday version of this: one prop, one callback per inner part, each expected to return a class string. ### How you define it Each slot is its own small definition. No wrapper, no props, no forwarding: ```ts import { className } from "@salty-css/astro/class-name"; export const selectControl = className({ base: { display: "flex", alignItems: "center", minHeight: "2.75rem", padding: "0 0.75rem", borderRadius: "8px", border: "1px solid {colors.border}", background: "{theme.bg}", }, variants: { state: { focused: { borderColor: "{colors.brand.main}" }, }, }, }); export const selectMenu = className({ base: { marginTop: "0.25rem", borderRadius: "8px", overflow: "hidden", background: "{theme.bg}", boxShadow: "0 8px 24px rgba(0, 0, 0, 0.12)", }, }); export const selectOption = className({ base: { padding: "0.5rem 0.75rem", cursor: "pointer", "&:hover": { background: "{colors.brand.subtle}" }, }, variants: { state: { selected: { background: "{colors.brand.main}", color: "{colors.paper}" }, }, }, }); ``` ### How you use it ```tsx // react-select is a React component, so this lives in a .tsx island — // the class strings themselves are framework-agnostic either way. import Select from "react-select"; import { selectControl, selectMenu, selectOption } from "../styles/select.css"; export const FlavourPicker = ({ options }) => (