Material UI V5 Theming Customization - reactjs

How can i override this css of material ui ? i want to change de color to : white, becouse when i try to write on my TextField the color is black

If you want just the text color to be white
You can add sx prop for the TextField.
<TextField
variant="outlined"
sx={{
'& .MuiInputBase-input': {
color: 'white',
},
}}
/>
If you want to customize the default MUI theme
Import createTheme and ThemeProvider.
import { createTheme, ThemeProvider } from '#mui/material/styles';
Override theme.
(You can go to https://mui.com/customization/default-theme/ and find exactly what to override.)
const theme = createTheme({
palette: {
common: {
white: '#FFFAFA'
},
}
})
Wrap Your Components or the TextField with ThemeProvider.
<ThemeProvider theme={theme} >
<TextField
variant="outlined"
sx={{
'& .MuiInputBase-input': {
color: 'white',
},
}}
/>
</ThemeProvider>
The color 'white' in the TextField will be the white color that you customized.
You can also define a color palette for the application.
Refer mui.com/customization/theming for more details.
Here is a youtube Tutorial for this
https://www.youtube.com/watch?v=xIIJfmDnvPE&ab_channel=TheNetNinja

If your custom style is overridden by Material UI you can just add !important; after each one of your custom styles.

You can the sx on the props of the object:
<TextField id="outlined-basic" sx={{background:'red'}} label="Outlined" variant="outlined" />
follow the link to see the props of sx: https://mui.com/system/the-sx-prop/

See the docs for overriding nested component styles.
For TextField that would be:
<TextField
variant="outlined"
sx={{
'& .MuiInputBase-input': {
color: 'white',
},
}}
/>)

Related

React - MUI5 - Overriding global theme for InputBase changes all other textfield styles

I am trying to override the style of all <InputBase /> components in my global theme.
MuiInputBase: {
styleOverrides: {
root: {
"&.MuiInputBase-root.Mui-focused": {
borderBottom: "1px solid #2660BE",
},
"&.MuiInputBase-root.Mui-error": {
borderBottom: "1px solid #EB0202",
color: "#EB0202",
},
},
},
},
But I notice that it is overriding the styles of even <TextField /> and <OutlinedInput /> components. Is there any way to override JUST <InputBase /> in global theming? Or should I use styled components?
Yes, you can include the :not pseudo class
https://developer.mozilla.org/en-US/docs/Web/CSS/:not
to exclude all other classes that derive from InputBase, e.g.
.MuiInputBase-input:not(.MuiOutlinedInput-input):not(.MuiTextField-root)

React Material UI Change Height of Textfield

How do I properly change the height of React Material UI v4 Textfield?
The following code messes up the label. Looking for a simple and easy method.
export const useTextFieldStyles = makeStyles({
root: {
minHeight: '42px',
},
});
<TextField
{...params}
margin="dense"
value={listItem}
InputProps={{
classes: {
input: textFieldStyles.root,
},
}}
/>
Referring to this link: Set TextField height material-ui
You can do this with changing style of mui component in your style classes like this
MuiInputLabel-outlined.MuiInputLabel-shrink
{
padding: .3rem !important;
transform: translate(-14px, -14px) scale(0.7) !important;
}
You can change my params

MaterialUI v5 -> How to style Autocomplete options

