Skip to content

Commit f9824d7

Browse files
authored
Update README
1 parent fda6787 commit f9824d7

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

README.md

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ If you aren't using npm in your project, you can include ReactCSSOM using UMD bu
4040

4141
## Usage
4242

43+
### Basic
44+
4345
Once you have installed react-cssom, supposing a CommonJS environment, you can import it in your index file, before `ReactDOM.render`.
4446

4547
```js
@@ -51,16 +53,89 @@ import App from './App';
5153
ReactDOM.render(<App />, document.getElementById('root'));
5254
```
5355

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.
5557
You can load styles in the way that you prefer but is important to keep in mind that selectors must be in this form:
5658

5759
```css
5860
.⚛ComponentName
5961
```
6062

6163
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.
6365
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
64139

65140
ReactCSSOM also exposes 2 simple API to mount and unmount styles, here they are:
66141

0 commit comments

Comments
 (0)