Overriding existing Mui classes using makeStyles - css

So, this can be a very minute implementation issue on my end I think. I used to keep stylesheets completely separate from my component.js file but lately, I started to see some benefits of writing styles inside the component with styled components, emotions, make styles, etc. I tried it on a dummy project and started to implement it out on some components on an app I am working on right now. The reasons were primarily more manageable, dev-experience, and it worked for me.
But then I found myself in this issue of overriding existing mui styles.
For example, I am using material-ui to make this accordion component. And there is a nested property
.MuiAccordionSummary-root.Mui-expanded {
min-height: 64px;
}
To edit this is my sass file would be very easy but how do I make it work with make styles?
My current styles look like this
const useStyles = makeStyles((theme) => ({
root: {
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
marginBottom: '16px',
maxHeight: '35px',
},
summary: {
backgroundColor: theme.palette.complimentory.main,
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
maxHeight: '35px',
height: '35px',
},
title: {
fontFamily: 'Helvetica',
fontStyle: 'normal',
fontWeight: 'normal',
fontSize: '14px',
lineHeight: '14px',
letterSpacing: '0.266667px',
textTransform: 'uppercase',
},
content: {
marginTop: '15px',
fontSize: '14px',
fontStyle: 'normal',
lineHeight: '16.8px',
letterSpacing: '0.375px',
fontWeight: '400',
fontFamily: 'Helvetica',
textAlign: 'left',
},
}))
I am struggling to target it inside makestyles, I think it must be possible, I just am having a hard time as I am not used to it.

You should use <StyledEngineProvider injectFirst> at the root of you project
import { render } from "react-dom";
import { StyledEngineProvider } from "#mui/material";
import App from "./App";
const rootElement = document.getElementById("root");
render(
<StyledEngineProvider injectFirst>
<App />{" "}
</StyledEngineProvider>,
rootElement
);
import * as React from "react";
import AppBar from "#mui/material/AppBar";
import Box from "#mui/material/Box";
import Toolbar from "#mui/material/Toolbar";
import Typography from "#mui/material/Typography";
import Button from "#mui/material/Button";
import IconButton from "#mui/material/IconButton";
import MenuIcon from "#mui/icons-material/Menu";
import makeStyles from "#mui/styles/makeStyles";
const useStyles = makeStyles((theme) => {
return {
root: {
backgroundColor: "red",
color: "pink"
}
};
});
export default function App() {
const classes = useStyles();
return (
<Box sx={{ flexGrow: 1 }}>
<AppBar position="static" className={classes.root}>
<Toolbar>
<IconButton
size="large"
edge="start"
color="inherit"
aria-label="menu"
sx={{ mr: 2 }}
>
<MenuIcon />
</IconButton>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</Box>
);
}
Working demo here https://codesandbox.io/s/wizardly-stonebraker-qnh3t

I'll use the AccordionSummary as an example on how to override existing styles. The Accordion Summary has 6 CSS rules that can be overridden: root, expanded, focused, disabled, content and expandIcon. You can see this at the CSS section of the AccordionSummary API page in Material UI's official documents. Each of these correspond to a Material UI class for the respective component e.g. root for the AccordionSummary corresponds to the class .MuiAccordionSummary-root. To override these styles, you do it via overriding styles with classes.
Using your example:
Create the class name and add the styling in makeStyles. I'll create one called summaryExpanded and will include the styling minHeight: '64px'.
const useStyles = makeStyles((theme) => ({
root: {
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
marginBottom: '16px',
maxHeight: '35px',
},
summary: {
backgroundColor: theme.palette.complimentory.main,
borderBottom: '0.5px solid',
borderBottomColor: theme.palette.primary.main,
maxHeight: '35px',
height: '35px',
},
summaryExpanded: {
minHeight: '64px',
},
title: {
fontFamily: 'Helvetica',
fontStyle: 'normal',
fontWeight: 'normal',
fontSize: '14px',
lineHeight: '14px',
letterSpacing: '0.266667px',
textTransform: 'uppercase',
},
content: {
marginTop: '15px',
fontSize: '14px',
fontStyle: 'normal',
lineHeight: '16.8px',
letterSpacing: '0.375px',
fontWeight: '400',
fontFamily: 'Helvetica',
textAlign: 'left',
},
}))
Override the style in the AccordionSummary component by accessing the relevant rule name and assigning your custom class. In this case, it is expanded, as we are looking to override .Mui-expanded in the AccordionSummary component.
const classes = useStyles();
<Accordion>
<AccordionSummary
classes: {{
expanded: classes.summaryExpanded,
}}
>
</AccordionSummary>
<AccordionDetail>
</AccordionDetail>
</Accordion>

