Material-UI Changing default color - reactjs

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]),
},
},`

Related

Typography is not effected with primary colors

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.

react material-ui v5 theming doesnt work with storybook

I spend a few days trying to customize the primary color and add two more colors to the palette. I was able to declare properly the new colors...but at the moment to see those new colors reflected on the button doesnt work. The button are taking the default properties even when I wrapped under the Themeprovider. I'm using storybook.
import React from "react";
import { Meta } from "#storybook/react/types-6-0";
import { Button } from "#mui/material";
import { createTheme, ThemeProvider, styled } from '#mui/material/styles';
const theme = createTheme({
palette: {
primary: {
contrastText: "#ff0000",
light: "#ff0000",
main: "#ff0000",
dark: "#ff0000"
},
tertiary: {
main: "#ff0000"
},
failure: {
main: "#ff0000"
}
}
});
const CustomStyles = () => {
return (
<ThemeProvider theme={theme}>
<Button variant="contained">Click me</Button>
</ThemeProvider>
);
}
const Template = () => {
return (
<CustomStyles />
);
};
export const Default = Template.bind({});
export default {
title: "mylib/Theme"
} as Meta;
This is how it looks
default button style
Themeprovider custom palette
As you may see, the ThemeProvider has the palette color definition...but some how the button doesnt take it.
Thanks in advance
Adding this to.storybook/preview.js was enough to solve my case. Follow the official migration guide on this matter to learn more.
//.storybook/preview.js
import { ThemeProvider } from '#mui/material/styles';
import { ThemeProvider as Emotion10ThemeProvider } from 'emotion-theming';
import { theme } from '../your/system/customTheme/path';
const defaultTheme = theme;
const withThemeProvider = (Story, context) => {
return (
<Emotion10ThemeProvider theme={defaultTheme}>
<ThemeProvider theme={defaultTheme}>
<Story {...context} />
</ThemeProvider>
</Emotion10ThemeProvider>
);
};
export const decorators = [withThemeProvider];
//another storybook exports.
EDIT: this issue seems to be related to stackoverflow.com/a/70254078/17724218 as OP commented below.

How can I set a primary color in Chakra UI theme?

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

Material-UI-next Theme not being applied to circular progress

I have updated my theme according to the docs but the circular progress continues to use the default primary color, not the one specified in the theme. Ultimately I'd like to be able to make the theme adjustable via the end user, using mobx computed values to adjust the theme, but currently I can't even get it to work with a static theme.
import {MuiThemeProvider} from "material-ui/styles"
import Loading from "../common/components/Loading";
import {createMuiTheme} from "material-ui";
const testingTheme = createMuiTheme({
primary: {
main: '#67e2ff',
light: '#a0ffff',
dark: '#1eb0cc',
contrastText: '#3b3f42'
},
secondary: {
main: '#590ce8',
light: '#9549ff',
dark: '#0000b4',
contrastText: '#fefefe'
}});
/**
* The AppContainer handles all of the logical functionality that has to happen at the root level.
* A prime example is the MuiThemeProvider, that must be the parent of all other rendered components.
* To change the muiTheme we use the AppContainer to render the MuiThemeProvider
*/
#inject("uiStore") #observer
export default class AppContainer extends Component {
componentDidMount() {
console.log(this.props.uiStore);
}
render() {
return (
<MuiThemeProvider theme={testingTheme}>
{this.props.uiStore.initialized ? <App/> : <Loading size={10}/>}
</MuiThemeProvider>
)
}
}
My definition for the theme was wrong, I was missing the root palette property.
const testingTheme = createMuiTheme({
palette: {
primary: {
main: '#67e2ff',
light: '#a0ffff',
dark: '#1eb0cc',
contrastText: '#3b3f42'
},
secondary: {
main: '#590ce8',
light: '#9549ff',
dark: '#0000b4',
contrastText: '#fefefe'
}
}
});

MUI - Change Button text color in theme

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

Resources