@@ -40,6 +40,8 @@ If you aren't using npm in your project, you can include ReactCSSOM using UMD bu
40
40
41
41
## Usage
42
42
43
+ ### Basic
44
+
43
45
Once you have installed react-cssom, supposing a CommonJS environment, you can import it in your index file, before ` ReactDOM.render ` .
44
46
45
47
``` js
@@ -51,16 +53,89 @@ import App from './App';
51
53
ReactDOM .render (< App / > , document .getElementById (' root' ));
52
54
```
53
55
54
- Now you can use react-cssom as you can see in the gif above, just like normal css.
56
+ Now you can use react-cssom as you can see in the gif above, just like in normal css, you can select React Components .
55
57
You can load styles in the way that you prefer but is important to keep in mind that selectors must be in this form:
56
58
57
59
``` css
58
60
.⚛ComponentName
59
61
```
60
62
61
63
For example, this is a valid one `.⚛App` .
62
- So, pay attetion that components' names and css selectors always match.
64
+ So, pay attention that components' names and css selectors always match.
63
65
This is particular important if you have css-modules that modifies the original names, or code obfuscation.
66
+ The first ones for example need a syntax like this:
67
+
68
+ ```css
69
+ :global .⚛ComponentName {
70
+ /* styles here */
71
+ }
72
+ ```
73
+
74
+
75
+ ### Adapting based on props
76
+
77
+ If you want to set styles based on props, you can do it in 2 ways:
78
+
79
+ 1 . Set inline styles, as we can see in this example:
80
+ ``` js
81
+ class Button extends React .Component {
82
+ render () {
83
+ return (
84
+ < button
85
+ style= {{
86
+ backgroundColor: this .props .primary ? ' blue' : ' black' ,
87
+ }}
88
+ >
89
+ Click me
90
+ < / button>
91
+ )
92
+ }
93
+ }
94
+ ```
95
+
96
+ 2 . Set a specific class, maybe using css-modules, as we can see here:
97
+ ``` js
98
+ import styles from ' ./Button.css' ;
99
+
100
+ export default class Button extends React .Component {
101
+ render () {
102
+ return (
103
+ < button
104
+ className= {this .props .primary ? styles .primary : styles .default }
105
+ >
106
+ Click me
107
+ < / button>
108
+ )
109
+ }
110
+ }
111
+ ```
112
+
113
+ and here is the corresponding css, note the global selector:
114
+
115
+ ``` css
116
+ :global .⚛Button {
117
+ height : 50px ;
118
+ width : 100px ;
119
+ }
120
+
121
+ .primary {
122
+ background-color : blue ;
123
+ }
124
+
125
+ .primary :global.⚛Button {
126
+ color : yellow ;
127
+ }
128
+
129
+ .default {
130
+ background-color : grey ;
131
+ }
132
+
133
+ .default :global.⚛Button {
134
+ color : black ;
135
+ }
136
+ ```
137
+
138
+ ### Additional API
64
139
65
140
ReactCSSOM also exposes 2 simple API to mount and unmount styles, here they are:
66
141
0 commit comments