Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] Add TS demo for DynamicCSS #17994

Merged
merged 1 commit into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/src/pages/customization/components/DynamicCSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Switch from '@material-ui/core/Switch';
// Like https://github.com/brunobertolini/styled-by
const styledBy = (property, mapping) => props => mapping[props[property]];

const StyledButton = withStyles({
const styles = {
root: {
background: styledBy('color', {
default: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
Expand All @@ -23,7 +23,11 @@ const StyledButton = withStyles({
blue: '0 3px 5px 2px rgba(33, 203, 243, .3)',
}),
},
})(({ classes, color, ...other }) => <Button className={classes.root} {...other} />);
};

const StyledButton = withStyles(styles)(({ classes, color, ...other }) => (
<Button className={classes.root} {...other} />
));

export default function DynamicCSS() {
const [color, setColor] = React.useState('default');
Expand Down
72 changes: 72 additions & 0 deletions docs/src/pages/customization/components/DynamicCSS.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from 'react';
import { withStyles, WithStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import Switch from '@material-ui/core/Switch';

interface Styles {
color: string;
children: React.ReactNode;
[key: string]: any;
}

interface ColorsMapping {
default: string;
blue: string;
[key: string]: any;
}

interface ButtonStyles extends WithStyles<typeof styles> {
color: string;
}

// Like https://github.com/brunobertolini/styled-by
const styledBy = (property: string, mapping: ColorsMapping) => (props: Styles) =>
mapping[props[property]];

const styles = {
root: {
background: styledBy('color', {
default: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
blue: 'linear-gradient(45deg, #2196F3 30%, #21CBF3 90%)',
}),
borderRadius: 3,
border: 0,
color: 'white',
height: 48,
padding: '0 30px',
boxShadow: styledBy('color', {
default: '0 3px 5px 2px rgba(255, 105, 135, .3)',
blue: '0 3px 5px 2px rgba(33, 203, 243, .3)',
}),
},
};

const StyledButton = withStyles(styles)(({ classes, color, ...other }: ButtonStyles) => (
<Button className={classes.root} {...other} />
));

export default function DynamicCSS() {
const [color, setColor] = React.useState('default');

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setColor(event.target.checked ? 'blue' : 'default');
};

return (
<React.Fragment>
<FormControlLabel
control={
<Switch
checked={color === 'blue'}
onChange={handleChange}
color="primary"
value="dynamic-class-name"
/>
}
label="Blue"
/>
<StyledButton color={color}>Dynamic CSS</StyledButton>
</React.Fragment>
);
}