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.
%cx (ReScript) / [%cx] (Reason) is styled-ppx’s lightweight alternative to %styled.<tag>. Instead of producing a React component module, it produces a plain string — the scoped class name generated by emotion. You attach that string to any element via its className prop.
This is useful when naming a whole component feels like overkill, when you need to apply styles inline in JSX, or when you want to reuse a single class definition across multiple elements without creating a module for it.
When to use %cx vs %styled.*
| Situation | Recommendation |
|---|---|
| You need a reusable, named React component | Use %styled.<tag> |
| Styles are used in exactly one place inline | Use %cx |
| You want to compose several class strings with logic | Use %cx |
You need DOM prop forwarding (onChange, ref, etc.) | Use %styled.<tag> |
Basic inline usage
The simplest form is writing%cx(...) directly inside a className prop:
Multi-line declarations
For longer style blocks, use a backtick string (ReScript) or a quoted string (Reason) to span multiple lines. The result is still astring:
fullWidth is a string that contains a hash pointing to a style <style> tag injected into the document <head>. It is stable across renders — emotion only injects each unique rule set once.
Combining class names
Because%cx is just a string, you can concatenate it with other class names using the standard string concatenation operator (++):
Features
- Selectors — pseudo-classes, pseudo-elements, and nested selectors work exactly as in
%styled.<tag>: - Media queries —
@mediablocks are fully supported inside%cx. - Interpolation — embed typed OCaml/ReScript values with
$(identifier)syntax:See Interpolation for how type inference works. - Array API — pass an array of
Css.rulevalues instead of a string:See Array API for full details.
What is NOT allowed
Wrapping braces ({ }) around the declarations are not permitted. The content of %cx must be a list of CSS declarations (property–value pairs and nested rules), not a CSS rule block.