styled
stablefunction styled<V extends AnyVariants = Record<string, never>>( Component: ElementType, config: StyledConfig<V>, ): ComponentType<VariantProps<V> & Omit<BoxProps, keyof VariantProps<V>>>
The element or component to wrap. Strings render through <Box as={tag}>; React components render directly. Pressable, Box, and any other motif primitive are valid targets.
Configuration object. base, variants, compoundVariants, defaultVariants — all optional, all merged in a defined order.
Description
Builds a regular React component that resolves variant props, merges the matching style bags onto
base, and renders Component with the result. Caller-supplied style props always override the
variant-derived defaults — a one-off tweak does not need a new variant.
The merge order, from lowest to highest priority:
base- Each active variant (explicit lookup; fallback function if the lookup misses)
- Each
compoundVariantsentry whose matchers all match - Caller props
If Component is a string, the result renders through <Box as={Component}> so style props go
through the standard pipeline.
StyledConfig
base— style props always applied.variants— named groups of style overrides. Two forms can coexist for the same axis:- Explicit:
size: { sm: { p: '$2' }, md: { p: '$4' } }. The matching prop accepts only the listed keys (orbooleanif the keys are'true'/'false'). - Fallback:
'...size': (val) => ({ p: val }). The variant name is the key with the...prefix stripped. Any value the caller passes flows through the fallback.
- Explicit:
compoundVariants— array of matchers plus acssbag. Applies when every matcher matches. Matchers can only target explicit variants — fallback values are open and not addressable.defaultVariants— values used when the caller does not specify the prop. Only explicit variants can have defaults.
VariantProps
The prop type derived from a variants config. Each variant name becomes one optional prop typed
as the union of its explicit keys and its fallback value type.
<Component size="sm" /> — size is keyof the explicit map; type-checked.
CompoundVariant
One entry in the compoundVariants array: a partial match against explicit variants, plus a css
bag applied when every matcher is satisfied.
Example
Related
- Variants — the model behind
variants/compoundVariants/defaultVariants. - Composition — how
styledcomposes withBox. - Build a design system — a longer worked example.