What's the reason for not being able to customize classes for a disabled element in Material-UI?

Currently, I am utilizing Material-UI to style my components. However, I am facing challenges when trying to customize the label class for disabled buttons. Despite setting a reference as "&$disabled", it does not yield the desired results.

import React from "react";
import { withStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";

// By using the `withStyles()` higher-order component, a `classes`
// prop is injected and utilized by the `Button` component.
const StyledButton = withStyles({
  root: {
    background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
    borderRadius: 3,
    border: 0,
    color: "white",
    height: 48,
    padding: "0 30px",
    boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)"
  },
  label: {
    "&$disabled": { backgroundColor: "blue", textTransform: "capitalize" }
  },
  disabled: {}
})(Button);

export default function ClassesShorthand() {
  return <StyledButton disabled>classes shorthand</StyledButton>;
}

If you want to explore more, here is a link to the codesandbox example: https://codesandbox.io/s/material-demo-7s9u3

Answer №1

You have identified two distinct issues:

  1. The placement of your &$disabled reference within the label class is causing a problem. The label class is applied to a span inside the button, whereas the disabled class needs to be on the button itself. This results in CSS with .MuiButton-label.Mui-disabled not matching anything. To resolve this, move &$disabled under root instead of label.
  2. Another issue exists in the definition of the background-image property within the linear-gradient in the root. You are only overriding the background-color, and when a background image is present, the color isn't visible. Therefore, for the disabled case, you'll need to remove the background image.
import React from "react";
import { withStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";

// The `withStyles()` higher-order component injects a `classes`
// prop utilized by the `Button` component.
const StyledButton = withStyles({
  root: {
    background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
    borderRadius: 3,
    border: 0,
    color: "white",
    height: 48,
    padding: "0 30px",
    boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)",
    "&$disabled": {
      backgroundImage: "none",
      backgroundColor: "blue",
      color: "rgba(255,255,255,0.6)",
      textTransform: "capitalize"
    }
  },
  disabled: {}
})(Button);

export default function ClassesShorthand() {
  return <StyledButton disabled>classes shorthand</StyledButton>;
}

https://codesandbox.io/s/disabled-button-background-veup6?fontsize=14&hidenavigation=1&theme=dark


If targeting the span within the button instead of the button itself is intentional, follow these steps (which focus on the label class as a descendant of the disabled class):

import React from "react";
import { withStyles } from "@material-ui/core/styles";
import Button from "@material-ui/core/Button";

// The `withStyles()` higher-order component injects a `classes`
// prop used by the `Button` component.
const StyledButton = withStyles({
  root: {
    background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
    borderRadius: 3,
    border: 0,
    color: "white",
    height: 48,
    padding: "0 30px",
    boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)"
  },
  label: {
    "$disabled &": {
      backgroundColor: "blue",
      color: "rgba(255,255,255,0.6)",
      textTransform: "capitalize"
    }
  },
  disabled: {}
})(Button);

export default function ClassesShorthand() {
  return <StyledButton disabled>classes shorthand</StyledButton>;
}

https://codesandbox.io/s/disabled-button-background-on-label-p9md4?fontsize=14&hidenavigation=1&theme=dark

Answer №2

When working with Material-ui Button, it is important to note that there is a disabled css rule which should not be left as an empty object. Below is an example of how to properly style the button:

const StyledButton = withStyles({
  root: {
    background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
    borderRadius: 3,
    border: 0,
    color: "white",
    height: 48,
    padding: "0 30px",
    boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)"
  },
  disabled: { background: "blue", textTransform: "capitalize" }
})(Button);

https://codesandbox.io/s/material-demo-59l32?fontsize=14&hidenavigation=1&theme=dark

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

The package.json file engines field specifying the version with a tilde and then a greater than sign (~>)

When a package.json file includes an engines field such as this: "engines" : { "node" : "~>12" }, What is the significance of ~> in this context? ...

Find the perfect match with Material UI Autocomplete's feature that allows you to search from the start of

Implementing material UI Autocomplete field in my React project, I aim to restrict search functionality to only match keywords from the beginning (for certain fields of the objects): Here's an example scenario with the available options: [ {data1: ...

Determine the selected radio button

----EDIT---- I am developing a jQuery mobile application and I need to determine which radio button is selected. This is the JavaScript code I'm using: function filter(){ if(document.getElementById('segment1').checked) { aler ...

