material-ui how to override nested styles - reactjs

I have a simple Material-UI component
<Chip
avatar={
<Avatar>
<TruckIcon color='primary' />
</Avatar>
}
label={name}
color='primary'
/>
the following styles applied from mui-theme:
.MuiChip-root .MuiChip-avatarColorPrimary {
color: #fff;
background-color: #21349f;
}
The question is how to override it?
I have tried many options in my theme override styles:
MuiChip: {
avatarColorPrimary : {
background-color: red;
}
MuiChip: {
root: {
avatarColorPrimary : {
background-color: red;
}
}
}
MuiChip: {
'& .avatarColorPrimary': {
background-color: red;
}
but none of them is working.
I'm using 4.9.5 version of Material-UI.

The most useful resource when trying to determine the appropriate way to override a style is to look at how the default styles are defined in the source code.
Below is an excerpt of the default styles from the Chip source code:
{
root: {
'& $avatarColorPrimary': {
color: theme.palette.primary.contrastText,
backgroundColor: theme.palette.primary.dark,
}
}
}
Below is an example of overriding the background color in the theme:
import React from "react";
import { createMuiTheme, ThemeProvider } from "#material-ui/core/styles";
import Avatar from "#material-ui/core/Avatar";
import Chip from "#material-ui/core/Chip";
import DoneIcon from "#material-ui/icons/Done";
const theme = createMuiTheme({
overrides: {
MuiChip: {
root: {
"& $avatarColorPrimary": {
backgroundColor: "red"
}
}
}
}
});
export default function Chips() {
return (
<ThemeProvider theme={theme}>
<Chip
avatar={
<Avatar>
<DoneIcon color="primary" />
</Avatar>
}
label="Sample Name"
color="primary"
/>
</ThemeProvider>
);
}

Related

How can use Material ui custom pagination?

<Pagination count={35} variant="outlined" shape="rounded" color="primary"/>
This sets a default background colour and text colour, but I need custom background colour and text colour. I can't change it. I am trying to change the colour but I failed.
import * as React from 'react';
import Pagination from '#mui/material/Pagination';
import Stack from '#mui/material/Stack';
import { styled } from '#mui/system';
//Make a styled pagination component using styled API that overrides default style
const MyPagination = styled(Pagination)({
'& .Mui-selected': {
backgroundColor: 'red',
color:'#19D5C6',
},
"& .MuiPaginationItem-root": {
border: "1px solid red"
}
//There has a lot of Global classes. you have to use it according to your requirement
//Some classes are MuiPaginationItem-root, MuiPaginationItem-page and Mui-selected
});
export default function BasicPagination() {
return (
<Stack spacing={2}>
<MyPagination count={10} />
</Stack>
);
}
click to get all classes name
I would use a theme which will apply consistency throughout your project.
Codesandbox implementation here: https://codesandbox.io/s/mutable-river-e15yxw
Theme.js
import { ThemeProvider, createTheme } from "#mui/material/styles";
import CssBaseline from "#mui/material/CssBaseline";
export default function Theme(props) {
const theme = createTheme({
components: {
MuiPagination: {
styleOverrides: {
root: ({ ownerState }) => ({ backgroundColor: "#ffeeaa" })
}
},
MuiPaginationItem: {
styleOverrides: {
root: ({ ownerState }) => ({ backgroundColor: "#ffcc00", color: "maroon" })
}
}
}
});
return (
<ThemeProvider theme={theme}>
<CssBaseline />
{props.children}
</ThemeProvider>
);
}
Demo.js
import * as React from "react";
import Pagination from "#mui/material/Pagination";
import Stack from "#mui/material/Stack";
import Theme from "./Theme";
export default function BasicPagination() {
return (
<Theme>
<Stack spacing={2}>
<Pagination count={10} sx={{ backgroundColor: "#eeffaa" }} />
<Pagination count={10} color="primary" />
<Pagination count={10} color="secondary" />
<Pagination count={10} disabled />
</Stack>
</Theme>
);
}
And there is another one if you prefer it, utilising the sx prop:
<Pagination
count={35}
shape="rounded"
color="primary"
sx={{
"& .MuiPagination-ul": { backgroundColor: "yellow" },
"& .MuiPaginationItem-page": { color: "red" },
"& .Mui-selected": { backgroundColor: "green" },
}}
/>
This is the result:
EDIT:
<Pagination
count={35}
shape="rounded"
color="primary"
sx={{
"& .MuiPagination-ul": { backgroundColor: "yellow" },
"& .MuiPaginationItem-page": { color: "red", border: "1px solid green" },
"& .Mui-selected": { backgroundColor: "green" },
}}
/>

How to apply styles to MUI MenuList with styled compoents

I'm trying to apply background-color when MenuItem component has the selected={true}
also I'd like to apply a style when MenuItem component hovered.
How can I do that?
import * as React from "react";
import Paper from "#mui/material/Paper";
import MenuList from "#mui/material/MenuList";
import Stack from "#mui/material/Stack";
import { MenuItem } from "./styles";
export default function MenuListComposition() {
return (
<Stack direction="row" spacing={2}>
<Paper>
<MenuList>
<MenuItem selected={true}>Profile</MenuItem>
<MenuItem>My account</MenuItem>
<MenuItem>Logout</MenuItem>
</MenuList>
</Paper>
</Stack>
);
}
styles.js
import styled from "styled-components";
import { default as MuiMenuItem } from "#mui/material/MenuItem";
export const MenuItem = styled(MuiMenuItem)`
.MuiMenuItem-root {
color: blue;
padding: 10px 0;
& .Mui-selected {
background-color: red;
}
&:hover {
background-color: green;
}
}
`;
Solution with styled-components
If you need to use styled-components instead of styled from Mui, you can do it.
export const MenuItem = styled(MuiMenuItem)`
color: blue;
padding: 20px;
&.Mui-selected {
background-color: red;
}
&:hover {
background-color: green;
}
`;
You can fix the problem by importing styled from material and adapt the format, like this:
import { MenuItem as MuiMenuItem, styled } from "#mui/material";
export const MenuItem = styled(MuiMenuItem)({
"&.MuiMenuItem-root": {
color: "blue",
padding: "10px",
"&.Mui-selected": {
backgroundColor: "red"
},
"&:hover": {
backgroundColor: "green"
}
}
});
and then I assume that what you want is to change the color only of the selected MenuItem and you can create the array of objects like in the code so you do not repeat to much code.
import React, { useState } from "react";
import Paper from "#mui/material/Paper";
import MenuList from "#mui/material/MenuList";
import Stack from "#mui/material/Stack";
import { MenuItem } from "./styles";
const ITEMS = [
{ index: 1, title: "Profile" },
{ index: 2, title: "My Account" },
{ index: 3, title: "Logout" }
];
export default function MenuListComposition() {
const [selectedIndex, setSelectedIndex] = useState(1);
const handleListItemClick = (event, index) => {
setSelectedIndex(index);
};
return (
<Stack direction="row" spacing={2}>
<Paper>
<MenuList variant="menu">
{ITEMS.map(({ index, title }) => (
<MenuItem
key={index}
onClick={(event) => handleListItemClick(event, index)}
selected={selectedIndex === index}
>
{title}
</MenuItem>
))}
</MenuList>
</Paper>
</Stack>
);
}
You need to make some configuration changes, because MUI uses 'emotion' as a default style engine, otherwise they would conflict with each other.
https://mui.com/material-ui/guides/styled-engine/#how-to-switch-to-styled-components
here is a working example you need to compare dependencies that are recommended for usage with react

Why this customTheme is not applied to my buttons

Why this customTheme is not applied to my buttons. I'm using the MUI 5. Please tell me where I'm making the mistakes & how to resolve this.
import React from 'react';
import NavBar from './components/NavBar';
import { createTheme, ThemeProvider } from '#mui/material/styles';
import { Button } from '#mui/material';
const customTheme = createTheme({
overrides: {
MuiButton: {
outlined: {
backgroundImage: 'linear-gradient(to top, #d299c2 0%, #fef9d7 100%)',
color: 'green'
},
text: {
color: "red"
}
}
}
})
function App() {
return (
<div id='appDiv2'>
<ThemeProvider theme={customTheme}>
<NavBar />
<Button variant='outlined'> Test Button </Button>
</ThemeProvider>
<DirectionSnackbar />
</div>
);
}
export default App;
The structure of the theme object depends on the version of MaterialUI. If you use latest, the scheme should be
const theme = createTheme({
components: {
// Name of the component
MuiButton: {
styleOverrides: {
// Name of the slot
outlined: {
backgroundImage: 'linear-gradient(to top, #d299c2 0%, #fef9d7 100%)',
color: 'green'
},
text: {
color: "red"
}
},
},
},
});
https://mui.com/customization/theme-components/#global-style-overrides

Custom color to Badge component not working

I need to add a custom color to my Badge component and it does not seem to work.
I have tried these:
<Badge className="badge" badgeStyle={{backgroundColor: '#00AFD7'}} variant="dot" />
<Badge className="badge" color='#00AFD7' variant="dot" />
These do not work. How can I pass a custom color to my Badge component
You can leverage withStyles and use the badge css class to customize this.
Here's an example:
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "#material-ui/core/styles";
import Badge from "#material-ui/core/Badge";
import MailIcon from "#material-ui/icons/Mail";
const styles = theme => ({
margin: {
margin: theme.spacing.unit * 2
},
customBadge: {
backgroundColor: "#00AFD7",
color: "white"
}
});
function SimpleBadge(props) {
const { classes } = props;
return (
<div>
<Badge
classes={{ badge: classes.customBadge }}
className={classes.margin}
badgeContent={10}
>
<MailIcon />
</Badge>
</div>
);
}
SimpleBadge.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(SimpleBadge);
In v4, you can use functions within the styles that leverage props.
Documentation here: https://material-ui.com/styles/basics/#adapting-the-higher-order-component-api
const styles = theme => ({
margin: {
margin: theme.spacing.unit * 2
},
customBadge: {
backgroundColor: props => props.color,
color: "white"
}
});
In MUI v5, you can use either the sx prop:
<Badge
badgeContent={130}
sx={{
"& .MuiBadge-badge": {
color: "lightgreen",
backgroundColor: "green"
}
}}
>
<MailIcon />
</Badge>
Or styled() function:
const StyledBadge = styled(Badge)({
"& .MuiBadge-badge": {
color: "red",
backgroundColor: "pink"
}
});
I left the bg property empty and it accepted the background from the style. I am using bootstrap-5.
<Badge bg="" style={{backgroundColor: '#00AFD7'}} variant="dot">...</Badge>

How to change Error color and underline color on input focus using material-ui v1.0.0-beta.40

I am using Material-UI v1.0.0-beta.40 and I want to change the border color and error text color.
How can this be done?
One of the ways to do it is inside of MuiTheme
import { createMuiTheme } from 'material-ui/styles';
const myTheme = createMuiTheme({
overrides:{
MuiInput: {
underline: {
'&:after': {
backgroundColor: 'any_color_hex',
}
},
},
}
});
export default myTheme;
and then import it into your component and use:
import {MuiThemeProvider} from 'material-ui/styles';
import myTheme from './components/myTheme'
<MuiThemeProvider theme = {myTheme}>
<TextField />
</MuiThemeProvider>
Hope this helps!
You can accomplish that by using errorStyle attribute
errorStyle The style object to use to override error styles
Version 0.20.0
<TextField
hintText="Hint Text"
errorText="This field is required"
errorStyle={{color: 'green'}}
/>
Working Demo
Version 1.0.0 beta
const styles = theme => ({
greenLabel: {
color: '#4CAF50',
},
greenUnderline: {
'&:before': {
backgroundColor: '#4CAF50',
},
},
greenUnderline: {
'&:after': {
backgroundColor: '#4CAF50',
},
},
});
<FormControl >
<InputLabel style={{color: 'green'}} htmlFor="name-simple">Error</InputLabel>
<Input classes={{ inkbar: classes.greenInkbar, underline: classes.greenUnderline }} id="name-simple" value={this.state.name} onChange={this.handleChange} />
</FormControl>
Here is the solution based on most recent changes by material-ui v1. Hope It's help
import { createMuiTheme } from '#material-ui/core/styles';
const inputBoxTheme = createMuiTheme({
overrides:{
MuiInput: {
underline: {
'&:after': {
borderBottom: '2px solid #fff',
},
"&:after": {
borderBottom: '2px solid #fff',
}
},
},
}
});
export default inputBoxTheme;
import { MuiThemeProvider } from '#material-ui/core/styles';
import inputBoxTheme from './theme/inputBoxTheme'
<MuiThemeProvider theme = {inputBoxTheme}>
<TextField />
</MuiThemeProvider>
An override isn't necessary to change the error color globally. It can be defined wherever you define your global colors / theme elements. For example:
import createMuiTheme from '#material-ui/core/styles/createMuiTheme';
export default createMuiTheme({
palette: {
primary: {
main: '#FFFFFF',
},
secondary: {
main: '#6200EE',
},
error: {
main: '#D0180B',
},
},
});

Resources