You can do something like this - play around override properties.
import { Overrides } from "#material-ui/core/styles/overrides";
const overrides: Overrides = {
MuiExpansionPanelSummary: {
root: {
minHeight: "3.25rem",
"&$expanded": {
minHeight: "3.25rem",
},
},
content: {
margin: "0.25rem",
"&$expanded": {
margin: "0.25rem",
},
},
expandIcon: {
padding: "0.5rem",
},
expanded: {},
},
};

Related

Styling the DatePicker from MUI

I'm using a DatePicker component from Mui Lab and I am trying to style the Calendar component by adding some border or background color. I used the PaperProps prop for DatePicker but it's not styling it. Trying to understand why I cant just use an SX prop for it
this is the calendar where i want to add a border to
import PropTypes from 'prop-types';
import { TextField } from '#material-ui/core';
import { alpha } from '#material-ui/core/styles';
import DatePicker from '#material-ui/lab/DatePicker';
import { AppBorderStyle } from '../theme';
export const DateField = (props) => {
const {
error,
fullWidth,
helperText,
label,
onChange,
onBlur,
placeholder,
disabled,
value,
name,
...other
} = props;
return (
<DatePicker
PopperProps={{
sx: {
'& .MuiPaper-root': {
backgroundColor: 'red',
border: '1px solid black',
}
}
}}
onChange={onChange}
renderInput={({ InputProps, ...rest }) => (
<TextField
{...rest}
disabled={disabled}
onBlur={onBlur}
name={name}
error={error}
fullWidth={fullWidth}
helperText={helperText}
label={label}
placeholder={placeholder}
sx={{
'& .MuiFilledInput-root': {
backgroundColor: 'background.paper',
borderRadius: 1,
border: AppBorderStyle,
px: 1.5,
py: 0.75,
transition: (theme) => theme.transitions.create([
'border-color',
]),
'&:hover': {
backgroundColor: 'background.paper'
},
'&.Mui-focused': {
backgroundColor: 'background.paper',
boxShadow: (theme) => `${alpha(theme.palette.primary.main,
0.25)} 0 0 0 0.2rem`
},
'& .MuiFilledInput-input': {
fontSize: 14,
height: 'unset',
lineHeight: 1.6,
p: 0
},
'&.Mui-disabled': {
backgroundColor: 'action.disabledBackground',
boxShadow: 'none',
borderColor: alpha('#D6DBE1', 0.5)
}
}
}}
variant="filled"
InputProps={{
disableUnderline: true,
...InputProps
}}
InputLabelProps={{
shrink: true,
sx: {
color: 'text.primary',
fontSize: 14,
fontWeight: 500,
mb: 0.5,
position: 'relative',
transform: 'none'
}
}}
/>
)}
value={value}
{...other}
/>
);
};
DateField.defaultProps = {
disabled: false,
};
DateField.propTypes = {
disabled: PropTypes.bool,
error: PropTypes.bool,
fullWidth: PropTypes.bool,
helperText: PropTypes.string,
label: PropTypes.string,
name: PropTypes.string,
onChange: PropTypes.func.isRequired,
onBlur: PropTypes.func.isRequired,
placeholder: PropTypes.string,
value: PropTypes.instanceOf(Date)
};
I noticed you're using depecrated version of mui as you can check here. For newer versions of material UI components, we import material components from #mui and not from #material-ui.
I'm not sure if the classes are the same in your version or not. But the issue for your case looks like you are targeting the wrong class to add a border. You need to target MuiPickersPopper-root class to change the border.
PopperProps={{
sx: {'&.MuiPickersPopper-root': {border: '4px solid red'},},
}}
I created a solution of DatePicker with the border here. Cheers!

How can style the backdrop of a MaterialUI modal?

