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
There are many different approaches on how to do styling in React, such as:
9
+
React comes with builtin support for inline styles, but there are also a number of third party libraries for styling React components. You might be comfortable with a specific setup, like:
There's no right or wrong when choosing a styling strategy, and each method has its up- and downsides. This page will give you some pointers on how to style your ReScript React components in your preferred style.
15
+
If they work in JS then they almost certainly work in ReScript. In the next few sections, we've shared some ideas for working with popular libraries. If you're interested in working with one you don't see here, search the [package index](https://rescript-lang.org/packages) or post in [the forum](https://forum.rescript-lang.org).
17
16
18
17
19
18
## Inline Styles
@@ -80,11 +79,8 @@ let app = <div className={styles["root"]} />
80
79
81
80
## CSS Utility Libraries
82
81
83
-
There are a number of third party libraries for styling React components and you might be comfortable with a specific setup. If they work in JS then they almost certainly work in ReScript. Below, we've shared some ideas for working with popular libraries. If you're interested in working with one you don't see here, search the [package index](https://rescript-lang.org/packages) or post in [the forum](https://forum.rescript-lang.org)
84
-
85
82
### Tailwind
86
83
87
-
88
84
CSS utility libraries like [TailwindCSS](https://tailwindcss.com) usually require some globally imported CSS.
89
85
90
86
First, create your TailwindCSS main entrypoint file:
@@ -137,9 +133,9 @@ When using the [Tailwind VSCode plugin](https://tailwindcss.com/docs/intellisens
137
133
138
134
## CSS-in-JS
139
135
140
-
Currently there are no official recommendations for CSS-in-JS yet due to the wildly different approaches on how to bind to CSS-in-JS libraries (going from simple to very advanced).
136
+
There's no way we could recommend a definitive CSS-in-JS workflow, since there are many different approaches on how to bind to CSS-in-JS libraries (going from simple to very advanced).
141
137
142
-
The most minimalistic approach is to create simple bindings to e.g. [`emotion`](https://emotion.sh/docs/introduction) (as described [here](https://github.com/bloodyowl/rescript-react-starter-kit/blob/eca7055c59ba578b2d1994fc928d8f541a423e74/src/shared/Emotion.res)):
138
+
For demonstration purposes, let's create some simple bindings to e.g. [`emotion`](https://emotion.sh/docs/introduction) (as described [here](https://github.com/bloodyowl/rescript-react-starter-kit/blob/eca7055c59ba578b2d1994fc928d8f541a423e74/src/shared/Emotion.res)):
143
139
144
140
```res
145
141
// src/Emotion.res
@@ -152,7 +148,7 @@ The most minimalistic approach is to create simple bindings to e.g. [`emotion`](
152
148
@module("@emotion/css") external injectGlobal: string => unit = "injectGlobal"
153
149
```
154
150
155
-
This will give you straight-forward access to the `emotion` apis. Here's how you'd use them in your app code:
151
+
This will give you straight-forward access to `emotion`'s apis. Here's how you'd use them in your app code:
156
152
157
153
```res
158
154
let container = Emotion.css({
@@ -178,7 +174,7 @@ module Styles = {
178
174
let app = <div className={Styles.container} />
179
175
```
180
176
181
-
Please note that this approach will not typecheck for invalid 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:
177
+
Please note that this approach will not check for invalid 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:
Here we used the already existing `React.Style.t` type to enforce valid CSS attribute names.
195
-
196
-
**Rule of thumb:** There's a spectrum on how type-safe an API might be, so choose a solution that fits to your team's needs.
197
-
198
191
Last but not least, you can also bind to functions that let you use raw CSS directly:
199
192
200
193
```res
@@ -206,4 +199,4 @@ let container = Emotion.rawCss(`
206
199
let app = <div className={container} />
207
200
```
208
201
209
-
For more CSS-in-JS ideas, you can also check out related discussions, or start a new topic on our [forum](https://forum.rescript-lang.org).
202
+
Please keep in mind that there's a spectrum on how type-safe an API can be (while being more / less complex to handle), so choose a solution that fits to your team's needs.
0 commit comments