Tips for altering the appearance of the material-ui slider thumb design when it is in a disabled state

Through the use of withStyles, I have successfully customized the style of the Slider:

const CustomSlider = withStyles(theme => ({
  disabled: {
    color: theme.palette.primary.main
  },
  thumb: {
    height: 24,
    width: 24,
  },
}))(Slider);

However, it appears that the changes to the height and width of the thumb only take effect when the component is not disabled (disabled={false}).

Is there a straightforward solution to adjust the slider's height and width when it is disabled (disabled={true})?

Check out the demo here: https://codesandbox.io/s/slide-thumb-size-gxb4g?file=/demo.js

Answer №1

Issue

The current style is being overridden by the className Mui-disabled

As a result, the color will remain unchanged.

https://i.sstatic.net/kJ9kF.png

Solution

To address this, override the styles for MuiSlider-thumb or Mui-disabled

One option is to utilize MUI classNames with the nesting selector

"& .MuiSlider-thumb": {
  height: 24,
  width: 24
}

It's important to note that withStyles attributes correspond to the CSS API. For customizing classNames not exposed by the CSS API, consider using className + style hooks instead.

Full code snippet:

import React from "react";
import Slider from "@material-ui/core/Slider";
import Paper from "@material-ui/core/Paper";
import { withStyles, makeStyles } from "@material-ui/core/styles";

const useStyles = makeStyles(theme => ({
  margin: {
    margin: theme.spacing(10),
    "& .MuiSlider-thumb": {
      height: 24,
      width: 24
    }
  }
}));

const CustomSlider = withStyles(theme => ({
  disabled: {
    color: theme.palette.primary.main
  },
  thumb: {
    // color: "red"
  }
}))(Slider);

export default function MyCustomSlider() {
  const classes = useStyles();
  return (
    <div>
      <Paper className={classes.margin}>
        <CustomSlider
          defaultValue={[10, 15]}
          min={0}
          max={20}
          valueLabelDisplay="on"
          disabled={true}
        />{" "}
        <CustomSlider
          defaultValue={[5, 7]}
          min={0}
          max={20}
          valueLabelDisplay="on"
          disabled={false}
        />{" "}
      </Paper>
    </div>
  );
}

https://i.sstatic.net/znetb.png


Update

Incorporating withStyles:

const styles = theme =>
  createStyles({
    margin: {
      margin: theme.spacing(10)
    },
    thumb: {
      "& .MuiSlider-thumb": {
        height: 24,
        width: 24
      }
    }
  });

function MyCustomSlider(props) {
  // const classes = useStyles();
  return (
    <div>
      <Paper className={props.classes.margin}>
        <Slider
          defaultValue={[10, 15]}
          min={0}
          max={20}
          valueLabelDisplay="on"
          disabled={true}
          className={props.classes.thumb}
        />{" "}
        <Slider
          defaultValue={[5, 7]}
          min={0}
          max={20}
          valueLabelDisplay="on"
          disabled={false}
        />{" "}
      </Paper>
    </div>
  );
}

export default withStyles(styles)(MyCustomSlider);

https://codesandbox.io/s/slide-thumb-size-jtmzl?fontsize=14&hidenavigation=1&theme=dark

Answer №2

customStyle: {
    "& .CustomSlider-thumb ": {
      display: "block",
    },
    "& .CustomSlider-track": {
      backgroundColor: "#f0f0f0",
    },
  },

This is a unique solution, allowing users to customize the appearance of a slider and its components.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Inconsistencies with Colorbox behavior in an external HTML document

Encountering a strange issue with colorbox while attempting to display a "div" element from another HTML file. The code functions perfectly, except for the final "div" in the external file. The structure of my external file is as follows; div1 img par ...

Limit the maximum column gap on the grid and stop it from expanding further

I'm currently facing an issue with some columns/rows on my website that are keeping content locked in the top left corner, reminiscent of early 00's web design. The site was originally designed for smaller screens like 1024x764, so when viewed on ...

Form with checkboxes in a Next.js and Typescript application

I am currently working on a project using Next.js and Typescript. I have implemented a form in this project, which is my first experience with Typescript and checkbox types. However, I am encountering difficulties in retrieving all checkbox values, adding ...

