Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Material-UI Slider Color

I want to change Material UI Slider component color

I have tried to change CSS style and it's not working, then I tried the solution given in this issue and applied this code but it's not working

getMuiTheme:

const muiTheme = getMuiTheme({
  slider: {
    trackColor: "yellow",
    selectionColor: "green"
  }
});

and in Component:

<MuiThemeProvider muiTheme={muiTheme}>
     <Slider
         min={18}
         max={90}
         ValueLabelComponent={ValueLabelComponent} 
         defaultValue={40}
     />
</MuiThemeProvider>
like image 545
Mohammad Sadegh Foroughi Avatar asked Oct 10 '19 16:10

Mohammad Sadegh Foroughi


2 Answers

It depends what version of Material-UI you are using.
Your code matches Material-UI v0.x:

import React from 'react';
import Slider from 'material-ui/Slider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import { MuiThemeProvider } from 'material-ui';

const muiTheme = getMuiTheme({
  slider: {
    trackColor: "yellow",
    selectionColor: "red"
  }
});

export default function V0Slider() {


  return (
    <MuiThemeProvider muiTheme={muiTheme}>
    <Slider
    min={18}
    max={90}
    ValueLabelComponent={0} 
    defaultValue={40}
    />
   </MuiThemeProvider>
  );
}

Edit Invisible Backdrop

If you are using Material-UI v4, that would be the way to go:

import React from "react";
import Slider from '@material-ui/core/Slider';
import { createMuiTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/styles';

const muiTheme = createMuiTheme({
  overrides:{
    MuiSlider: {
      thumb:{
      color: "yellow",
      },
      track: {
        color: 'red'
      },
      rail: {
        color: 'black'
      }
    }
}
});

export default function V4Slider() {
  return (
    <ThemeProvider theme={muiTheme}>
      <Slider min={18} max={90} defaultValue={40} />
    </ThemeProvider>
  );
}

Edit Invisible Backdrop

If you are using another version of material-ui, let me know which one and i'll try to help.

like image 161
Ido Avatar answered Nov 06 '22 03:11

Ido


I did the override with the 'withStyles' of material-ui v4.

This is for the Slider styles :

const CustomSlider = withStyles({
    root: {
        color: "#6f8eff",
        height: 3,
        padding: "13px 0",
    },
    track: {
        height: 4,
        borderRadius: 2,
    },
    thumb: {
        height: 20,
        width: 20,
        backgroundColor: "#fff",
        border: "1px solid currentColor",
        marginTop: -9,
        marginLeft: -11,
        boxShadow: "#ebebeb 0 2px 2px",
        "&:focus, &:hover, &$active": {
            boxShadow: "#ccc 0 2px 3px 1px",
        },
        color: "#fff",
    },
})(Slider);

And just after that render your slider :

<CustomSlider
    value={value}
    onChange={handleChange}
    step={20} />

And Color should update, i also update the thumbs here for custom thumbs. Hope it's helps : )

like image 20
Ido Bleicher Avatar answered Nov 06 '22 02:11

Ido Bleicher