I have a MUI Modal set up, it's currently working, however the backdrop is being displayed as black. I have styling in place, but for some reason the backdrop is not being updated and remains pitch black.
My code is below, a screenshot is also below of what I see when the Modal is opened. Please let me know how I can fix this issue.
import { Box, Modal, Typography } from "#material-ui/core";
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
modalStyle: {
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: 300,
bgcolor: "background.paper",
boxShadow: 24,
p: 4,
backgroundColor: "white",
borderRadius: "10px",
},
}));
const PopupModal = ({ postModalOpen, handleClose, children }) => {
const classes = useStyles();
return (
<Modal open={postModalOpen} onClose={handleClose}>
<Box className={classes.modalStyle}>
ModalText
<Typography
id="modal-modal-title"
variant="h6"
component="h2"
style={{
fontSize: "14px",
marginLeft: "5px",
}}
>
{children}
</Typography>
</Box>
</Modal>
);
};
export default PopupModal;
If you'd like to style the backdrop, your styles must be passed to the Modal component directly:
For example:
const useStyles = makeStyles((theme) => ({
/** Changed modalStyle */
modalStyle: { backgroundColor: "rgba(255, 0, 0, 0.5)" },
/** Moved content styling to new class */
contentStyle: {
/** Also removed some invalid attributes **/
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
width: "300px",
backgroundColor: "white",
borderRadius: "10px"
}
}));
const PopupModal = ({ postModalOpen, handleClose, children }) => {
const classes = useStyles();
return (
// Note the changed className props with your sample code
<Modal
open={postModalOpen}
onClose={handleClose}
className={classes.modalStyle}
>
<Box className={classes.contentStyle}>
<Typography
id="modal-modal-title"
variant="h6"
component="h2"
style={{
fontSize: "14px",
marginLeft: "5px"
}}
>
{children}
</Typography>
</Box>
</Modal>
);
};
Working Example: https://codesandbox.io/s/material-demo-forked-edhqx?file=/demo.js
FYI: You can also override all backdrops in your application, on a global scale, by passing a MuiBackdrop override object to createMuiTheme().
This is how I customized my MUI Backdrop (MUI v5)
<Backdrop open={isOpen} sx={{ backgroundColor: 'rgba(0, 0, 0, 0.25)', zIndex: 1 }} onClick={handleCloseMenu} />

Problem using the work-breake with Arabic language

I have here list of cards that I want to be in Arabic language but it has a problem with continue the word, it takes like half the word letters on line then break the half on the next line, for example in arabic "بطر
يق"
A picture for example:
can you help me please
Please note that the code written in React.js
the code
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import Card from '#material-ui/core/Card';
import CardActions from '#material-ui/core/CardActions';
import CardContent from '#material-ui/core/CardContent';
import Button from '#material-ui/core/Button';
import Typography from '#material-ui/core/Typography';
import { fontSize, textAlign } from '#material-ui/system';
import "../fonts/29LTAzer-Medium.ttf"
import "../Styles/card.css"
const useStyles = makeStyles({
overrides: {
MuiCardActions: {
root: {
display: 'flex',
alignItems:"center",
padding: 8,
flexDirection: "row-reverse",
},
}
},
card: {
width: 700,
height:250,
backgroundColor: "#009CA7",
color:"white",
borderRadius: 25,
margin: 15,
textAlign: 'right',
fontFamily:"LTAzer-Regular",
},
bullet: {
display: 'inline-block',
margin: '0 2px',
transform: 'scale(0.8)',
fontFamily: "LTAzer-Regular",
},
title: {
fontSize: 14,
color:"white",
fontFamily: "LTAzer-Medium",
},
pos: {
marginBottom: 12,
color:"white",
fontFamily: "LTAzer-Regular",
},
button:{
color:"white",
width:50,
marginLeft: 300,
fontSize: 15,
border: 3,
border: "solid #9DCB82",
textAlign: "center",
backgroundColor: "#9DCB82",
// paddingRight: 50,
flexDirection:"row-reverse",
fontFamily: "LTAzer-Regular"
},
desc:{
textAlign:"right",
direction:"rtl",
fontFamily:"LTAzer-Regular",
wordBreak:"break-all",
// overflowWrap:"break-word",
whiteSpace:"&nbsp",
wordWrap:"normal",
textOverflow:"ellipsis",
overflow:"hidden",
display:"-webkit-box",
// lineHeight:16,
WebkitLineClamp: 2,
WebkitBoxOrient: "vertical",
},
name:{
fontFamily: "LTAzer-Medium",
}
});
export default function SimpleCard(props) {
const classes = useStyles();
const bull = <span className={classes.bullet}>•</span>;
return (
<Card className={classes.card}>
<CardContent>
<Typography className={classes.title} color="textSecondary" gutterBottom>
{props.cardType}
</Typography>
<Typography className={classes.name} variant="h5" component="h2">
{props.cardName}
</Typography>
<Typography className={classes.pos} color="textSecondary">
{props.cardDate}
</Typography>
<Typography className={classes.desc} variant="body2" component="p">
{props.cardDesc}
</Typography>
</CardContent>
<CardActions>
<Button className={classes.button} size="small">تفاصيل</Button>
</CardActions>
</Card>
);
}
I solved the problem by removing the wordBreak:"break-all"

Can't Make MouseEnter and MouseLeave Animation on React