Is it possible to create a button on my website that, when clicked, sends data to my Python script?

I need some help with my project - I'm trying to figure out how to create a button on my website that, when clicked, will display a string in my Python terminal. Can anyone offer some guidance on how to achieve this? Thanks in advance! ...

Issue with react-quill image handler failing to display images

import ReactQuill from "react-quill"; import Quill from "quill"; import "react-quill/dist/quill.snow.css"; const AddPrice = () => { const quillRef = useRef(null); const [body, setBody] = useState(""); const too ...

Execute a node script file at random intervals

I've been utilizing forever to maintain the continuous execution of a node script: forever start script1.js However, my requirement is to run these files in a random order... For instance: Execute node script1.js Execute node script2.js Run script ...

How can we enforce that only a certain type of ReactElement is allowed to be passed as props to a Component in TypeScript?

eslint and vscode seem to have trouble detecting validation errors when passing incompatible ReactElement types. Despite searching through documentation and examples, I haven't been able to find a solution that works. // Footer.tsx export interface ...

Disabling an animation in an Android TabLayout when a specific tab is selected

Whenever I touch on TabLayout, a brief flash of grey color appears. How can I disable this effect? ...

String variable representing the name of a React element

As I was reviewing the source code of a React UI library, I stumbled upon an interesting code pattern that I will simplify here: function Test() { let Button = "button"; // ... return <Button>Click me</Button>; } I'm curious about ...

Finding mongoose in an array of objects nested within another object

Here is the MongoDB JSON document I am working with: { categoryId: '1', categoryName: 'Outdoors Equipments', items: [ { itemId: '1', itemName: 'Camping T ...

Personalized search feature for Bootstrap tables

Below is the table structure: <table> <tr> <th>Number</th> <th>Name</th> </tr> <tr> <td>200.10.1234</td> <td>Maria Anders</td> </tr> <tr> < ...

Display the duplicated values of an array extracted from a JSON object just once

Having some trouble creating a list from json data, you can check out the example. My goal is to display each value only once if duplicates occur (for example, if there are 2 Martini glasses, I only want one to appear in the list), while still preserving t ...

Error: Unable to access 'length' property of null in Next.js issue

I am encountering an error in my Next.js files: 117 | }; 118 | > 119 | if (data.length <= 0) { | ^ 120 | return null; 121 | } 122 | If I want to display it with an image, the error looks like this:https://i.stack ...

Guidelines for specifying alternate structures in JSON schema

I have two different JSON file structures, one like this: { "api": "http://my.api.host", "modules": [ "core", "module", "another" ] } and the other like this: { "api": "http://my.api.host", "modules": "*" } The modules attri ...

What steps should I take to transition from a class to a function in this scenario?

I am struggling to make the following code work properly. Every attempt I have made has resulted in issues with the event. import React from 'react' import YouTube from 'react-youtube'; export default class youtube extends React.Comp ...

I am encountering an issue with the return ( statement and I'm unable to comprehend the reason behind it

import { connect } from 'react-redux' import { Link } from 'react-router-dom' class MyFavoriteStories extends React.Component { markAsFavorite = (e) => { this.setState({ bgColor: "blue" }) } render () { con ...

The variable req.body.Dates has not been declared

I am currently working on a project that involves dynamically populating two drop down menus using SQL Server. Depending on the selected items, I need to load a specific ejs template using AJAX. My goal is to load data based on the selected criteria. For e ...

cleaner urls using nextjs routing

Currently working on developing a fresh marketing platform for my company utilizing next.js, and encountering some hurdles with URLs. Essentially, I've created a custom API route to retrieve data from our internal database using Prisma: getAllDealers ...

How to create a custom hover effect for IconButtons in Material-UI

Customizing Hover Effects on IconButton I am currently using an IconButton component from Material-UI and have noticed a subtle grey border that appears when hovering over the icon. I am looking for a way to disable this hover effect, as I cannot seem to ...

Element with adhesive properties alters its color at a particular location

I need the sticky element to change colors at specific points and then revert back to its original color. The color should be orange initially, green on block 2, and orange again on block 3. For the complete code and to address any issues with jQuery, pl ...