Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of Select component's border and arrow icon Material UI

Tags:

I'm trying to use a Material UI Select component on a dark background:

enter image description here

But I'm unable to change the color of the drop down icon and underline border to white. I've looked at overriding the styles using classes, and I'm able to change color with the following:

const styles = theme => {     root: {        borderBottom: '1px solid white',     },     icon: {        fill: 'white',     }, }  class MyComponent extends React.Component {      render() {         const {classes} = this.props;         return (             <Select                 value={this.props.value}                 inputProps={{                     classes: {                         root: classes.border,                         icon: classes.icon,                     },                 }}             >                 <MenuItem                     value={this.props.value}                 >                     Foo                 </MenuItem>             </Select>            )     } } 

However, I cannot seem to set the color of the underline while the Select component is in focus, i.e. with the above code, I get the white underline and icon, but while focus is on the component the underline stays black.

like image 727
simen-andresen Avatar asked Jul 17 '18 17:07

simen-andresen


People also ask

How do I change the arrow color in select material UI?

To change the color of Select component's border and arrow icon with React Material UI, we can use the '&:before' and '&:after' selectors and apply the styles for them. We call makeStyles with a function that returns an object with the select property set to an object with the drop down styles.

How do I change the color of my icons in material UI?

The simplest way to specify/override the color of an Icon in Material-UI is to use a custom CSS class name. Suppose that you want to show a green checkbox rather than a red triangle, depending on the outcome of some process. You then apply makeStyles to that function, and run the result.


1 Answers

With some help from Jan-Philipp I got everything colored white, also while the component stays in focus:

const styles = theme => ({     select: {         '&:before': {             borderColor: color,         },         '&:after': {             borderColor: color,         }     },     icon: {         fill: color,     }, });   class MyComponent extends React.Component {      render() {         const {classes} = this.props;         return (             <Select                 value="1"                 className={{classes.select}}                 inputProps={{                     classes: {                         icon: classes.icon,                     },                 }}             >                 <MenuItem value="1"> Foo 1</MenuItem>                 <MenuItem value="2"> Foo 2</MenuItem>             </Select>            )     } } 

Not a very pretty solution to getting the right contrast, but it does the job.

Edit The above answer is missing some code, and is also missing the hover color, as suggested by @Sara Cheatham. See updated hooks based solution:

const useStyles = makeStyles({     select: {         '&:before': {             borderColor: 'white',         },         '&:after': {             borderColor: 'white',         },         '&:not(.Mui-disabled):hover::before': {             borderColor: 'white',         },     },     icon: {         fill: 'white',     },     root: {         color: 'white',     }, })  export const MyComponent = () => {     const classes = useStyles()     return (         <Select             className={classes.select}             inputProps={{                 classes: {                     icon: classes.icon,                     root: classes.root,                 },             }}             value='1'         >             <MenuItem value='1'> Foo 1</MenuItem>             <MenuItem value='2'> Foo 2</MenuItem>         </Select>     ) } 
like image 62
simen-andresen Avatar answered Sep 18 '22 07:09

simen-andresen