React Material UI - Tuong-Nguyen/JavaScript-Structure GitHub Wiki
- Create a theme
- Use
<MuiThemeProvider />to wrap the components we want to customize
- Base on the options received
const theme = createMuiTheme({
status: {
danger: 'orange'
}
});-
createPalette({}): https://material-ui-1dab0.firebaseapp.com/customization/themes#palette -
createTypography(): https://material-ui-1dab0.firebaseapp.com/customization/themes#typography - Read more theme variables
Override default style of Button
const theme = createMuiTheme({
overrides: {
MuiButton: {
// Name of the styleSheet
root: {
// Name of the rule
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)'
// ...
}
}
}
});<MuiThemeProviver theme={theme}>
<Component />
</MuiThemeProvider>The default theme is light. We can make a theme dark by setting type to dark
const theme = createMuiTheme({
palette: createPalette({
type: 'dark', // Switching the dark mode on is a single property value change.
}),
});- Create a stylesheet
const styleSheet = createStyleSheet('OverridesClassNames', {
buttonStyle: {
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)'
}
});- Set
classNameto this stylesheet from component's propertiesprops.classes.buttonStyle
function OverridesClassNames(props) {
return (
<Button className={props.classes.buttonStyle}>
Button
</Button>
);
}- Wrap with Higher-order Component (HoC)
withStyles
export default withStyles(buttonStyle)(OverridesClassNames);- Example: https://material-ui-1dab0.firebaseapp.com/customization/overrides#overriding-with-class-names
- Create a stylesheet with
classesis documented as CSS API of the component
const styleSheet = createStyleSheet('OverridesClasses', {
root: {
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)'
},
label: {
textTransform: 'capitalize',
},
});- Set
classesto this stylesheet from component's propertiesprops.classes.CSSAPI
<Button
classes={{
root: props.classes.root, // className, e.g. `OverridesClasses-root-X`
label: props.classes.label, // className, e.g. `OverridesClasses-label-X`
}}
>
Button
</Button>- Wrap with Higher-order Component (HoC)
withStyles
export default withStyles(styleSheet)(OverridesClasses);- Create a style as set of rules
const style = {
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
borderRadius: 3,
border: 0
};- Set to
styleof component
<Button style={style}>
{'inline-style'}
</Button>Create a custom component that has the same theme with other material-ui components
- Use Higher-order Component (HoC)
withTheme()
export default withTheme(ComponentWithTheme);- Get the
themeas aproperty
function ComponentWithTheme(props) {
const { theme } = props;
const primaryText = theme.palette.text.primary;
// ...
}