Dynamic components let you drive CSS values directly from React props. Instead of a static CSS string, you provide a function — the labelled arguments become props on the generated component, and the function body is a CSS string that interpolates those arguments. styled-ppx validates the CSS at compile time and infers the correct type for each argument based on where it appears in the CSS.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/davesnx/styled-ppx/llms.txt
Use this file to discover all available pages before exploring further.
Function form of %styled.<tag>
Wrap your CSS string in a function that takes labelled arguments (~argName) and returns the CSS string. styled-ppx picks up the argument names, exposes them as component props, and infers their types from the CSS property positions they are used in — a technique called type holes.
color and background are inferred as CSS.Color.t because they appear in the color and background-color CSS properties, which accept colour values. You can confirm this by hovering the prop in your editor — no annotation is required. All interpolation rules apply inside the function body.
How type inference works (type holes)
When styled-ppx sees$(color) in the position of a CSS property value, it reads the property name (color:) and uses the known CSS grammar to determine what type that position accepts. It then unifies that type with the labelled argument ~color, giving it a concrete type without any manual annotation.
This means:
margin: $(size)→sizeis inferred as a CSSlengthtype (e.g.CSS.px(int),CSS.rem(float), etc.)color: $(c)→cis inferred asCSS.Color.topacity: $(o)→ois inferred asfloat
Default prop values
Labelled arguments can have default values using the standard OCaml/ReScript default argument syntax:Important limitation: the returned expression must be a literal
The last expression of a dynamic component function — the value that becomes the CSS — must be a string literal or an array literal written directly at that position. It cannot be a variable reference or a function call.Name collision with DOM props
All styled components expose the standard DOM props for their element viamakeProps. If you declare a dynamic prop with the same name as one of those DOM props, the dynamic prop wins — the original DOM prop is replaced.
Using the Array API for more complex logic
When you need pattern matching, conditional logic, or variable bindings inside a dynamic component, combine it with the Array API. Return an array ofCss.rule values from your function instead of a plain string.
See Array API for full examples including switch expressions and runtime theme lookups.