-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (41 loc) · 1.02 KB
/
index.js
File metadata and controls
46 lines (41 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Dynamic styles in CSS = flipping CSS classnames
// Dynamic styles in CSS in JS = ternary
import React from 'react';
import { createComponent } from 'cf-style-container';
import { button, active } from './styles.css';
const Button = createComponent(
({ active }) => ({
padding: '2rem',
backgroundColor: active ? '#ccc' : 'white'
}),
'button',
['onClick']
);
export default class DynamicStyles extends React.Component {
constructor(props) {
super(props);
this.state = { active: false };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState(state => ({
active: !state.active
}));
}
render() {
return (
<article>
<h1>2. Dynamic Styles</h1>
<button
className={`${button} ${this.state.active ? active : ''}`}
onClick={this.handleClick}
>
Hit me
</button>
<Button active={this.state.active} onClick={this.handleClick}>
Hit me
</Button>
</article>
);
}
}