How to disable Material-UI contrast ratio console errors? - reactjs

My console is full of
Material-UI: the contrast ratio of 2.590660811372324:1 for #fff on #5EB44B
falls below the WCAG recommended absolute minimum contrast ratio of 3:1.
error messages which I would like to hide. Is there a way how to do that? I've done the research but could find anything useful.
Here is my createMuiTheme code
createMuiTheme({
themeName: 'radovix',
palette: {
primary: {
main: '#5EB44B'
}
},
contrastThreshold: 2
});
Note, I'm aware that best approach would be changing the colors that I'm using and therefore solving the error, but that is not possible in my case.

This console error is produced by getContrastText which is used whenever a contrastText color is not explicitly specified.
Instead of specifying contrastThreshold: 2, I would recommend explicitly specifying the desired contrastText color for those cases where the default contrastThreshold of 3 does not pick the one you want. Customization of contrastThreshold is really only intended for increasing the threshold -- not for decreasing it.
Here's a working example:
import React from "react";
import { createMuiTheme, ThemeProvider } from "#material-ui/core/styles";
import Button from "#material-ui/core/Button";
import { common } from "#material-ui/core/colors";
const theme = createMuiTheme({
palette: {
primary: {
main: "#5EB44B",
contrastText: common.white
}
}
});
export default function App() {
return (
<ThemeProvider theme={theme}>
<Button color="primary" variant="contained">
Primary Color Button
</Button>
</ThemeProvider>
);
}

Related

Make MUI Checkbox border color as gradient