Steps for adding an array of JSON objects into a single JSON object

I have a JSON array that looks like this: var finalResponse2 = [ {Transaction Amount: {type: "number"}}, {UTR number: {type: "string"}} ] My goal is to convert it into the following format: responses : [ { Transaction Amount: {type: "number"}, UTR numbe ...

Simple steps for retrieving URL parameters with AngularJS

HTML source code <div ng-app=""> <div ng-controller="test"> <div ng-address-bar browser="html5"></div> <br><br> $location.url() = {{$location.url()}}<br> $location.search() = {{$locati ...

Launch the Image-Infused Modal

I am completely new to the world of Ionic development. Currently, I am working on a simple Ionic application that comprises a list of users with their respective usernames and images stored in an array. Typescript: users = [ { "name": "First ...

Trouble Arising from the Lack of Coordination Between CSS Transition and JavaScript Update Triggered by Element

I'm currently working on a web development project that involves a list of clickable elements. When one of these elements is clicked, it should become active and trigger a CSS transition (such as a transform) with a duration of 200ms. Additionally, I ...

Pagination Component for React Material-UI Table

I am interested in learning about Table Pagination in React UI Material. Currently, my goal is to retrieve and display data from an API in a Material UI Table. While I have successfully implemented some data from the API into the Material UI Table, I am ...

Create a webpage layout using Bootstrap that features a left-aligned image within a div, with

Is there a way to achieve this layout using Bootstrap? View the desired design I am looking to create a div with an image on the left (float:left) and text (which could be one or multiple lines) along with a button aligned to the bottom right. The goal i ...

Calculate the sum of the column values and disable the option to insert new rows in the UI grid

I am currently working on a ui-grid that has a column C which displays percentage values. There is a button labeled "add rows" that allows users to add new rows to the grid. However, the catch is that users can only add new rows until the total percentage ...

1. "Implementing initial state in React using ES6 classes"2. "Utilizing ES

The following class has been developed: class App extends Component{ render() { return ( <div className="app"></div> ); } } What is the correct way to set the initial state? Using getInitialState() ...

"Unraveling Vue.js: A guide to fetching JSON data one object at a time

I am facing a challenge with a large JSON file (40 MB) that includes data on countries, their IDs, and a total sum that I need to calculate. { "0": { "id": 0, "country": "usa", "sum": 201, }, ...

A set of six DIV's, divided into two columns of three each

I'm attempting to arrange two columns with three vertical DIVs side by side. The left column is designated for main text, while the right column is reserved for miscellaneous information. Each column consists of a top and bottom DIV that display image ...

Challenges encountered with axios and useState in a React application

Having an issue with fetching data using axios. The console.log shows that it's working fine, but when trying to set it to the state, it returns an empty array. I've searched for a solution without success. Any suggestions? import React, { useSta ...

Using JavaScript to Filter Arrays with Partial String Matching

I have an array of objects where the value I need to filter on is nested in a lengthy string. The array is structured as follows: { "data": { "value": & ...

Tips for creating a sticky bootstrap column that remains in place as you scroll

I am looking to design a FAQ page similar to Twitter's FAQ. The goal is to have the left column remain fixed in its position while allowing users to scroll through the content. Here is what I have attempted so far, but it is not functioning as expect ...

Implementing a Custom CSS Theme in JavaFX

Hey everyone, I stumbled upon this amazing JavaFX theme called Flatter on . I've been attempting to implement it for a couple of hours now, but I'm struggling with the process. Can someone provide me with a detailed guide on how to activate thi ...

Using dynamic classes within a v-for loop

Edited How can I dynamically assign classes to multiple elements within a v-for loop? Since I cannot utilize a computed property due to the presence of v-for, I attempted using a method by passing the index of the element, but that approach did not yield t ...

Error loading Azure Active Directory web form: Server returned a 401 status code for the requested resource

I recently made changes to my web site (incorporating a web form and entity framework) to use AAD connection, following the guidance in this insightful blog post. However, I am encountering an issue where scripts and css files are not loading properly. Th ...

What is the most effective way to utilize bootstrap classes and their associated functions exclusively for mobile devices by using media queries?

I am encountering an issue with a table in my application that causes the UI to break when the screen width is reduced to less than 600px. To address this, I would like to implement the following bootstrap classes to add a scroll to the application: .tabl ...