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