Skip to content

Commit 274403f

Browse files
committed
Small refinements / wording
1 parent 0cc9dcf commit 274403f

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

pages/docs/react/latest/styling.mdx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ There are many different approaches on how to do styling in React, such as:
1313
- CSS utility libraries (`tailwindcss`, `tachyons`, etc.)
1414
- CSS-in-JS (such as `styled-components`, `emotion`, etc.)
1515

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+
1619
## Inline Styles
1720

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:
1922

2023
```rescript
2124
<div style=(
2225
ReactDOM.Style.make(~color="#444444", ~fontSize="68px", ())
2326
)/>
2427
```
2528

26-
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.
2730

2831
**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.
2932

@@ -49,7 +52,7 @@ Use a `%%raw` expression to import CSS files within your ReScript / React compon
4952

5053
## CSS Modules
5154

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.
5356

5457
As an example, let's say we have a CSS module like this:
5558

@@ -61,13 +64,14 @@ As an example, let's say we have a CSS module like this:
6164
}
6265
```
6366

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:
6568

6669
```res
6770
// {..} means we are handling a JS object with an unknown
6871
// set of attributes
6972
@module external styles: {..} = "./styles.module.css"
7073
74+
// Use the obj["key"] syntax to access any classname within our object
7175
let app = <div className={styles["root"]} />
7276
```
7377

@@ -119,7 +123,7 @@ let make = (~active: bool) => {
119123

120124
When using the [Tailwind VSCode plugin](https://tailwindcss.com/docs/intellisense), make sure to include ReScript as a target language to get autocompletion:
121125

122-
- Open the VSCode settings
126+
- Open your VSCode settings
123127
- Search for `Tailwind CSS: Include Languages`
124128
- Add a new item with following settings:
125129
- Item: `rescript`
@@ -131,7 +135,7 @@ When using the [Tailwind VSCode plugin](https://tailwindcss.com/docs/intellisens
131135

132136
## CSS-in-JS
133137

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).
135139

136140
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)):
137141

@@ -172,7 +176,7 @@ module Styles = {
172176
let app = <div className={Styles.container} />
173177
```
174178

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:
176180

177181
```res
178182
@module("@emotion/css") external css: React.Style.t => string = "css"
@@ -185,7 +189,9 @@ let app = <div
185189
/>
186190
```
187191

188-
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.
189195

190196
Last but not least, you can also bind to functions that let you use raw CSS directly:
191197

@@ -198,4 +204,4 @@ let container = Emotion.rawCss(`
198204
let app = <div className={container} />
199205
```
200206

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.
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

Comments
 (0)