I have a react app using MUI. When you import a button, you can set its style using primary={true} or secondary={true}. I want to change the primary and secondary colors. I found out that I can do that this way:
const theme = createMuiTheme({
palette: {
primary: '#00bcd4',
secondary: '#ff4081'
}
});
and then here I can use it:
<MuiThemeProvider theme={theme}>
<App/>
</MuiThemeProvider>
But I got an error: createMuiTheme is not a function...
I went into the MUI package and found out that there is not such file and when I import createMuiTheme, I get undefined. It's supposed to be imported from material-ui/styles/theme but it actually doesn't have this folder at all!
I was using material-ui#0.19.4. I updated this package to v20 and there is still no such folder anyway.
New Answer
With the latest version of Material UI, createMuiTheme is deprecated now. Hence, instead of that we should use createTheme
import React from 'react';
import { render } from 'react-dom';
import { MuiThemeProvider, createTheme } from '#material-ui/core/styles';
import Root from './Root';
// use default theme
// const theme = createTheme();
// Or Create your Own theme:
const theme = createTheme({
palette: {
secondary: {
main: '#E33E7F'
}
}
});
function App() {
return (
<MuiThemeProvider theme={theme}>
<Root />
</MuiThemeProvider>
);
}
render(<App />, document.querySelector('#app'));
Old answer
From https://material-ui.com/customization/themes/:
import React from 'react';
import { render } from 'react-dom';
import { MuiThemeProvider, createMuiTheme } from '#material-ui/core/styles';
import Root from './Root';
// use default theme
// const theme = createMuiTheme();
// Or Create your Own theme:
const theme = createMuiTheme({
palette: {
secondary: {
main: '#E33E7F'
}
}
});
function App() {
return (
<MuiThemeProvider theme={theme}>
<Root />
</MuiThemeProvider>
);
}
render(<App />, document.querySelector('#app'));
The two diferrences with the documentation i noticed is the name of the prop for the MuiThemeProvider:
<MuiThemeProvider muiTheme={muiTheme}>
<AppBar title="My AppBar" />
</MuiThemeProvider>
And the function is not createMuiTheme but getMuiTheme:
import getMuiTheme from 'material-ui/styles/getMuiTheme';
Update:
If you open the light theme from the package, they are not using primary or secondary, maybe you should try like that?
You should be using v1-beta as the docs recommend. I had these issues myself and realised that I was using an outdated version of MUI.
npm install material-ui#next
Import:
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
Create your theme:
const theme = createMuiTheme({
palette: {
secondary: {
main: '#d32f2f'
}
},
});
Apply it to your button:
<MuiThemeProvider theme={theme}>
<Button
onClick={this.someMethod}
variant="raised"
color="secondary"
</Button>
</MuiThemeProvider>
Usually if you get any import issues regarding MUI, then it's almost every time some version problem.
If you want to use a custom color, put it in the main property of an object. MUI will use that color to calculate the dark, light and contrastText values besides the main one.
dark, light: a darker and lighter versions of the main color.
contrastText: the color of the text if the background color is the main color.
The example below:
const theme = createTheme({
palette: {
primary: {
main: '#ff0000', // very red
},
secondary: {
main: '#00fff0', // very cyan
},
},
});
Generates the following color properties in the primary and secondary object:
const theme = useTheme();
const { main, dark, light, contrastText } = theme.palette.primary;
// same with secondary
// const { main, dark, light, contrastText } = theme.palette.secondary;
You can also use MUI colors by passing the color object directly to primary or secondary property. This time, MUI uses the property 500 (for example amber[500]) to calculate 3 other colors. The code below:
import { amber, deepPurple } from '#mui/material/colors';
const theme = createTheme({
palette: {
primary: amber,
secondary: deepPurple,
},
});
Generates the following color properties in the primary and secondary object, Note that because you pass the whole color, all other shades from 50 to A700 are also available as a small side-effect:
Live Demo
Related Answers
How to add custom MUI palette colors
Material-UI: how to access all palette shades inside component
The new API for MUI Material v5 is described in https://mui.com/material-ui/customization/theming/.
import * as React from 'react';
import { red } from '#mui/material/colors';
import { ThemeProvider, createTheme } from '#mui/material/styles';
const theme = createTheme({
palette: {
primary: {
main: red[500],
},
},
});
const App = () =>
<ThemeProvider theme={theme}>...</ThemeProvider>;
Related
I am new to MUI, but all was pretty easy to understand beside changing to dark theme.
I opened documentation of MUI at page where there are examples with dark theme. I copied the first example and it didn't work to me. Why doesn't this simple code example change my MUI theme to 'dark' mode? from here I understood that I need to add more components, but I didn't really understand what does it mean. If I don't add DOM tree it doesn't work? Why the background doesn't change?!
import React from 'react';
import { ThemeProvider, createTheme } from '#mui/material/styles';
function App() {
const darkTheme = createTheme({
palette: {
mode: 'dark'
},
});
return (
<ThemeProvider theme={darkTheme}>
Hello World
</ThemeProvider>
);
}
export default App;
You shoul add <CssBaseline />
import React from 'react';
import { ThemeProvider, createTheme } from '#mui/material/styles';
function App() {
const darkTheme = createTheme({
palette: {
mode: 'dark'
},
});
return (
<ThemeProvider theme={darkTheme}>
<CssBaseline />
Hello World
</ThemeProvider>
);
}
export default App;
I can't figure out from the documentation how to style a button so the background color of that component is purple using Material UI themes. I can get the text color to change to purple with the below. Please let me know what I am doing wrong!
import React, { Component } from 'react';
import { createTheme, ThemeProvider } from '#material-ui/core/styles';
import purple from '#material-ui/core/colors/purple';
import Button from '#material-ui/core/Button';
const theme = createTheme({
palette: {
primary: {
main:purple[500],
}
},
});
export default class Practice extends Component {
render(){
return (
<div>
<ThemeProvider theme={theme}>
<Button color="primary">Add theme</Button>
</ThemeProvider>
</div>
)}}
const theme = createTheme({
overrides : {
MuiButton : {
root : {
// apply your style here
}
},
palette: {
primary: {
main:purple[500],
}
},
});
For the currently latest MUI 5 it should be (from the docs):
const theme = createTheme({
components: {
// Name of the component
MuiButtonBase: {
defaultProps: {
// The props to change the default for.
disableRipple: true, // No more ripple, on the whole application 💣!
},
},
},
});
And then provide your theme as usual.
More info: https://mui.com/material-ui/customization/theme-components/#default-props
I am trying to set a new palette theme for my react app using material-ui's createMuiTheme. This is my code for my custom theme:
import {createMuiTheme} from '#material-ui/core/styles';
const customTheme = createMuiTheme({
palette: {
primary: {
main: '#1977d2', //blue
contrastText: 'white',
},
secondary: {
main: '#FF6600', //orange
contrastText: 'white',
},
regular: {
main: '#73C2FB' //maya
}
}
})
export default customTheme;
This is the code where I set the custom theme as the app's theme:
import './App.css';
import {ThemeProvider} from '#material-ui/core/styles';
import customTheme from './themes/customTheme';
import App from './app/App';
function Main() {
return (
<ThemeProvider theme={customTheme}>
<App />
</ThemeProvider>
);
}
export default Main;
And this is the code where I try to use color regular in a component:
BarButton = ({label, callBack}) => {
return (
<Button variant="contained" color="regular" className={this.props.classes.appBarButton} onClick={callBack}>{label}</Button>
)
}
When I use color="primary" or color="secondary", it works, but color="regular" returns a default light gray color, instead of #73C2FB, that is a light blue.
I followed these directions to achieve what I am aiming, but it is not working.
Custom theme properties can never be applied to any MUI component via the color prop. The reason for this is that MUI takes the the interpolated string value to apply styling via its default props. Your example of
<Button variant="contained" color="regular">{label}</Button>
would look for a containedRegular property of classes that does not exist. IIUC MUI should also provide a props validation error.
Instead, custom theme colors can be applied via the styles or className props.
const useStyles = makeStyles(theme => ({
regular: {
color: theme.palette.regular.main
}
}))
const classes = useStyles()
const theme = useTheme()
<Button style={{color: theme.palette.regular.main}}>foo</Button>
<Button className={classes.regular}>bar</Button>
I'm creating an app using Material-UI components, and have changed the colours in the theme as follows (in src/index.js):
const theme = createMuiTheme({
palette: {
pale: {
main: '#FCF4D9',
contrastText: '#383838'
}
}
})
ReactDOM.render(
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>,
document.getElementById('root'))
Then, in one of my components I want to use this to make the app bar have a pale background. I'm trying to do this as follows (src/Header/index.js):
const Header = () => {
const theme = useTheme()
return (
<AppBar position="fixed" className="appBar" color={`${theme.palette.pale}`}>
...
</AppBar>
)
}
This isn't working - the background just comes out as white. If I rename "pale" to "primary" and then use
color="primary"
that works fine, but I already have another colour called "primary" so I can't do it that way.
I suspect that I'm doing the template literal wrongly, but I'm not sure - I've tried various other combinations of brackets, $ sign etc, but this is the only one that compiles. Or is it that I can only use "primary" and "secondary" in there, and not a theme property? If so, how do I use the theme property?
I hope this helps, Material-UI lets you customize Your own theme.
import { createMuiTheme } from '#material-ui/core/styles';
import purple from '#material-ui/core/colors/purple';
import green from '#material-ui/core/colors/green';
const theme = createMuiTheme({
palette: {
primary: {
main: purple[500],
},
secondary: {
main: green[500],
},
pale: {
main: '#FCF4D9',
contrastText: '#383838',
}
},
});
UPDATED
Once you customize your theme, you can use the different colors on your components based on their names.
For example, In your src/Header/index.js Component, You can do this:
const useTheme = makeStyles((theme) => ({
header: {
color: theme.pale.main,
},
}));
const Header = () => {
const theme = useTheme()
return (
<AppBar position="fixed" className="appBar" color={theme.header}>
...
</AppBar>
)
}
I am trying to look for documentation or code example how can I specify addition colors in Material UI theme.
Right now I have following theme configuration
const theme = createMuiTheme({
palette: {
primary: {
main: "#B31728"
},
secondary: {
main: "#202833"
}
},
...
Now I have a case where I want to use a color for successful operations such as
import { green } from "#material-ui/core/colors";
<Fragment>
{isVerified ? (
<VerifiedUser style={{ color: green[500] }} />
) : (
<Error color="primary" />
)}
</Fragment>
I want to set the color of VerifiedUser Icon in the same way it is set for Error Icon. But the theme palette configuration only has primary and secondary intentions. How can I set a color lets say "success" so that I can be able to pass it like
<VerifiedUser color="success" />
For Material-UI, you can only assign inherit primary secondary default to color, you can customize primary and secondary through createMuiTheme.
To apply your custom theme into component, use MuiThemeProvider :
<MuiThemeProvider theme={theme}>
//your component
</MuiThemeProvider>
Therefore, if you want to generate green theme component, you can create a theme, then use MuiThemeProvider to wrap your component.
Code sample(generate green button):
import React from 'react';
import { MuiThemeProvider, createMuiTheme } from '#material-ui/core/styles';
import Button from '#material-ui/core/Button';
const theme = createMuiTheme({
palette: {
primary: { main: '#00FF00' }
});
function GreenButton() {
return (
<MuiThemeProvider theme={theme}>
<Button color="primary">This is green button</Button>
</MuiThemeProvider>
);
}
Further reading: Customize Material-UI with your theme