Skip to content

Commit 0cc9dcf

Browse files
bloodyowlryyppy
andauthored
Add precisions about CSS-in-JS (#270)
* Enable css highlighting * Add styling section to rescript-react docs * Fix formatting * Add precisions about CSS-in-JS - Shows examples where we hoist the output className (vs inlining it in props, which degrades performances for no reason when in `render`) - Add "organization" example where styles are grouped in a `Styles` module (like React Native) - Feature a binding that lets users input raw CSS (quite popular) * Update pages/docs/react/latest/styling.mdx Co-authored-by: Patrick Ecker <[email protected]> Co-authored-by: Patrick Stapfer <[email protected]>
1 parent 3dd0519 commit 0cc9dcf

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

pages/docs/react/latest/styling.mdx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The most minimalistic approach is to create simple bindings to e.g. [`emotion`](
139139
// src/Emotion.res
140140
141141
@module("@emotion/css") external css: {..} => string = "css"
142+
@module("@emotion/css") external rawCss: string => string = "css"
142143
@module("@emotion/css") external keyframes: {..} => string = "css"
143144
@module("@emotion/css") external cx: array<string> => string = "cx"
144145
@@ -148,12 +149,27 @@ The most minimalistic approach is to create simple bindings to e.g. [`emotion`](
148149
This will give you straight-forward access to the `emotion` apis. Here's how you'd use them in your app code:
149150

150151
```res
151-
let app = <div
152-
className={Emotion.css({
152+
let container = Emotion.css({
153+
"color": "#fff",
154+
"backgroundColor": "red"
155+
})
156+
157+
let app = <div className={container} />
158+
```
159+
160+
You can also use submodules to organize your styles more easily:
161+
162+
```res
163+
module Styles = {
164+
open Emotion
165+
let container = css({
153166
"color": "#fff",
154167
"backgroundColor": "red"
155-
})}
156-
/>
168+
})
169+
// your other declarations
170+
}
171+
172+
let app = <div className={Styles.container} />
157173
```
158174

159175
Please note that this approach will not be type-checking for valid css attribute names. If you e.g. want to make sure that only valid CSS attributes are being passed, you could define your `css` function like this as well:
@@ -162,11 +178,24 @@ Please note that this approach will not be type-checking for valid css attribute
162178
@module("@emotion/css") external css: React.Style.t => string = "css"
163179
164180
// Usage is slightly different (and probably less ergonomic)
181+
let container = ReactDOM.Style.make(~padding="20px", ())->css;
182+
165183
let app = <div
166-
className={ReactDOM.Style.make(~padding="20px", ())->css}
184+
className={container}
167185
/>
168186
```
169187

170188
In the example above we used the already existing `React.Style.t` type to enforce valid CSS attribute names. There's a spectrum on how type-safe an API might be, so choose a solution that fits your team's needs.
171189

190+
Last but not least, you can also bind to functions that let you use raw CSS directly:
191+
192+
```res
193+
let container = Emotion.rawCss(`
194+
color: #fff;
195+
background-color: red;
196+
`)
197+
198+
let app = <div className={container} />
199+
```
200+
172201
For more CSS-in-JS ideas, you can also check out related discussions on our [forum](https://forum.rescript-lang.org), or start a new topic.

0 commit comments

Comments
 (0)