RELATED QUESTION OVER AT: Styles being overwritten by Material-UI style
I am create a component library on top of Material UI. Using JSS I'd like to be able to pass in styles to my custom components. However, I'm having issues with material-ui's root styles having higher specificity than what I'm passing in. I have tried overwriting the material-ui components default styles with the classes
syntax but it simply creates another class with a similar name and higher specificity (makeStyles-root-51).
Consuming Custom Component:
import React from 'react';
import {gSelect} from 'g-react-component-library/dist'
import {createUseStyles} from 'react-jss'
const useStyles = createUseStyles({
gSelect: {margin: "15px"},
example: {float: "left", display: "inline-block", whiteSpace: 'nowrap', verticalAlign: 'top'}
});
function App() {
const classes = useStyles();
return (
<div className={classes.example}>
<div className={classes.separator}>
<div>Selects:</div>
<gSelect default={1} classes={{gSelect: classes.gSelect}} callback={(e)=>{console.log(`${e} selected`)}} options={[1,2,3,4]}/>
<gSelect default={'One'} classes={{gSelect: classes.gSelect}} callback={(e)=>{console.log(`${e} selected`)}} options={["One", "Two", "Three", "Four"]}/>
</div>
</div>
);
}
export default App;
The Actual Custom Component:
import React, {useState} from 'react';
import {Button, Select, FormControl, MenuItem, InputLabel} from '@material-ui/core'
import {makeStyles} from '@material-ui/styles'
import PropTypes from 'prop-types'
const gSelect = (props) => {
const [value, setValue] = useState();
const handleChange = event => {
props.callback(event.target.value);
setValue(event.target.value);
};
const useStyles = makeStyles({
select: {
border: 'solid #33333366 1px',
color: 'rgba(51, 51, 51, 0.66)',
fontWeight: '700',
backgroundColor: 'white',
outline: 'none',
borderRadius: '5px',
textAlign: 'left',
width: '300px',
position: 'relative',
},
root: {
}
});
const classes = useStyles(props);
return (
<FormControl classes={{root: classes.gSelect}}>
<InputLabel id="demo-simple-select-label">{props.default}</InputLabel>
<Select value={value} onChange={handleChange} className={classes.select}>
{props.options.map((option, index) => {
return <MenuItem key={`${option}_${index}`} value={option}>{option}</MenuItem>
})}
</Select>
</FormControl>
)
};
gSelect.propTypes = {
callback: PropTypes.func.isRequired,
options: PropTypes.array.isRequired,
default: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number
]).isRequired,
disabled: PropTypes.bool,
width: PropTypes.string
};
module.exports = {
gSelect
};
useStyles
hook inside the component, defeats the purpose. – Hakeemclasses
should come through props and you should pass those props to your hook - material UI does all the magic of concatenating passed in classes to your own. I personally prefer thewithStyles
hoc because all of this logic is handled automagically for you and you don't have to worry about forgetting to pass your props to your useStyles hook. – Hakeemconsole.error
. As a matter of fact, they are this warning. Material UI is providing those warnings because they think that it's caused by devs not knowing what they are doing (most of the time they are correct). But if you know how the merging of classes works, they are honestly safe to ignore. By the way, this sandbox should show you how to "fix" it: codesandbox.io/s/material-demo-udlfh – HakeemclassName
and that worked. I tried this originally but I think because I was using thecreateUseStyles
instead ofmakeStyles
it was not working. However, your help was really invaluable to getting to that solution, thanks! Also, I've run into a very similar but different issue and created another question. By no means do I expect your help but if you wanted to take a crack at it be my guest. Thanks again! #60180084 – Hackney