I'm new at react. I'm using media cards to present some picture and small text and have been trying to give small sliding animation to my text and background layer. However I failed. Codes are below:
import React, { useState, Component } from 'react';
import { makeStyles } from '#material-ui/core/styles';
import CardActionArea from '#material-ui/core/CardActionArea';
import CardContent from '#material-ui/core/CardContent';
import CardMedia from '#material-ui/core/CardMedia';
import Typography from '#material-ui/core/Typography';
import Grid from '#material-ui/core/Grid';
import Paper from '#material-ui/core/Paper';
export default function SectionCard(props) {
const [areaHeight, setAreaHeight] = useState(100);
const [imgSize, setImgSize] = useState(1);
const [lineColor, setLineColor] = useState('dodgerblue');
const [textPosition, setTextPosition] = useState(0);
const useStyles = makeStyles((theme) => ({
root: {
maxWidth: 345,
},
media: {
height: 400,
scale: imgSize,
},
greyCover: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
height: areaHeight,
width: '100%',
bottom: 0,
position: 'absolute',
},
line: {
backgroundColor: lineColor,
height: 5,
width: '100%',
borderRadius: 0,
},
textPosition: {
paddingTop: textPosition,
fontFamily: 'Quicksand !important',
color: 'white !important',
},
body: {
fontFamily: 'Quicksand !important',
},
}));
const classes = useStyles();
return (
<Grid item xs={12} sm={4} onMouseEnter={() => (setAreaHeight('100%'), setLineColor('red'), setImgSize(1.2), setTextPosition('150px!important'))} onMouseLeave={() => (setAreaHeight(100), setLineColor('dodgerblue'), setImgSize(1), setTextPosition('0px !important'))}>
<CardActionArea>
<CardMedia
className={classes.media}
image={props.img}
/>
<CardContent className={classes.greyCover} style={{ fontFamily: 'Quicksand', }}>
<div className={classes.textPosition}>
<Typography gutterBottom variant="h5" component="h2" align="center">
{props.title}
</Typography>
<Typography component="p" align="center">
{props.text}
</Typography>
</div>
</CardContent>
</CardActionArea>
<Paper className={classes.line} />
</Grid>
);
}
Codes are probably messy. Sorry for that. onMouseEnter and onMouseLeave are working fine. I just want to see that greyCover class and text moves slowly to height 100% and text to mid of the frame.
I didn't want to use another packages yet I don't know how to use too to be honest.
What I'm doing wrong?

React Radium Class Style Overrides

I am trying to write clean and DRY code styles with Radium.
This is what my button is so far.
/**
*
* Button
*
*/
import React, { PropTypes } from 'react';
import Radium from 'radium';
const styles = {
base: {
borderRadius: '3px',
backgroundColor: '#23cdf4',
fontSize: '13px',
},
primaryBlue: {
backgroundColor: '#23cdf4',
},
primaryBlueBig: {
borderRadius: '8px',
fontSize: '16px',
padding: '14px 28px',
},
primaryRed: {
backgroundColor: '#F99CAC',
},
};
class Button extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<button
style={[styles.base, styles[this.props.type], this.props.style]}
>
{text}
</button>
);
}
}
Button.propTypes = {
text: PropTypes.any,
type: PropTypes.oneOf(['primaryBlue', 'primaryBlueBig']).isRequired,
style: PropTypes.object,
disabled: PropTypes.bool,
};
export default Radium(Button);
Two things I cannot figure out:
How can I extend the background color used by primaryBlue into primaryBlueBig without repeating myself?
How can I change the background color of both blue buttons if the disabled is true?
This is a slimmed down version of what I am currently working with and am trying to get away from having giant if else blocks in the render function. Thanks! ^.^
You can use modifiers.
This is basically what you’re already doing, but with a little more refactoring.
I would define the styles a little differently:
const styles = {
base: {
borderRadius: '3px',
backgroundColor: '#ffffff',
fontSize: '13px',
},
primaryBlue: {
backgroundColor: '#23cdf4',
},
primaryRed: {
backgroundColor: '#F99CAC',
},
big: {
borderRadius: '8px',
fontSize: '16px',
padding: '14px 28px',
},
disabled: {
primaryBlue: {
backgroundColor: '#eeeeee',
},
primaryRed: {
backgroundColor: '#eff0f1',
}
}
};
Then you can have a big and a disabled prop.
return (
<button
style={[
styles.base,
styles[this.props.type],
this.props.big && styles.big,
this.props.disabled && styles.disabled[this.props.type]
]}>
{text}
</button>
);

Resources