You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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]>
@@ -148,12 +149,27 @@ The most minimalistic approach is to create simple bindings to e.g. [`emotion`](
148
149
This will give you straight-forward access to the `emotion` apis. Here's how you'd use them in your app code:
149
150
150
151
```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({
153
166
"color": "#fff",
154
167
"backgroundColor": "red"
155
-
})}
156
-
/>
168
+
})
169
+
// your other declarations
170
+
}
171
+
172
+
let app = <div className={Styles.container} />
157
173
```
158
174
159
175
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
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.
171
189
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
+
172
201
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