I am writing styleOverrides to MuiCheckBox and want to make its borders as linear gradient. Any ideas how to make it possible?
MuiCheckbox: {
styleOverrides: {
root: {
color: 'linear-gradient(89.38deg, #957947 -13.88%, #E1BC6C 27.59%, #EFDB7C 66.54%, #E9BA6A 105.86%)',
backgroundColor: 'transparent',
}
}
You can add borderColor property to your root like this,
root:{
borderColor: 'linear-gradient()'
}
Something I use and I am converting our whole app to in order to do the migration from MUI 4 to 5, is component overrides.
So say you have a or component. Basically in like a styled.js file, you can do:
import { withStyles } from '#material-ui/core';
import { MenuItem } from '#material-ui/core';
const MenuItemStyled = withStyles((theme) => ({
root: {
borderColor: 'yourGradient'
}
})(MenuItem);
export { MenuItemStyled }
(in jsx file like 'index.js')
import MenuItemStyled from './styled';
import Menu from '#material-ui/core';
<Menu>
<MenuItemStyled value={blah}>blah</MenuItemStyled>
<MenuItemStyled value={blahblah}>blahblah</MenuItemStyled>
</Menu>
I find this gives you more root access to component styles without having to dig through specific classnames in the inspector. When overriding components like this is checks for your custom styles first before render and it just makes everything significantly easier. Plus in MUI 5 useStyles is depricated, so you'll want to do it this way regardless in order to keep your app current.

Set MUI Theme for Specific React Page

We are using MUI with our React site and currently have light & dark themes. I would like to force the light theme on a specific page (log in). For some reason, I cannot seem to find a solution anywhere online. How can I go about doing this?
import { createTheme, ThemeProvider } from '#material-ui/core';
const loginTheme = createTheme({
palette: {
background: {
default: '#303030',
paper: '#424242'
},
},
});
<ThemeProvider theme={loginTheme}>
wrap the content you want to use the above theme
</ThemeProvider>

Extending Material UI's existing dark mode colors

Material UI's default theme ships a palette of colors, including a special set of dark colors (docs, code).
What makes these dark mode colors special is components who consume them don't need to depend on knowing the theme's palette.mode (aka light/dark mode) - they update automatically.
codesandbox demo
My goal is to extend this set of colors, such that components I write can use new colors beyond this built-in set, e.g. theme.palette.myColor and benefit from the same automatic behavior.
In other words, I don't want to have dark mode logic be duplicated in each theme-consuming component:
const WhatIDontWantComponent = () => (
<Box
sx={{
color: (theme) =>
theme.palette.mode === "light"
? theme.palette.myColor.light
: theme.palette.myColor.dark,
}}
/>
);
I instead want to use
const WhatIWantComponent = () => (
<Box
sx={{
color: (theme) => theme.palette.myColor
}}
/>
);
So myColor would be included in the light/dark set that already exists, and benefit from this automatic behavior.
Possible? Is there some way to accomplish this within my app without patching MUI in some way to accept custom colors?
deps
#material-ui/core version 5.0.0-beta.4
react, react-dom 17.0.2
next.js 11.0
Ended up going with this:
const baseTheme = createTheme({...common options...})
export const lightTheme = createTheme({ ...light specific...}, baseTheme)
export const darkTheme = createTheme({ mode: "dark", ...dark specific...}, baseTheme)
based on this discussion.
We basically perform theme composition in multiple steps + utilizing how the second arg to createTheme() gets deepmerge'd.
Once you have the two themes, you could consider setting up a toggle by bringing your own mode:
<ThemeProvider theme={mode ? lightTheme : darkTheme}>
{...app...}
</ThemeProvider>
This works, but IMO a downside of this approach though is that I have two themes and have to manage dark/light state externally. Not a huge problem but feels a bit redundant -- in my mind dark/light mode seems like a concern that could (should) be handled internally to a single theme.
Perhaps there are good reasons against, but IMO it feels more ergonomic if MUI would allow users to specify our own dark palette, like I asked in my original question, vs the current hard coded one.
So if you're looking for custom MUI dark colors, this approach seems to be the best one given the situation right now.
You can customize the material-ui theme using theme provider component in order to add your custom colors for e.g,
import { createTheme, colors } from '#material-ui/core/styles';
const theme = createTheme({
palette: {
background: {
default: "#F6F7FF",
paper: colors.common.white
},
primary: {
main: "#43A047"
},
secondary: {
main: "#43A047"
},
text: {
primary: "#000000",
secondary: "#6b778c"
},
// Add your custom colors if any
},
});
You can write this configuration code inside a separate file and then import it into your root component file.
import theme from "src/theme";
import { ThemeProvider } from "#material-ui/core";
<ThemeProvider theme={theme}>
<YourRootComponent />
</ThemeProvider>
Then consume it the same way you do it for the default material UI colors
Also, you can customize typography, shadows, override default class, and much more check out this

Material UI theme override issue

Here is my code, I am trying to override the theme of the Material UI react, But somehow it is not getting override, I am getting default colors only. Here is my code.
I have installed proper libraries also, In package JSON i have added "#material-ui/styles": "^4.11.4", and "#material-ui/core": "4.11.4", Which are required. Cleaned install and restart, It looks simple but still not working.
import {ThemeProvider} from '#material-ui/styles';
import Button from '#material-ui/core/Button';
import React from "react";
function App() {
return (
<ThemeProvider theme={theme}>
<Button color="primary">Primary</Button>
<Button color="secondary">Secondary</Button>
</ThemeProvider>
);
}
const theme = createMuiTheme({
palette: {
primary: {
// Purple and green play nicely together.
main: "#000000",
},
secondary: {
// This is green.A700 as hex.
main: '#11cb5f',
},
},
overrides: {
MuiButton: {
textPrimary: {
color: "#efefef"
},
text: {
color: '#000000',
},
containedPrimary: {
backgroundColor: "#9f9f9f"
}
},
},
});
export default App;
You're missing the createMuiTheme import, and you're importing ThemeProvider from the wrong folder.
Try this:
import { createMuiTheme, ThemeProvider } from '#material-ui/core/styles';
You shouldn't really need the #material-ui/styles package at all. Also, make sure to define theme before using the variable in App.

The optimum way of changing Material UI react theme at runtime

I am new to React and Material UI and still am trying to grasp the composition over inheritance.
I am trying to achieve switching to dark/light theme at runtime in a react app. I have achieved it somehow but with lots of code duplication. I am sure there is a better way.
Here is what I have so far:
Theme.js
import { createMuiTheme } from "#material-ui/core/styles";
export const darkTheme = createMuiTheme({
palette: {
type: "dark",
},
//.....a lot of items
});
export const lightTheme = createMuiTheme({
palette: {
type: "light",
},
//.....duplicating same items as above
});
App.js
import { lightTheme, darkTheme } from "../shared/Theme";
const App = ({
theme
}) => {
return (
<ThemeProvider theme={theme === "dark" ? darkTheme : lightTheme}>
{/*Components...*/}
</ThemeProvider>
);
};
The theme props is getting injected using redux and its doing its job fine. This is workable solution but not the best one.
I found in Material UI documentation that we can have [outer and inner theme provider][1] and I tried doing the following (but it didn't work):
<ThemeProvider theme={…} >
<ThemeProvider theme={outerTheme => ({ darkMode: true, ...outerTheme })}>
{...Component}
</ThemeProvider>
</ThemeProvider>
I am aware of useStyle hook but it just lets you create class names that you can use in your component. What I want is: take a certain section of the existing theme object and replace a property in it.
Any help will is appreciated. Thanks for reading this far!
[1]: https://material-ui.com/styles/advanced/#main-content
"Duplication" is a problem when there is property-value repetition, otherwise is an alternate configuration, that is, repeating objects or their properties are not an issue. If there is no property-value repetition, using separate objects is the way, MUI does it, so you are right on track.
Now, Assuming you meant property-value repetition (shared styling) at:
//.....duplicating same items as above
where there is property-value repetition mixed with alternate configuration within your objects, and based on your goal:
take a certain section of the existing theme object and replace a property in it
Try a factory method:
const createThemeOptions = (isDarkMode)=>({
palette: {
mode: isDarkMode? "dark" : "light", // alternate configuration
},
primary: {
main: indigo['A700'], // property-value repetition(shared styling)
},
secondary: {
main: isDarkMode? deepOrange['A200'] : deepOrange['900'], // alternate configuration
},
// make your other object properties shared or alternate...
});
export const darkTheme = createMuiTheme(createThemeOptions(true));
export const lightTheme = createMuiTheme(createThemeOptions());

Resources