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
- CSS-in-JS (such as `styled-components`, `emotion`, etc.)
15
15
16
+
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.
17
+
18
+
16
19
## Inline Styles
17
20
18
-
You can apply a `style` attribute to any DOM element with our `ReactDOM.Style.make` API:
21
+
This is the most basic form of styling, coming straight from the 90ies. You can apply a `style` attribute to any DOM element with our `ReactDOM.Style.make` API:
It's a labeled (typed!) function call that maps to the familiar style object `{color: '#444444', fontSize: '68px'}`.
29
+
It's a labeled (therefore typed) function call that maps to the familiar style object `{color: '#444444', fontSize: '68px'}`. For every CSS attribute in the CSS specfication, there is a camelCased label in our `make` function.
27
30
28
31
**Note** that `make` returns an opaque `ReactDOM.Style.t` type that you can't read into. We also expose a `ReactDOM.Style.combine` that takes in two `style`s and combine them.
29
32
@@ -49,7 +52,7 @@ Use a `%%raw` expression to import CSS files within your ReScript / React compon
49
52
50
53
## CSS Modules
51
54
52
-
CSS modules can be imported like any other JS module. The imported value is a JS object, with each key mapping to a classname within the imported CSS file.
55
+
[CSS modules](https://github.com/css-modules/css-modules) can be imported like any other JS module. The imported value is a JS object, with attributes equivalent to each classname defined in the CSS file.
53
56
54
57
As an example, let's say we have a CSS module like this:
55
58
@@ -61,13 +64,14 @@ As an example, let's say we have a CSS module like this:
61
64
}
62
65
```
63
66
64
-
We now need to create a module binding that imports our styles:
67
+
We now need to create a module binding that imports our styles as a JS object:
65
68
66
69
```res
67
70
// {..} means we are handling a JS object with an unknown
// Use the obj["key"] syntax to access any classname within our object
71
75
let app = <div className={styles["root"]} />
72
76
```
73
77
@@ -119,7 +123,7 @@ let make = (~active: bool) => {
119
123
120
124
When using the [Tailwind VSCode plugin](https://tailwindcss.com/docs/intellisense), make sure to include ReScript as a target language to get autocompletion:
121
125
122
-
- Open the VSCode settings
126
+
- Open your VSCode settings
123
127
- Search for `Tailwind CSS: Include Languages`
124
128
- Add a new item with following settings:
125
129
- Item: `rescript`
@@ -131,7 +135,7 @@ When using the [Tailwind VSCode plugin](https://tailwindcss.com/docs/intellisens
131
135
132
136
## CSS-in-JS
133
137
134
-
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 (going from simple to very advanced).
138
+
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).
135
139
136
140
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)):
137
141
@@ -172,7 +176,7 @@ module Styles = {
172
176
let app = <div className={Styles.container} />
173
177
```
174
178
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:
179
+
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:
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.
192
+
Here we used the already existing `React.Style.t` type to enforce valid CSS attribute names.
193
+
194
+
**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.
189
195
190
196
Last but not least, you can also bind to functions that let you use raw CSS directly:
191
197
@@ -198,4 +204,4 @@ let container = Emotion.rawCss(`
198
204
let app = <div className={container} />
199
205
```
200
206
201
-
For more CSS-in-JS ideas, you can also check out related discussionson our [forum](https://forum.rescript-lang.org), or start a new topic.
207
+
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).
0 commit comments