I'm having a problem with changing button text color directly in the MUI theme. Changing primary color + button font size works fine, so the problem isn't in passing the theme on. Here's my code:
import React from 'react';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import { lightBlue } from 'material-ui/colors';
import styled from 'styled-components';
const theme = createMuiTheme({
palette: {
primary: lightBlue, // works
},
raisedButton: {
color: '#ffffff', // doesn't work
},
typography: {
button: {
fontSize: 20, // works
color: '#ffffff' // doesn't work
}
}
});
const App = ({ user, pathname, onToggleDrawer }) => (
<MuiThemeProvider theme={theme}>
...
</MuiThemeProvider>
);
I also tried using an imported color instead of the #ffffff, but that had no effect either.
Anybody got any ideas?
This worked for me. The color we chose decided to have a dark button contrast color but white as contrast color looks arguably better:
const theme = createMuiTheme({
palette: {
primary: {
main: "#46AD8D",
contrastText: "#fff" //button text white instead of black
},
background: {
default: "#394764"
}
}
});
Solved it! This finally did the trick:
const theme = createMuiTheme({
palette: {
primary: lightBlue,
},
overrides: {
MuiButton: {
raisedPrimary: {
color: 'white',
},
},
}
});
So, you just have to use "overrides" and be explicit about the exact type of component you want to change.
When you set a color in your Button (e.g. <Button color='primary'), how the text color is applied depend on the variant of the Button:
text | outlined: Use the main color (e.g. primary.main) as the text color.
contained: Use the contrastText color as the text color and main color as the background color.
import { blue, yellow } from '#mui/material/colors';
import { createTheme, ThemeProvider } from '#mui/material/styles';
const theme = createTheme({
palette: {
primary: {
main: blue[500],
contrastText: yellow[500],
},
},
});
Live Demo
Related Answer
Change primary and secondary colors in MUI
This solution works for me
const theme = createMuiTheme({
overrides: {
MuiButton: {
label: {
color: "#f1f1f1",
},
},
},
This worked for me, e.g. for custom success and error colors:
// themes.ts
import { createTheme, responsiveFontSizes } from '#mui/material/styles';
// Create a theme instance.
let theme = createTheme({
palette: {
primary: {
main: '#F5F5F5', // Used elsewhere
},
success: {
main: '#11C6A9', // custom button color (seafoam green)
contrastText: '#ffffff', // custom button text (white)
},
error: {
main: '#C6112E', // custom button color (red)
},
},
});
theme = responsiveFontSizes(theme);
export default theme;
Then in your .jsx/.tsx just declare the Button color.
Success button:
<Button
variant="contained"
color="success"
>
Success button text
</Button>
And for the red button w/ outline:
<Button
variant="outlined"
color="error">
Danger button text
</Button>
From https://github.com/mui-org/material-ui/blob/master/src/styles/getMuiTheme.js#L200 you can see what can be set in the theme for various components, and on raisedButton you will see that color is the actually the button background and to set the text colour you will need to change textColor property instead.
const theme = getMuiTheme({
raisedButton: {
textColor: '#ffffff', // this should work
primaryTextColor: '#ffffff' // or this if using `primary` style
}
});
Its not exactly intuitive given that CSS color affects text rather than background, and it doesn't even match up with the properties for the component itself http://www.material-ui.com/#/components/raised-button which have props for backgroundColor and labelColor instead!!!
Related
I am trying to prepare global or app level theming.
In theme.js, I have given 'primary' color, which I expect to work on <Typography> as well.
// Demo.js
import * as React from "react";
import { createTheme, ThemeProvider } from "#mui/material/styles";
import { purple } from "#mui/material/colors";
import Button from "#mui/material/Button";
import Typography from "#mui/material/Typography";
const theme = createTheme({
typography: {
color: purple[500]
},
palette: {
primary: {
main: purple[500]
},
secondary: {
main: "#11cb5f"
}
}
});
export default function Palette() {
return (
<ThemeProvider theme={theme}>
<Button>Primary</Button> // primary color is applied ie purple color is applied, though 'color' property is not written.
<Button color="secondary">Secondary</Button> // secondary is applied
<Typography>some text</Typography> // 'primary' color not applied
</ThemeProvider>
);
}
Both <button> get theme colors, and first <Button> gets primary color applied, though property "color" is not given. But <Typography> remains unaffected ie 'primary' color is not applied.
I need like this
<Typography>some text</Typography>
How to apply 'primary' color, without writing 'color' property?
also,
typography: {
color: purple[500]
},
is also, not effecting the text color inside 'typography'.
sandbox link
It's easy enough to achieve it by setting it in your theme in the text like below (see the comment). In this way you can omit the color="primary" in your <Typography> component as primary is the default one.
But also you can set and use the other variations and use them like <Typography color="secondary">Test text</Typography>. Hope the below example is easy to understand.
export const theme = createTheme({
palette: {
primary: {
main: '#2d9cdb',
dark: '#2e85d4',
},
secondary: {
main: '#2e85d4',
},
text: {
primary: '#000000', // Here is where you set your color.
secondary: '#2d9cdb',
},
success: {
main: '#55ab68',
light: '#48C830',
},
warning: {
main: '#f38713',
},
error: {
main: '#ed4a3a',
},
divider: '#707070',
},
...
}
Default value for color in Button component is 'primary' as per the MUI Documentation. And you would want to override color for typography by applying external styles or by doing something as shown below.
const theme = createTheme({
palette: {
primary: {
// Purple and green play nicely together.
main: purple[500]
},
secondary: {
// This is green.A700 as hex.
main: "#11cb5f"
}
},
// Override root typography color.
components: {
MuiTypography: {
styleOverrides: {
root: {
color: purple[500]
}
}
}
}
});
const theme = createTheme({
components: {
MuiTypography: {
defaultProps: {
color: 'primary'
}
}
}
})
the key is defaultProps.
This is how you override default props of MUI components.
I want to set the primary and secondary color in my theme.js/theme.ts. Is there any way to do that?
I am used to work with Material UI components in my React projects.
It is possible to set a palette with 'primary' and 'secondary' there.
I mean something like this:
export const theme = createTheme({
palette: {
primary: {
main: "#7bb9e8"
},
secondary: {
main: "#eb7f7f"
}
}
})
According to this section: https://chakra-ui.com/docs/styled-system/theming/customize-theme#using-theme-extensions
You don't have to pass color='primary' everywhere.
Just create custom theme and then set primary as default color.
This example shows how to set Cyan as default color for every component.
const theme = extendTheme(
{
colors: {
primary: baseTheme.colors.cyan
},
breakpoints,
},
withDefaultColorScheme({
colorScheme: 'primary'
}),
);
As soon as you have defined all color's shades, you can use your custom color as a component's colorSheme:
const theme = extendTheme({
colors: {
primary: {
main: "#7bb9e8",
50: "#e3f2fd",
100: "#bbdefb",
200: "#90caf9",
300: "#64b5f6",
400: "#42a5f5",
500: "#2196f3",
600: "#1e88e5",
700: "#1976d2",
800: "#1565c0",
900: "#0d47a1"
}
}
});
And your component :
<Button colorScheme="primary">Button</Button>
Demo
You can customize the theme in any possible way :
import { extendTheme } from "#chakra-ui/react";
const theme = extendTheme({
colors: {
primary: {
main: "#7bb9e8"
},
secondary: {
main: "#eb7f7f"
}
}
});
Then pass this theme to ChakraProvider
//index.js
<ChakraProvider theme={theme}>
<App />
</ChakraProvider>
And you will be able to use your own colors accessing the theme via useTheme hook:
import { useTheme } from "#chakra-ui/react";
...
const theme = useTheme();
...
<Button bg={theme.colors.primary.main}>Button</Button>
or override a component styles globally inside the component object
Demo
You can set two variants of primary colors with default and _dark property. It will be used depending on actual color mode (light/dark theme).
import { extendTheme } from '#chakra-ui/react'
const theme = extendTheme({
initialColorMode: 'light',
semanticTokens: {
colors: {
text: {
default: '#16161D',
_dark: '#ade3b8',
},
primary: {
default: '#FF0080',
_dark: '#fbec8f',
},
})
export default theme
I'm creating a theme for an app with createMuiTheme. I'm using material-table and I need to target to this icon of the column table header that is currently sorted:
Watching in developer tools it has the following CSS selectors:
.MuiTableSortLabel-root.MuiTableSortLabel-active.MuiTableSortLabel-root.MuiTableSortLabel-active .MuiTableSortLabel-icon {
color: rgba(0, 0, 0, 0.54);
opacity: 1;
}
How can i do that in the createMuiTheme object from this?
const theme = createMuiTheme({
overrides : {
MuiTableSortLabel: {
root: {
//i want to modify the icon color
color: blue
}
}
}
})
When you are uncertain how to override the default styles, the best resource is to look at how the default styles are defined. The browser developer tools will show you the end result, and the source code will show you the approach to use to generate CSS with similar specificity.
Below is the relevant code from TableSortLabel that controls the icon color:
export const styles = theme => ({
/* Styles applied to the root element. */
root: {
'&$active': {
// && instead of & is a workaround for https://github.com/cssinjs/jss/issues/1045
'&& $icon': {
opacity: 1,
color: theme.palette.text.secondary,
},
},
}
});
You can use very similar syntax for the theme override:
const theme = createMuiTheme({
overrides: {
MuiTableSortLabel: {
root: {
"&$active": {
"&& $icon": {
color: "blue"
}
}
}
}
}
});
Relevant JSS Documentation: https://cssinjs.org/jss-plugin-nested/?v=v10.0.3#use-rulename-to-reference-a-local-rule-within-the-same-style-sheet
I'm currently customizing a few components using global theme overrides in the hopes of maintaining as much of the integrity of the Material-UI theming engine as possible. I know I could accomplish what I'm trying to do using composition, but I want to see if it's possible to achieve this via overrides.
The Goal
Change the background color of the BottomNavigation component to use the primary color of the current theme, and ensure the label gets a color that is legible on top of that background color.
My Current Approach
const theme = createMuiTheme({
palette: {
primary: {
main: 'rgba(217,102,102,1)'
}
},
overrides: {
MuiBottomNavigation: {
root: {
backgroundColor: 'rgba(217,102,102,1)'
}
},
MuiBottomNavigationAction: {
wrapper: {
color: '#fff'
}
}
}
});
This code accomplishes the task and turns the bottom navigation red and the label/icons white. However, I want the flexibility of being able to change the primary color in the palette and have the component update accordingly.
What I'm Trying To Do
const theme = createMuiTheme({
palette: {
primary: {
main: 'rgba(217,102,102,1)'
}
},
overrides: {
MuiBottomNavigation: {
root: {
backgroundColor: 'primary.main'
}
},
MuiBottomNavigationAction: {
wrapper: {
color: 'primary.contrastText'
}
}
}
});
In this way I could easily update the primary color and not have to worry about changing every reference to it across my overrides. I realize I could extract the rgba value out into a const and that would accomplish part of my goal, but I don't see how I could access something as useful as contrastText in case I choose a much lighter primary color.
So - does anyone know of a way to reference theme palette colors in a theme override definition? Any help would be greatly appreciated!
There's another approach here. createMuiTheme accepts any number of additional theme objects to be merged together.
With that in mind you could replicate your accepted answer without having two different ThemeProvider. And if you move the theme definition to its own module, it won't be recreated on each render.
import { createMuiTheme } from "#material-ui/core/styles";
const globalTheme = createMuiTheme({
palette: {
primary: {
main: "rgba(217,255,102,1)"
}
}
});
const theme = createMuiTheme(
{
overrides: {
MuiButton: {
root: {
backgroundColor: globalTheme.palette.primary.main
},
label: {
color: globalTheme.palette.primary.contrastText
}
}
}
},
globalTheme
);
export default theme;
I updated the CodeSandBox to reflect this.
Ill provide two solutions- one is more readable and maintainable, and one has better performance.
The readable and maintainable approach:
Create nested themes.
One theme will be for defining the palette, and one theme will be for overrides.
Because its two themes, you can access the palette theme from overrides theme:
const globalTheme = createMuiTheme({
palette: {
primary: {
main: 'rgba(217,255,102,1)'
}
},
});
const overridesTheme = createMuiTheme({
overrides: {
MuiButton: {
root: {
backgroundColor: globalTheme.palette.primary.main,
},
label: {
color:globalTheme.palette.primary.contrastText,
}
},
}
})
You can refer to this CodeSandbox
This approach doesn't have good performance, bacause every render a new CSS object will be computed and injected
The better performance approach:
First you create an Mui theme skeleton, with the palette.
After it has been created, you add the styles that rely on the palette (notice how I have to use the spread operator a lot to avoid deleting styles):
const theme = createMuiTheme({
palette: {
primary: {
main: 'rgba(217,255,102,1)'
}
},
})
theme.overrides = {
...theme.overrides,
MuiButton: {
...theme.MuiButton,
root: {
...theme.root,
backgroundColor: theme.palette.primary.main,
},
label: {
...theme.label,
color:theme.palette.primary.contrastText,
}
},
}
You can refer to this CodeSandbox
There's a new way to do it in MUI v5 that is much more straightforward. Suppose you want to override the background color of an Mui Button. Inside the custom theme object, you specify this:
const customTheme = createTheme({
components: {
MuiButton: {
styleOverrides: {
root: {
backgroundColor: 'red',
},
},
},
},
});
This is the usual case for hard coding a value. Now, if you wanted to use some property like the primary color from the custom theme you just made, you can pass an arrow function to the root(or whatever component you need to override) with an object as the argument, and return an object containing the styles you need. You can access the theme inside the returned object.
const customTheme = createTheme({
palette: {
primary: {
main: '#002255',
},
},
components: {
MuiButton: {
styleOverrides: {
root: ({ theme }) => ({
backgroundColor: theme.palette.primary.main,
}),
},
},
});
As you can see, inside the object you can destructure the theme. Similarly you can destructure ownerState, which contains all the props and state of the component, which you can access using dot operator.
To illustrate, I have declared a prop called dark on an Mui Button Component
<Button dark={true}>Random Button</Button>
Now I can access this prop in the button, using the object destructuring
const customTheme = createTheme({
palette: {
primary: {
main: '#002255',
},
},
components: {
MuiButton: {
styleOverrides: {
root: ({ ownerState, theme }) => ({
backgroundColor: ownerState.dark
? theme.palette.primary.dark
: theme.palette.primary.light,
}),
},
},
});
For people looking at this for answers on theming and switching between themes.
https://codesandbox.io/s/material-theme-switching-with-pallete-colors-vfdhn
Create two Theme objects and switch between them. Use the same theme property between them so all your overrides can use the same palette to ensure things are not repeated and we use the overrides completely.
import { createMuiTheme } from "#material-ui/core/styles";
let globalTheme = createMuiTheme({
palette: {
primary: {
main: "#fa4616"
}
}
});
export const LightTheme = createMuiTheme(
{
overrides: {
MuiButton: {
root: {
backgroundColor: globalTheme.palette.primary.main
},
label: {
color: globalTheme.palette.primary.contrastText
}
}
}
},
globalTheme
);
globalTheme = createMuiTheme({
palette: {
primary: {
main: "#0067df"
}
}
});
export const DarkTheme = createMuiTheme(
{
overrides: {
MuiButton: {
root: {
backgroundColor: globalTheme.palette.primary.main
},
label: {
color: globalTheme.palette.primary.contrastText
}
}
}
},
globalTheme
);
How can I change the default color ?
What is the object I need to modify in the theme.js ?
EDIT
I want to modify the defaut (grey color) who isn't primary or secondary or error.
I came across a similar issue to the OP, specifically I wanted to change the default Button color from grey to white. The question commenters are correct, each component has its own specific styles and colors versus a global default color. These need to be overridden via a custom theme. Below is an example of overriding Button's default class contained, by creating a theme override to change the default button color. CONTANTS is used to define the specific colors, etc.
import React from 'react';
import { createMuiTheme } from '#material-ui/core/styles';
import { ThemeProvider } from '#material-ui/styles';
import * as CONSTANTS from './Constants'
const theme = createMuiTheme({
palette: {
primary: {
// light: will be calculated from palette.primary.main,
main: CONSTANTS.BLUE,
// dark: will be calculated from palette.primary.main,
contrastText: CONSTANTS.CONTRAST_TEXT,
},
},
overrides:{
MuiButton:{
contained:{
color: CONSTANTS.BLUE,
backgroundColor: CONSTANTS.CONTRAST_TEXT,
'&:hover': {
backgroundColor: CONSTANTS.LIGHT_BLUE,
// Reset on touch devices, it doesn't add specificity
'#media (hover: none)': {
backgroundColor: CONSTANTS.CONTRAST_TEXT,
},
}
}
}
}
});
interface IThemeProps{
children:any;
}
export default function Theme(props: IThemeProps) {
return (
<ThemeProvider theme={theme}>
{props.children}
</ThemeProvider>
);
}
And to use the new theme:
import React from 'react';
import Theme from './Theme';
import { Header, Home } from './Components';
const App: React.FC = () => {
return (
<Theme>
<Header>
<Home />
</Header>
</Theme>
);
}
export default App;
const customTheme = createTheme({
components: {
MuiButton: {
styleOverrides: {
outlined: {
color: "red",
},
},
},
},
});
I found on Material UI documentation about overriding the default properties
You need to modify the palette object in the theme.
`palette: {
primary: {
light: palette.primary[300],
main: palette.primary[500],
dark: palette.primary[700],
contrastText: getContrastText(palette.primary[500]),
},
secondary: {
light: palette.secondary.A200,
main: palette.secondary.A400,
dark: palette.secondary.A700,
contrastText: getContrastText(palette.secondary.A400),
},enter code here
error: {
light: palette.error[300],
main: palette.error[500],
dark: palette.error[700],
contrastText: getContrastText(palette.error[500]),
},
},`