How do I change the Material-UI AppBar background colour? - reactjs

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

Related

How to customize background color of button using Material UI themes

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

Material UI override react subtree except for certain components

Is there a way to override the theme of a react sub-tree, but skip the override for certain components?
I'm overriding all children of a component, making sure all the font size is small, using:
const overrideTheme = (theme: Theme): Theme => {
return createTheme({
...theme,
typography: {
fontSize: 11,
}
})
}
const MyCustomThemeWrapper = ({children}) => {
return (
<div>
<ThemeProvider theme={overrideTheme}>
{children}
</ThemeProvider>
</div>
)
}
What I want to do is make this override exclude certain components (and their children). In particular, I don't want to override the font size of any Dialog components that's part of the children subtree. Any way I can do this, without having to "re-override" the theme for each Dialog? I.e. I want something this:
const Component1 = () => {
return (
<MyCustomThemeWrapper>
<div>
This text has fontSize 11
<span> more font size 11</span>
<Dialog open={open}>
<DialogContent>
This text has the "original" fontSize
</DialogContent>
</Dialog>
</div>
</MyCustomThemeWrapper>
)
}
where the custom theme override "hits" all the children of MyCustomThemeWrapper except for the Dialog subtree. I know I'm asking for much here, but would be really nice if this was possible.
You can override inline component's styles with wrapping another ThemeProvider as a parent to inline components. For instance, If you would like to except the Dialog component from override ThemeProvider, You should wrap it with another ThemeProvider. Because of your code wasn't clear enough to describing on it, I've made another example which in that inline checkboxs styles, override by another ThemeProvider:
import React from "react";
import { createTheme, ThemeProvider } from "#material-ui/core/styles";
import Checkbox from "#material-ui/core/Checkbox";
import { green, orange } from "#material-ui/core/colors";
const outerTheme = createTheme({
palette: {
secondary: {
main: orange[500]
}
}
});
const innerTheme = createTheme({
palette: {
secondary: {
main: green[500]
}
}
});
const CustomCheckBox = () => {
return (
<ThemeProvider theme={innerTheme}>
<Checkbox defaultChecked />
</ThemeProvider>
);
};
export default function App() {
return (
<ThemeProvider theme={outerTheme}>
<Checkbox defaultChecked />
<Checkbox defaultChecked />
<CustomCheckBox />
</ThemeProvider>
);
}
Here's the result:

Set new color for material-ui theme

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>

Setting additional colors in theme Material UI React

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

Change primary and secondary colors in MUI

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

Resources