I'm trying to apply some basic styles to the options inside the Autocomplete component from MUI v5. I'm just trying to change the background color on hover, and based on whether or not it is selected. I've tried 2 approaches based on the documentation, using a theme, and applying the sx prop to Autocomplete.
Using Theme almost has me there, code below:
import { createTheme, ThemeProvider } from '#mui/material/styles';
const theme = createTheme({
components: {
MuiAutocomplete: {
styleOverrides: {
option: {
'&[aria-selected="true"]': {
backgroundColor: '#e3abed',
},
'&:hover': {
backgroundColor: '#9c27b0',
},
backgroundColor: '#fff',
},
},
},
},
})
I have the ThemeProvider wrapped around my entire app
and the component:
<Autocomplete
options={['1', '2', '3']}
renderInput={(params) => <TextField {...params} label="Priority" />}
onChange={(_e, val) => setPriority(val)}
/>
So, this almost works. However the [aria-selected="true"] styling is only being applied when I am also hovering over another option in the dropdown. Otherwise it's the default grey that comes with the component and I don't understand why.
The second path I have tried is to use the sx prop on the Autocomplete component. In the docs it says you can target child components by their class name: https://mui.com/material-ui/customization/how-to-customize/#overriding-styles-with-class-names
Here is my component:
<Autocomplete
options={['1', '2', '3']}
renderInput={(params) => <TextField {...params} label="Priority" />}
onChange={(_e, val) => setPriority(val)}
sx={{
'& .MuiAutocomplete-option': {
backgroundColor: '#000',
},
'& .Mui-focused': {
backgroundColor: 'red',
},
}}
open
/>
That doesn't appear to be having any effect. I've been working on this for almost 2 hours and can't seem to get to the finish line here. Any help would be greatly appreciated.
I had the same problem; turns out I just needed to explore the Autocomplete API docs' Props section a bit further. There are several customization possibilities if you don't want to deal with global theme customization:
The PaperComponent prop allows customization of "[t]he component used to render the body of the popup." Note that simply using sx on Autocomplete does not work here because (as #bnays-mhz pointed out) the PopperComponent that the PaperComponent is nested in lives outside the main DOM tree (for z-index/modal UX purposes).
The renderGroup prop allows overriding the rendering of option group headers.
The renderOption prop allows overriding the rendering of individual options.
function CustomPaper({ children }) {
return (
<Paper
sx={{
"& .MuiAutocomplete-listbox": {
"& .MuiAutocomplete-option[aria-selected='true']": {
bgcolor: "purple",
"&.Mui-focused": {
bgcolor: "purple",
}
}
},
"& .MuiAutocomplete-listbox .MuiAutocomplete-option.Mui-focused": {
bgcolor: "red",
}
}}
>
{children}
</Paper>
);
}
Following up on Lars' answer, here's an example using a custom Paper component. Just pass in the custom Paper component name to the PaperComponent prop on Autocomplete <Autocomplete PaperComponent={CustomPaper} {...blahBlahOtherProps} />. This way is nice if you don't want to override the theme for all Autocomplete components.
You can override Autocomplete options css by targeting class
'.MuiAutocomplete-popper'
You will have to apply css globally because this node is created outside the root element in DOM.
I too faced this issue and found a solution.
You Can try this to set the css for options, single option and while hovered (focused) in the Autocomplete using 'sx' prop
Easy Way to customize your AutoComplete component which can be used in Mui V5
<Autocomplete
limitTags={1}
disablePortal
id="simple-search"
value={select.region}
onChange={handleChange("region")}
options={region}
sx={{
width: { sm: "100%", md: 340 },
"& + .MuiAutocomplete-popper .MuiAutocomplete-option":
{
backgroundColor: "#363636",
},
"& + .MuiAutocomplete-popper .MuiAutocomplete-option[aria-selected='true']":
{
backgroundColor: "#4396e6",
},
"& + .MuiAutocomplete-popper .MuiAutocomplete-option[aria-selected ='true'] .Mui-focused":
{
backgroundColor: "#3878b4",
},
}}
disableCloseOnSelect
multiple
renderInput={(params) => (
<TextField {...params} label="Region" color="info" />
)}
/>

Change Color of Text Field from Text Field in Material UI v5

I am Using MUI and did everything to change the Color of the Text in a MUI Text Field and/or its background Color.
I followed the Documentation and tried:
const CssTextField = styled(TextField)({
And also Things inside the Component, such as
InputProps={{
style: { color: "red" }
}}
or
InputProps={{
color: "red"
}}
Nothing works for me and I donĀ“t know why.
I hope that you can help me.
According to docs, InputProps accepts:
Props applied to the Input element. It will be a FilledInput, OutlinedInput or Input component depending on the variant prop value.
Therefore, style: { color: "red" } doesn't work because the aforementioned components don't accept the style prop and color prop accepts theme colors such as secondary , error etc.
You can customize the color and background of your TextField with sx prop:
<TextField
id="outlined-basic"
variant="outlined"
sx={{
input: {
color: "red",
background: "green"
}
}}
/>
Can you try
InputProps={{
backgroundColor: "red"
}}
instead of
InputProps={{
color: "red"
}}
Maybe
// Option 1
<TextField style ={{width: '100%'}} />
// Option 2
<TextField fullWidth />

Material UI | How to change the font colour of a disabled input text field?

The colour of a disabled input text field created using material UI is light grey by default and it is not very visible against a white background. Is there any way to change the font colour of a disabled input text field?
Below is an example of how to do this showing the customized version next to the default styling.
import React from "react";
import { withStyles } from "#material-ui/core/styles";
import TextField from "#material-ui/core/TextField";
import Button from "#material-ui/core/Button";
const DarkerDisabledTextField = withStyles({
root: {
marginRight: 8,
"& .MuiInputBase-root.Mui-disabled": {
color: "rgba(0, 0, 0, 0.6)" // (default alpha is 0.38)
}
}
})(TextField);
export default function Demo() {
const [disabled, setDisabled] = React.useState(true);
return (
<>
<Button onClick={() => setDisabled(!disabled)}>Toggle Disabled</Button>
<br />
<br />
<DarkerDisabledTextField
disabled={disabled}
id="outlined-basic"
label="Custom"
value={`Disabled = ${disabled}`}
variant="outlined"
/>
<TextField
disabled={disabled}
id="outlined-basic"
label="Default"
value={`Disabled = ${disabled}`}
variant="outlined"
/>
</>
);
}
I think the simplest way is to create an object to define the font color and pass it to the TextField's inputProps.
const fontColor = {
style: { color: 'rgb(50, 50, 50)' }
}
This way you can toggle the font with the components state as you wish or simply keep it constant.
<TextField
label="Title"
onChange={updateTitle}
value={title}
disabled={true}
inputProps={fontColor}/>
import { TextField, styled } from '#mui/material'
const CustomTextField = styled(TextField)({
'& .MuiInputBase-root.Mui-disabled': {
backgroundColor: '#f0f0f0',
},
});
Then use them as standard component
<CustomTextField
name="manual"
label="Manual"
size="small"
disabled={watch({}).platform === 'manual' ? false : true}
/>
if u use RHF with controller, mean u create custom TextField using RHF, u just change the component inside the styled()
For example:
import RHFTextField from "../RHFTextField"
const CustomTextField = styled(RHFTextField)({
'& .MuiInputBase-root.Mui-disabled': {
backgroundColor: '#f0f0f0',
},
});
with background color changes, it will more visible..
In the Mui-v5 above solution is not working.
Below are the solutions for Mui-v5.
Solution 1:
const styles = theme => ({
input: {
"& input.Mui-disabled": {
color: "green"
}
}
});
Solution: 2 (use sx property for the component)
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}
eg.
<TextField
fullWidth
disabled=true
variant="outlined"
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}
/>
None of all the many answers online worked for me, using MUI v4.
Finally I found a very easy but hacky solution for setting the font color of a disabled TextField component.
The problem is, (at least for Firefox) it will use this weird style cheet:
.css-[...]-MuiInputBase-input-MuiOutlinedInput-input.Mui-disabled {
opacity: 1;
-webkit-text-fill-color: rgba(0, 0, 0, 0.38);
}
I'm too new to this stuff and I don't know how to change it, so my way was to overwrite it:
.css-[...]-MuiFormControl-root-MuiTextField-root input {
-webkit-text-fill-color: rgba(255,255,255,0.6) !important;
color: rgba(255,255,255,0.6);
}
The !important part is important!
In React with MUI v4, it's as simple as that:
const textFieldColor = /*...*/;
const textFieldSX = {
input: {
"-webkit-text-fill-color": `${textFieldColor} !important`,
color: `${textFieldColor} !important`,
},
};
/*...*/
<TextField sx={textFieldSX} />
Just in case, this -webkit-text-fill-color property would turn into color one time, one can use an !important there too.
works (MUIv5):
sx={{
"& .MuiInputBase-input.Mui-disabled": {
WebkitTextFillColor: "#000000",
},
}}

Resources