Material UI TextField Style Override Using Theme - reactjs

I have a TextField: https://mui.com/components/text-fields/
<TextField
fullWidth
type={'email'}
variant={'standard'}
label={'Email'}/>
My background is dark so I'm trying to override the default colours using my theme. I have been able to change the text color and the bottom border when in focus to white but I cannot figure out how to change the bottom border to white when out of focus.
Not in focus:
In Focus:
Here is what my theme looks like:
const lightTheme: ThemeOptions = createTheme({
palette: {
primary: {
main: '#ffffff',
},
},
typography: {
allVariants: {
color: 'white'
},
},
components: {
MuiInputBase: {
styleOverrides: {
input: {
color: 'white',
}
}
}
}
});
I'm also having a hard time finding documentation on which styles are available and what they do in the Material UI docs. Perhaps in addition to helping me solve this particular problem someone can point me to a good spot in the docs for references whenever I want to change the styles on any Material UI component.

If you inspect and check the style through the browser, then you will realize the border bottom is applied for the ::before pseudo element MuiInput-root class.
Like I said, I guess it will work if you pass the same to the pseudo element into your theme.
const lightTheme: ThemeOptions = createTheme({
palette: {
primary: {
main: '#ffffff',
},
},
typography: {
allVariants: {
color: 'white'
},
},
components: {
MuiInputBase: {
styleOverrides: {
input: {
color: 'white',
'&::before': {
border-bottom: 1px solid rgba(0, 0, 0, 0.42); // use your color
}
}
}
}
}
});

Related

Add alpha for all variants in MUI Theme - React

I have a MUI theme setup and I would like to add an alpha color to MuiChip. I am not sure on how to access the specific color inside createTheme. For example, if my primaryColor is '#FF0000' and the secondary color is '#00ff00', just for MuiChip, I want to do backgroundColor: alpha(theme.palette, 0.1); If I pass a variant prop for the chip component, the backgroundColor should be changed to the alpha version of that variant.
export const theme = createTheme({
palette: {
primary: {
main: colors.primary,
},
secondary: {
main: colors.secondary,
},
error: {
main: colors.red,
},
},
components: {
MuiChip: {
styleOverrides: {
colorPrimary: {
color: 'white',
},
root: {
backgroundColor: alpha(theme.palette, 0.1)
}
},
},
},
});
Please advice.
As I can see your theme you misunderstood how MUI works with themes. I see this main problems:
If you use just MUI colors.primary is not valid. You must use exact color code like #fff. (not true if you use some kind of color lib)
in styleOverrides you must use the template from the doc here. With this you will get theme and ownerState to manipulate components on CSS level.
theme.palette is not the exact color reference you should use theme.palette.primary.main in alpha()
You can use alpha but I think opacity is easier
Just for information for other readers here we can find CSS reference of Chip component for Rule Names to override.
I made a working code on Codesandbox, you can try it.
Here is my theme to solve the problem:
export const theme = createTheme({
palette: {
primary: {
main: "#a0a"
},
secondary: {
main: "#0a0"
},
error: {
main: "#a00"
}
},
components: {
MuiChip: {
styleOverrides: {
root: {
color: "#0a0"
},
colorSuccess: ({ ownerState, theme }) => ({
...(ownerState.variant === "custom" && {
backgroundColor: alpha(theme.palette.primary.main, 0.3)
})
})
}
}
}
});

How to deduce React styleOverrides for Material-UI components?

I'm trying to customize the color of my MUI5 TextField component. Simply overriding the primary and secondary colors is not enough, so I'm striving for more control over the colors.
The component:
<TextField
label='Username'
variant='outlined'
onChange={(e) => setToken(e.target.value)}
value={token}
size='small'
/>
This is the theme that I use:
const theme = createTheme({
components: {
MuiTextField: {
styleOverrides: {
root: {
'.MuiOutlinedInput-root': {
'.MuiOutlinedInput-notchedOutline': {
borderColor: 'pink',
},
'&:hover .MuiOutlinedInput-notchedOutline': {
borderColor: 'pink',
},
},
},
},
},
MuiInputLabel: {
styleOverrides: {
root: {
color: 'pink',
'&.Mui-focused': {
color: 'pink',
},
},
},
},
},
});
These style overrides are enough to control the color of
the label (Username) when the TextField is focused and unfocused
the border color when the TextField is unfocused or (focused AND hovered over)
The problem is that I haven't been able to change the border color when the TextField is active and not hovered over. This is the style that is applied on it: css-9ddj71-MuiInputBase-root-MuiOutlinedInput-root.Mui-focused.MuiOutlinedInput-notchedOutline
I tried different combinations of style overrides in the custom theme but no luck so far.
Can someone tell me how to change the border color in the above case?
Maybe I should build a component with more basic components if I want such fine-grained control over colors?
try this:
styleOverrides: {
root: {
'&.Mui-focused': {
'.MuiOutlinedInput-notchedOutline': {
borderColor: 'green',
},
},
'&.Mui-error': {
'.MuiOutlinedInput-notchedOutline': {
borderColor: 'red',
},
},
},
}

