Skip to content

Commit 292f9bc

Browse files
netochaveseps1lon
authored andcommitted
[docs] Add TS demo for DynamicCSSVariables (#17983)
1 parent 13caec8 commit 292f9bc

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

docs/src/pages/customization/components/DynamicCSSVariables.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,40 @@ const useStyles = makeStyles({
1616
},
1717
});
1818

19+
const blue = {
20+
'--background-start': '#2196F3',
21+
'--background-end': '#21CBF3',
22+
'--box-shadow': 'rgba(33, 203, 243, .3)',
23+
};
24+
25+
const defaultColor = {
26+
'--background-start': '#FE6B8B',
27+
'--background-end': '#FF8E53',
28+
'--box-shadow': 'rgba(255, 105, 135, .3)',
29+
};
30+
1931
export default function DynamicCSSVariables() {
2032
const classes = useStyles();
21-
const [color, setColor] = React.useState('default');
33+
const [color, setColor] = React.useState(defaultColor);
2234

2335
const handleChange = event => {
24-
setColor(event.target.checked ? 'blue' : 'default');
36+
setColor(event.target.checked ? blue : defaultColor);
2537
};
2638

2739
return (
2840
<React.Fragment>
2941
<FormControlLabel
3042
control={
3143
<Switch
32-
checked={color === 'blue'}
44+
checked={color === blue}
3345
onChange={handleChange}
3446
color="primary"
3547
value="dynamic-class-name"
3648
/>
3749
}
3850
label="Blue"
3951
/>
40-
<Button
41-
className={classes.button}
42-
style={
43-
color === 'blue'
44-
? {
45-
'--background-start': '#2196F3',
46-
'--background-end': '#21CBF3',
47-
'--box-shadow': 'rgba(33, 203, 243, .3)',
48-
}
49-
: {
50-
'--background-start': '#FE6B8B',
51-
'--background-end': '#FF8E53',
52-
'--box-shadow': 'rgba(255, 105, 135, .3)',
53-
}
54-
}
55-
>
52+
<Button className={classes.button} style={color}>
5653
{'CSS variables'}
5754
</Button>
5855
</React.Fragment>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react';
2+
import { makeStyles } from '@material-ui/core/styles';
3+
import Button from '@material-ui/core/Button';
4+
import FormControlLabel from '@material-ui/core/FormControlLabel';
5+
import Switch from '@material-ui/core/Switch';
6+
7+
const useStyles = makeStyles({
8+
button: {
9+
background: 'linear-gradient(45deg, var(--background-start) 30%, var(--background-end) 90%)',
10+
borderRadius: 3,
11+
border: 0,
12+
color: 'white',
13+
height: 48,
14+
padding: '0 30px',
15+
boxShadow: '0 3px 5px 2px var(--box-shadow)',
16+
},
17+
});
18+
19+
const blue = {
20+
'--background-start': '#2196F3',
21+
'--background-end': '#21CBF3',
22+
'--box-shadow': 'rgba(33, 203, 243, .3)',
23+
} as React.CSSProperties;
24+
25+
const defaultColor = {
26+
'--background-start': '#FE6B8B',
27+
'--background-end': '#FF8E53',
28+
'--box-shadow': 'rgba(255, 105, 135, .3)',
29+
} as React.CSSProperties;
30+
31+
export default function DynamicCSSVariables() {
32+
const classes = useStyles();
33+
const [color, setColor] = React.useState(defaultColor);
34+
35+
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
36+
setColor(event.target.checked ? blue : defaultColor);
37+
};
38+
39+
return (
40+
<React.Fragment>
41+
<FormControlLabel
42+
control={
43+
<Switch
44+
checked={color === blue}
45+
onChange={handleChange}
46+
color="primary"
47+
value="dynamic-class-name"
48+
/>
49+
}
50+
label="Blue"
51+
/>
52+
<Button className={classes.button} style={color}>
53+
{'CSS variables'}
54+
</Button>
55+
</React.Fragment>
56+
);
57+
}

0 commit comments

Comments
 (0)