BothDocumentation 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.
%styled.<tag> and %cx accept an array of Css.rule values as an alternative to a plain CSS string. This is the Array API. It lets you compose styles from variables, conditional expressions, pattern matches, and function calls — things that are impossible inside a static string — while still producing fully scoped, emotion-injected CSS at runtime.
What is Css.rule?
Css.rule is a single CSS declaration, selector block, or at-rule. Concretely it covers:
- A CSS property declaration — e.g.
display: flex - A CSS selector — e.g.
&:hover { color: red; } - A CSS at-rule — e.g.
@media (max-width: 600px) { ... }
Css.rule values using:
%css("...")(ReScript) /[%css "..."](Reason) — the inline CSS extension (see %css reference)- Any constructor from the
CSS.*module (see Runtime: CSS)
Basic example
Css.rule is valid inside the array.
Features
Composability
Reference shared style variables or helper functions defined elsewhere in your codebase. DRY out common patterns across components.
Conditionals
Use ternary expressions or
if/else to apply styles based on props or environment values.Pattern matching
Use
switch to map a variant prop to a specific CSS declaration — type-safely.bs-css migration
Bridge existing
bs-css rules or inject unsupported properties via CSS.unsafe without rewriting everything at once.Composability, conditionals, and function calls
Pattern matching in dynamic components
Combine dynamic components with the Array API to useswitch expressions that map typed props to CSS declarations:
Usage with dynamic components (function body)
When the component function needs to run arbitrary code before producing the array — for example, calling a theme function — wrap the function body in braces and end it with an array literal:Theme.button(~variant) is called with fresh props each time.
Limitation: the last expression must be an array literal
Just as with string-form dynamic components, the last expression of the function body must be an array literal written directly in the source — it cannot be a variable reference or a function call that returns an array.Migration from bs-css and unsupported properties
The Array API is the recommended path for migrating frombs-css or for using CSS properties that styled-ppx’s parser does not yet support. Pass existing Css.rule values you already have, or use CSS.unsafe to inject a raw property string:
%styled:
When using
CSS.unsafe, type safety is your responsibility. The property name and value are passed through to emotion without any validation by styled-ppx. Prefer filing an issue or opening a PR to add proper support for a missing property if you have the time.