How can I change MUI v5 Button to lighten on hover instead of darken using theme?

I have setup my theme as outlined in the docs.
palette: {
primary: {
main: customColor
}
}
When I hover, my mui button darkens based on the main color, but I want it to lighten based on the same color (without having to hard code a value anywhere). How can I achieve this?
Update:
Based on Prashant Jangam's answer I was able to get everything exactly as I needed it. See code below:
components: {
MuiButton: {
styleOverrides: {
root: ({ theme }) => ({
'&.MuiButton-containedPrimary:hover': {
backgroundColor: theme.palette.primary.light,
},
'&.MuiButton-containedSecondary:hover': {
backgroundColor: theme.palette.secondary.light,
},
}),
},
},
}
check the global component overide section for more details https://mui.com/customization/theme-components/#global-style-overrides
following code with give you start. here I have changed opacity on button hover state.
const theme = createTheme({
// your other theme settings here
components: {
MuiButton: {
styleOverrides: {
root: ({ ownerState }) => ({
'&:hover': {
opacity:0.5,
},
}),
},
},
},
});
I customize it like so:
const theme = createTheme({
get components() {
return {
MuiButton: {
styleOverrides: {
root: ({ ownerState }) => ({
'&:hover': {
backgroundColor: this.palette[ownerState.color].light,
transition: '0.2s',
},
}),
},
},
}
},
palette: {
primary: {
main: '#3E4BA0',
dark: '#313b7d',
light: '#5664bd',
},
},
})
At least on the version 4 of MUI the palette.primary.dark is the color that is used for hovering the button. Just replace the value of the dark property with the value that you want (hex color or rgb/rgba).
Note: It can also change some hovering colors on another components
const baseTheme = createMuiTheme({
palette: {
primary: {
main: '#4B882F',
dark: '#345F20', // <-------- rgba(75, 136, 47, 0.2)
light: '#6F9F58'
}
}
})
You can't use it like this because the baseTheme was not defined yet.
const baseTheme = createMuiTheme({
palette: {
primary: {
main: '#4B882F',
dark: baseTheme.palette.primary.light,
light: '#6F9F58'
}
}
})

In Material UI, How can I override a selector selected component style?

In Material UI, to extend the distance between MuiInputLabel and MuiInput, I have to override the marginTop of label + .MuiInput-formControl.
However, createMuiTheme's override only provide direct override of a Mui Component CSS, such as:
createMuiTheme({
overrides: {
MuiInput: {
formControl: {
marginTop: '1.5rem',
},
},
}
})
How can I do something like:
createMuiTheme({
overrides: {
'label+MuiInput': {
formControl: {
marginTop: '1.5rem',
},
},
}
})
Thanks...
Here's the relevant JSS documentation:
https://cssinjs.org/jss-plugin-nested?v=v10.0.0-alpha.10#use--to-reference-selector-of-the-parent-rule
Here's the syntax you need:
const theme = createMuiTheme({
overrides: {
MuiInput: {
formControl: {
"label + &": {
marginTop: "1.5rem"
}
}
}
}
});
Here's a working example:

material-ui v.1 - define component's background with theme

I'm using material-ui v.1 library for React, and want to set background of Paper components.
I want to do that using themes.
I'm using top level
const theme = createMuiTheme({
palette: {
//type: 'light'
}
});
<MuiThemeProvider theme={theme}>
</MuiThemeProvider>
What should I put in createMuiTheme to do that?
I've tried several options (like palette:{paper: {backgroundColor: 'black'}}, and paper: {backgroundColor: 'black'}, and backgroundColor: {paper: 'black'}) but nothing works.
NOTE: type: 'light', type: 'dark' works fine.
According to https://material-ui-next.com/customization/themes/, the following property should be used :
const theme = createMuiTheme({
palette: {
background: {
paper: 'red'
}
}
});
If you take a look at the themes documentation you will see:
{
"palette": {
"shades": {
"light": {
"background": {
"paper": "#fff",
},
},
},
},
}

Resources