styled-ppx supports Melange through the standard opam and dune toolchain. Once installed, the PPX binary integrates with dune’sDocumentation 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.
(preprocess (pps ...)) stanza and the styled-ppx.melange runtime library supplies the CSS module that the generated code depends on.
This guide assumes you already have a working Melange project with opam and dune. If you’re starting from scratch, follow the official Melange getting started guide first.
Install styled-ppx via opam
Install the The package exposes two libraries:
styled-ppx opam package. This provides both the PPX binary and all runtime libraries:styled-ppx— the PPX that transforms[%styled.div {|...|}],[%cx {|...|}], and related extensions at compile time.styled-ppx.melange— the runtime library that exposes theCSSmodule required by styled components at runtime.
Update your dune build files
You need to do two things in your dune configuration:If your project uses a
- Add
styled-ppxunder(preprocess (pps ...)) - Add
styled-ppx.melangeunder(libraries ...)
(library ...) stanza with (modes melange):(melange.emit ...) stanza:reason-react and reason-react-ppx are optional. They are only required if you use styled components ([%styled.div {||}]). If you only use [%cx] or [%css] to generate classNames, you can omit them.Write your first styled component
With dune configured, every Reason file in your library can use the styled-ppx extensions. Here is a complete example showing a dynamic The
Link component with a default colour prop, alongside a [%cx] layout className:Link module is a fully typed React <a> component. The labelled argument ~color defaults to CSS.hex("4299E1") and is interpolated into the CSS via $(color). The layout binding is a plain string containing a hashed className computed by the PPX at compile time.Next steps
Once your Melange project is styled and building, explore the full reference documentation:- Styled components — all HTML tags, dynamic props, and className extension
- className with
[%cx]— generate classNames without creating a component - Keyframe animations — type-safe
@keyframesdefinitions - Global styles — resets and base styles with
[%styled.global] - CSS rule with
[%css]— single-rule values for composition with the Array API