--- author: [Teemu Lahjalahti](https://www.linkedin.com/in/teemulahjalahti/) index: https://salty-css.dev/llms.txt --- # `styled` `styled` creates a typed component from an HTML tag or from another component. You hand it a style object; you get back a component where every variant name you declared is a typed prop — consumed for styling, and kept off the DOM unless you opt it back in. The call has to live in a file the compiler reads (`.css.ts`, `.css.tsx`, `.salty.ts`, `.styled.ts`, `.styles.ts`) and has to be a top-level `export`. In a plain `.ts` file it type-checks fine and emits no CSS. For how these components are meant to be composed and extended in a project, see [Styled components](/react/docs/basics/styled-components) in Basics. ## Import ```ts import { styled } from "@salty-css/react/styled"; ``` Astro uses `@salty-css/astro/styled`. Next.js uses the React subpath — only the build plugin is Next-specific. ## Signature ```ts styled(tag: string | ComponentType, params: StyledParams): StyledComponent ``` **`tag`** — an HTML tag name (`"div"`, `"button"`, `"a"`, …) or a component. A component is *wrapped*, not replaced; see [Extending a component](#extending-a-component). Third-party components have one requirement: they must accept a `className` prop, since that's the only channel Salty has to deliver the generated styles. **`params`** — the options object below. Every key is optional. **Returns** — a component that accepts the variant props you declared, the native props of the underlying element, and `className`, `style`, `as`, `children`, `ref`, plus any [`css-*` prop tokens](#prop-tokens) your styles reference. ## Options | Key | What it does | | :-- | :-- | | [`base`](#base) | The styles every instance gets. | | [`variants`](#variants) | Named style branches, each exposed as a typed prop. | | [`compoundVariants`](#compound-variants) | Extra styles for when several variant values are active together. | | [`anyOfVariants`](#any-of-variants) | Extra styles shared by several variant values. Never wins a conflict. | | [`defaultVariants`](#default-variants) | Which branch applies when the consumer omits the prop. | | [`defaultProps`](#default-props) | HTML attributes the element renders with by default. | | [`element`](#element) | Render a different tag without redefining the styles. | | [`passProps`](#pass-props) | Let variant props through to the element instead of consuming them. | | [`className`](#class-name) | Add your own class name alongside the generated hash. | | [`displayName`](#display-name) | The name this component goes by in build output and DevTools. | | [`priority`](#priority) | Which cascade layer the rules land in. Higher wins ties. | ### `base` Styles applied to every instance, in Salty's full style-object syntax: ```ts export const Card = styled("div", { base: { padding: "1.5rem", borderRadius: "12px", background: "{theme.background}", "&:hover": { boxShadow: "0 8px 24px rgb(0 0 0 / 0.08)" }, "& > svg": { flexShrink: 0 }, "@tabletDown": { padding: "1rem" }, }, }); ``` What's accepted inside the object: - **Nested selectors** via `&` — `"&:hover"`, `"&:not(:hover)"`, `"& > svg"`, `"& + &"`, grouped `"&:hover, &:focus"`. - **Token references** — `{path.to.token}`, themed ones as `{theme.xxx}`. Paths are validated at build time. - **At-rules** — an inline `"@media (...)"`/`"@container (...)"` key, or a name you registered with `defineMediaQuery` as `"@tabletDown"`. An unknown `@name` is emitted as a literal at-rule and silently never matches. - **Templates and modifiers** — applied as keys and values inside the object. - **Values** — `string`, `number`, a function, or a promise. Functions and promises are resolved by the compiler, so the result lands in the stylesheet as a literal. Bare numbers get the unit from `defineConfig({ defaultUnit })`, `px` by default. ### `variants` Each top-level key is an axis and becomes a typed prop; each child key is one value on that axis. ```ts export const Button = styled("button", { base: { padding: "0.6em 1.2em", cursor: "pointer" }, variants: { variant: { solid: { background: "{theme.color}", color: "{theme.background}" }, outlined: { border: "1px solid currentColor" }, }, size: { small: { fontSize: "0.8em" }, large: { fontSize: "1.2em" }, }, loading: { true: { opacity: 0.6, pointerEvents: "none" }, }, }, }); ``` ```tsx