CSS unable to modify the color of the switch in HTML code

I've been struggling to change the color of the Switch to yellow when it's turned on. Despite my attempts, I haven't been successful in doing so. Is it even possible to achieve this color change?

 <Switch
                size="small"
                checked={this.state.switchChecked}
                onClick={this.handleSwitchState}
                color="yellow"
              />

If anyone could lend a hand with this issue, I would greatly appreciate it. My goal is to have the Switch display a yellow color when toggled on.

Answer №1

If you want to personalize the color scheme, you'll need to utilize FormControlLabel in place of Switch, and then specify the desired colors within color, "&$checked", and

"&$checked + $track"
.

import React from "react";
import Switch from "@material-ui/core/Switch";
import FormControlLabel from "@material-ui/core/FormControlLabel";
import { withStyles } from "@material-ui/core/styles";
import { yellow } from "@material-ui/core/colors";

const PurpleSwitch = withStyles ({
  switchBase: {
    color: yellow[300],
    "&$checked": {
      color: yellow[500]
    },
    "&$checked + $track": {
      backgroundColor: yellow[500]
    }
  },
  checked: {},
  track: {}
})(Switch);

class App extends React.Component {
  state = {
    switchChecked: false
  };

  handleSwitchState = () => {
    this.setState((prevState) => {
      return { switchChecked: !prevState.switchChecked };
    });
  };

  render() {
    const { switchChecked } = this.state;
    return (
      <FormControlLabel
        control={
          <PurpleSwitch
            checked={switchChecked}
            onChange={this.handleSwitchState}
            name="switchChecked"
          />
        }
        label="Yellow color"
      />
    );
  }
}

export default App;

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

Webpack is failing to load the logo PNG image file

Is there a way to make the logo png file visible on the webpage? I have been encountering issues with loading the image while other elements like HTML, css, and fonts are loading properly when the web pack is started. List of Error Messages: Refused to a ...

Modifying webpack settings for a create-react-app based project: A step-by-step guide

After starting a new react project with create-react-app, I am looking to update the webpack configuration. However, I cannot seem to locate the webpack file. Should I be creating this file myself, or is there another process involved? As someone who is ...

State pertains to a collection of elements rather than specific individuals

My concept involves 10 buttons: clicking on one button triggers recognition and changes the button's color, which reverts back to its original state when recognition ends. The issue arises when clicking a button causes all buttons to change color, an ...

The TextInput field is not displaying my placeholder text - what could be causing this issue?

I am facing an issue where the placeholder is not appearing on both <TextInput> elements, and when the user inputs something, it does not show up in those <TextInput> boxes. I need to understand why this behavior is occurring. Below is the con ...

Rendering data in React using the array indexRendering data in React

I'm encountering an issue in React JS where I need to display all data from a REST API with a numeric index. REST API: [ { "id": "1", "start_date": "2020-05-08 09:45:00", "end_date": "2020-05-08 10:00:00", "full_name": "mirza", "cust_full_name": "fu ...

What is the best way to delay the fetch operation until the redux state is ready?

When fetching data from a database, I need it to differ depending on the user. I attempted to achieve this by passing the userid as a query parameter. However, the id is stored in a redux state which takes some time before it is available. Despite using if ...

Incorrect viewport widths in Vue Bootstrap fluid containers

I require a container that spans 100% of the available width until it reaches a specific breakpoint (max-width). Upon exploring the official documentation, I came across the responsive fluid container, which appeared to be a suitable choice. Responsive ...

The Importance of Strict Contextual Escaping in ReactJS

When making an API call, we receive a URL in the response. In Angular JS, we can ensure the safety of this URL by using $sce.trustAsResourceUrl. Is there an equivalent function to trustAsResourceUrl in React JS? In Angular, //Assuming 'response&apos ...

What is the solution for addressing the size problem of the image within the modal?

img1 img2 Upon clicking on the modal, it displays the image similar to a lightbox, but the issue arises when the image becomes scrollable and does not fit the screen properly. Take a look at the code below: HTML <div class='row'> ...

Challenges with moving images in Unslider

There seems to be an issue with the unslider-arrows on the unslider banner. The images are not sliding properly when transitioning to the next image without user input. Instead, they resize from small to full size starting at the top of the .banner div. ...

Removing outlines on <p> <a> or <div> elements with Ionic and Angular seems to be a challenging task

Hey there, I’m currently working on my application which includes a login page. I've implemented a “Forgotten password ?” link, but no matter what I try, like using .class and :focus {outline: none}, I still see a yellow square around the item. ...

What is the process for integrating a personalized font into the <head> section of the ConvertTo-HTML?

I created a script to generate an HTML file containing information about the services on a computer, along with some additional styling. $a = "<style>" $a = $a + "BODY{background-color:peachpuff;}" $a = $a + "TABLE{border-width: 1px;border-style: so ...

VueJS Error: Unable to access the 'className' property of an undefined variable

I'm currently working on a menu design project where I need to highlight the active tab/page that the user is on by adding a color class and utilizing JavaScript to update the active link. Here's a snippet of my template: <div class="menu ...

Using custom or external fonts icons in Chrome Packaged Apps involves the following steps:

Seeking to enhance the appearance of my Chrome Packaged App built in AngularDart, I searched for external icons online but came up empty-handed. Despite attempting various strategies and implementing the workaround provided below, I have been unable to ach ...

"Struggling to divide CSS Code with PHP containing nested brackets? Here's how you can tackle it

I'm currently attempting to break down my CSS code stored in variables, but I consistently encounter issues when dealing with media queries or similar elements. While I don't require every single rule to be outlined, I am looking for a solution ...

The NVD3 chart embedded in a React application generates HTML elements that persist on the page indefinitely without fading away

In my React application, I integrated an NVD3 scatter chart. However, I have encountered an issue with the tooltip not disappearing when hovering over the chart: https://i.stack.imgur.com/6lLok.png After moving the cursor away from the chart, the tooltip ...

Comparison between a reusable React hook and a convoluted JavaScript response organization

I am currently developing a web application using React. In my code, I have numerous calls to the database that all share a very similar structure. The only variances among these calls are the table names and parameters, resulting in a multitude of functio ...

What is the best way to display API error messages to the user?

When a user tries to upload a file that is not an image, I need to display the error message returned from a REST API. The JSON response received from the API will look something like this: { "publicError": "Could not be uploaded, it is not an image! ...

Menus that mimic the style of Google Translate's select options

I'm looking to design an HTML select menu using CSS in a similar fashion to Google Translate's style on Google Translate. I've searched for tutorials online, but they all involve jQuery. Is there a way to achieve something similar using only ...