--- author: [Teemu Lahjalahti](https://www.linkedin.com/in/teemulahjalahti/) index: https://salty-css.dev/llms.txt --- # Interactive State Interactive state — hover, focus, a form that's mid-submit, a panel that's "in edit mode" — mostly isn't a JavaScript problem. A surprising amount of it is state the browser already tracks for you and will style for free, and for the rest, Salty gives you a short ladder of escalations so you only reach for React state when the platform genuinely can't see what you need. The mindset underneath it, and the thing that keeps components small: **climb the ladder only as far as the state forces you to.** Four rungs, roughly: 1. **The browser already tracks it** — hover, focus, a valid field, a checked box, a container that holds an invalid input. Style it with a pseudo-class. No prop, no state, no JavaScript. This is Example 1, and it covers more than people expect. 2. **It's app state the browser can't see, but it's a closed set** — idle / submitting / error / success. Lift it to a **variant** and drive that variant from your React state. Example 2. 3. **It's an open-ended value, not a closed set** — a progress width, a live count, a user-picked amount. That's a **prop token** (`{props.X}`), not a variant. Example 3. 4. **You want a whole region to *****read***** as a state** — recolor everything under "edit mode" or "danger" at once. That's **theming** pointed at a state axis instead of a light/dark one. Example 4. Everything below is authored in TypeScript in a `.css.ts` file and compiled to a static stylesheet at build time. Rungs 1, 2 and 4 ship no styling runtime at all — the only JavaScript in play is your own state setting a prop or an attribute. Rung 3 is the one place a value rides a live CSS variable into the browser, on purpose. --- ## Example 1 — State the browser already tracks Start here every time, because this rung is bigger than it looks. The browser is a state machine that's already running: it knows what's hovered, focused, checked, valid, disabled, and what any element *contains*. Salty's job is just to let you nest those selectors, scoped to a component's hash, with `&`. ### The everyday ones Hover, focus, active, disabled — the states you already reach for. Nest them in `base` and they apply to every instance, no prop required: ```ts import { styled } from "@salty-css/react/styled"; export const Button = styled("button", { base: { padding: "0.6em 1.2em", borderRadius: "6px", cursor: "pointer", transition: "background 150ms ease", "&:hover": { background: "{colors.grey.light}" }, "&:active": { transform: "translateY(1px)" }, // native disabled — the browser also stops clicks and tells assistive tech "&:disabled": { opacity: 0.5, cursor: "not-allowed" }, }, }); ``` Nothing here is a variant, because nothing here is *yours* to track — the browser flips these states as the user interacts, and your rule just reacts. Hold onto the `&:disabled` line; it comes back in Example 2 as the reason a `disabled` **variant** is usually a mistake. ### The ones people forget they have A chunk of "I'll just go wire that up myself" is a pseudo-class you didn't know had shipped. We'll memoize every callback and shave a kilobyte off a bundle, then hand-roll in JavaScript a piece of state the browser was already tracking for free. A few worth keeping in the front of your mind: ```ts export const Field = styled("label", { base: { display: "grid", gap: "0.25rem", // the field wrapper glows when anything inside it has focus "&:focus-within": { outline: "2px solid {colors.brand.main}" }, // style the input, but only show validation AFTER the user has interacted — // :user-invalid waits for a blur/submit, so you don't scold an empty field on load "& input:user-invalid": { borderColor: "{colors.danger}" }, "& input:user-valid": { borderColor: "{colors.success}" }, // fade the label while the input still shows its placeholder "& input:placeholder-shown + span": { opacity: 0.6 }, }, }); ``` `:focus-within`, `:user-invalid` / `:user-valid`, and `:placeholder-shown` are all Baseline-supported and let you build a form that reacts to itself with zero state. `:user-invalid` in particular is the fix for the classic "why is my whole form red before I've typed anything" problem — unlike plain `:invalid`, it only matches once the user has actually touched the field. ### And it's not only pseudo-classes State the browser tracks isn't limited to pseudo-classes. Some of it lives in plain attributes, and some elements are little state machines on their own. `
` is the clearest example — a native open/closed disclosure with no JavaScript and no library. The browser flips the `open` attribute when the summary is clicked; you just style off it: ```ts export const Disclosure = styled("details", { base: { borderLeft: "3px solid transparent", "& summary": { cursor: "pointer", fontWeight: 600 }, // the browser toggles [open] for you — style the open state directly "&[open]": { borderLeftColor: "{colors.brand.main}" }, "&[open] summary": { marginBottom: "0.5rem" }, }, }); ``` The same goes for the ARIA and `data-` attributes already sitting in your markup. If you've set `aria-expanded`, `aria-current`, or `aria-selected` for assistive tech — and you should have — that attribute doubles as a free styling hook, no extra state to track: ```ts export const NavLink = styled("a", { base: { opacity: 0.7, // the attribute is there for screen readers anyway; style off it for free "&[aria-current='page']": { opacity: 1, fontWeight: 700 }, }, }); ``` ### Reacting to children, and styling into children Two directions worth naming, because they're the ones that used to require JavaScript. **From a child's state, up to the parent** — this is `:has()`, the long-requested "parent selector." The wrapper styles itself based on what it contains: ```ts export const Card = styled("article", { base: { padding: "1rem", borderRadius: "8px", // trim the top padding only when the card actually leads with an image "&:has(> img:first-child)": { paddingTop: 0, overflow: "hidden" }, // the whole card flags itself when a field inside is invalid "&:has(input:user-invalid)": { borderColor: "{colors.danger}" }, // and lifts when anything inside takes keyboard focus "&:has(:focus-visible)": { boxShadow: "0 0 0 3px {colors.brand.muted}" }, }, }); ``` **From the parent's state, down into children** — a parent pseudo-class driving a descendant. Nest the child selector under the state: ```ts export const Menu = styled("nav", { base: { "& a": { opacity: 0.7, transition: "opacity 150ms ease" }, // light every link up when the nav itself is hovered "&:hover a": { opacity: 1 }, }, }); ``` Because every selector Salty emits is anchored to the component's own hash, none of this leaks — `&:has(...)` and `& a` only ever match inside *this* component, never some unrelated element across the app. That scoping guarantee is the whole point of the [Scoping & Specificity concept](/concepts/scoping), and if you need to target another Salty component by identity rather than by tag, that page covers interpolating a component's hash into a selector. The takeaway for the rest of the ladder: before you add a prop, ask whether the browser is already tracking the thing you want to style. Often it is. --- ## Example 2 — App state the browser can't see: lift it to a variant Some state is genuinely yours. Whether a request is in flight, whether the last save failed — the browser has no pseudo-class for "my API call is pending." When the state is a **closed set of named modes**, model it as a variant axis and drive that axis from your React state. ### How you define it One axis, one value per mode. The browser-tracked states stay where they belong — in `&:disabled` — and the variant only carries what the browser can't know: ```ts import { styled } from "@salty-css/react/styled"; export const SubmitButton = styled("button", { base: { padding: "0.6em 1.2em", borderRadius: "6px", cursor: "pointer", transition: "background 150ms ease, opacity 150ms ease", "&:disabled": { opacity: 0.5, cursor: "not-allowed" }, }, variants: { status: { idle: {}, submitting: { opacity: 0.7, pointerEvents: "none" }, error: { background: "{colors.danger}", color: "white" }, success: { background: "{colors.success}", color: "white" }, }, }, defaultVariants: { status: "idle" }, }); ``` ### How you use it Your state hook owns the value; you pass it straight in as the prop. Note that the *real* `disabled` here is the native HTML attribute — the browser disables the button and styles it via the `&:disabled` from `base` — while `status` carries the app state: ```tsx "use client"; import { useState } from "react"; import { SubmitButton } from "./submit-button.css"; export function SignupForm() { const [status, setStatus] = useState<"idle" | "submitting" | "error" | "success">("idle"); async function handleSubmit() { setStatus("submitting"); try { await createAccount(); setStatus("success"); } catch { setStatus("error"); } } return ( {status === "submitting" ? "Creating account…" : "Sign up"} ); } ``` The variant prop is consumed by Salty and never reaches the DOM, so there's no stray `status="error"` attribute on the rendered `