Customizing Material-UI: A guide to overriding inner components

Looking to customize the styles of a Material UI Switch for the small variation? I have applied global styles but now want to override the styles of elements like track and thumb. Targeting the root element with the sizeSmall class is working, but unsure of the correct approach to modifying inner elements.

Should I use something like & $sizeSmall within track or vice versa?

Check out the code sandbox: https://codesandbox.io/s/material-demo-forked-vkdsh?file=/theme.js

Here's the relevant theme override:

MuiSwitch: {
      root: {
        width: 96,
        height: 40,
        padding: 0,
        margin: 8,
        borderRadius: 48,
        boxShadow:
          "inset -3px -3px 7px #FFFFFF, inset 3px 3px 7px rgba(156, 156, 156, 0.48);"
      },
      sizeSmall: {
        width: 60,
        height: 18,
        borderRadius: 9,
        padding: 0
      },
      switchBase: {
        marginTop: 7,
        marginLeft: 12,
        padding: 1,
        "&$checked, &$colorPrimary$checked, &$colorSecondary$checked": {
          transform: "translateX(46px)",
          color: "#fff",
          "& + $track": {
            opacity: 1,
            border: "none",
            background:
              "linear-gradient(172.54deg, #2B8CFF -34.02%, #0354CE 124.23%);"
          }
        }
      },
      thumb: {
        width: 24,
        height: 24,
        boxShadow:
          "2px 1px 8px rgba(185, 185, 185, 0.3), inset 0px -1px 1px rgba(95, 95, 95, 0.4);"
      },
      track: {
        margin: "auto 16px",
        height: 8,
        borderRadius: 9,
        border: "1px solid #D3D4D5",
        backgroundColor: "#fafafa",
        opacity: 1,
        transition:
          "background-color 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms,border 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms"
      }
    },

Answer №1

Check out the CSS classes available, including one called track. This can be customized to affect all instances. For individual targeting, utilize the classes prop and specify the track class.

<Switch classes={{ root: someClass, track: trackClass}}/>

Answer №2

For a better understanding of how material-ui deals with this, you can refer to this link. It will give you a more precise approach to what you are attempting to accomplish :)

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

Sneak beneath another element with a div's sliding movement

I'm attempting to make an absolutely positioned div element (#slideout) slide underneath another div element .menu using CSS, if possible. Check out my code here. Currently, when the red tab is clicked, #slideout covers .menu, but I want it to hide ...

How can one append a string of text to the right of each bar? Let's find out!

.chart div { font: 10px sans-serif; background-color: steelblue; text-align: right; padding: 3px; margin: 1px; color: white; } </style> <div class="chart"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5 ...

Is it possible to maintain consistent tick sizes on recharts axis without compromising the maximum value?

My data array is structured as shown below and I am looking to present it in a specific way using recharts. const severityExampleData: any = [ {symptom_type: 'chest pain', mild: 1, moderate: 2, severe: 1, total: 4}, {symptom_type: 'p ...

Steps for updating a text section beneath a carousel

I'm looking to enhance my webpage with a bootstrap carousel, which is pretty standard. However, I want the content under each slide to change dynamically, but I'm struggling to make it work. As a newbie, I could use some guidance. I've atte ...

The designation for the active tab in Material-UI Tabs needs to be more precise

Click here to access the Code Sandbox: https://codesandbox.io/s/ypx4qpjvpx Highlighted sections from the code snippet: const styles = theme => ({ root: { flexGrow: 1, backgroundColor: theme.palette.background.paper }, label: { font ...

What is the best way to transmit a Blob object to the server without it arriving empty?

I'm currently facing an issue where I am attempting to send a Blob object containing video data to my server for upload to my S3 storage. However, the Blob is arriving as an empty object on the server side. const blob = new Blob(videoChunks, { t ...

Using JavaScript to modify the -webkit-animation-play-state

Hello, I'm currently facing a challenge in changing my css3 -webkit-animation-play-state property from paused to running upon clicking on another div. Does anyone have any suggestions on how I can achieve this? I believe JavaScript might be the way to ...

What is the best way to trigger a new css animation on an element that is already in the midst of

Let's talk about an element that already has an animation set to trigger at a specific time: .element { width: 100%; height: 87.5%; background: #DDD; position: absolute; top: 12.5%; left: 0%; -webkit-animation: load 0.5s ease-out 5s bac ...

Creating a table with adjustable row heights is a great way to enhance the user

Can the height of an HTML table row be adjusted dynamically by dragging and dropping, similar to how it's done in this demo (https://reactdatagrid.io/docs/row-resize), but for free? ...

Encountering the error message "string spec is required" when trying to run npm install

I am facing an issue while trying to run a react app that I developed two years ago. Upon running the app using "npm install", I encountered the following error: npm ERR! must provide string spec npm ERR! A complete log of this run can be found in: npm ERR ...

What is the best way to eliminate the white margin that is showing up on the right side of my website?

Here's a question that has been bugging me recently... So, I've been working on creating my own version of an Airbnb website called 'tombnb' for a few weeks now. Everything was going smoothly until I encountered a persistent issue. Th ...

Redirecting page after user authentication in ReactJS

Is there a way to redirect a user back to the page they were on (such as a detail page) or to a specific page after successfully logging in using React, similar to request.GET.next in Django with regular templates? I am utilizing Formik and Django Knox aut ...

Enhance your SVG image in D3 by incorporating a drop-shadow effect

Trying to apply a drop-shadow effect to an SVG image using D3. Check out my code below and see the example block here: var imgurl = 'http://www.logo-designer.co/wp-content/uploads/2015/08/2015-Penn-State-University-logo-design-4.png'; var mar ...

Error message stating "The file 'routes-manifest.json' was not found during NextJs deployment on Vercel"

My Next.js application was functioning correctly until I added some files to update my code. Now, it is failing to deploy on Vercel and showing this error message: https://i.stack.imgur.com/2y7CB.png Despite searching online for solutions, I haven't ...

ERROR : The value was modified after it had already been checked for changes

Encountering an issue with [height] on the main component and seeking a solution Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '753'. Current value: '731'. I have th ...

Struggling to craft an accurate GraphQL request to Yelp

As a newcomer to Apollo, I am facing some challenges while trying to send a basic GraphQL query to the Yelp server. import React from 'react'; import { render } from 'react-dom'; import ApolloClient from 'apollo-boost'; impor ...

Having trouble with script tag not loading content in Next.js, even though it works perfectly fine in React

Currently, I am attempting to utilize a widget that I have developed in ReactJS by utilizing script tags as shown below- React Implementation import React from "react"; import { Helmet } from "react-helmet"; const Dust = () => { ...

Issue with the button border not functioning correctly upon hover

I'm currently practicing my HTML and CSS skills, with a focus on creating buttons. I have encountered an issue where I want a colored border to appear around the button when hovered over. Unfortunately, I seem to be stuck and unable to achieve the des ...

Bootstrap requires Visual Studio Code live-server plugin to function properly

I have been utilizing the Visual Studio Code IDE for my coding projects, along with a helpful plugin called "live server" that allows for quick checks of code changes. Everything was running smoothly with Bootstrap until I encountered an issue. When I att ...

Choose a different configuration for the shrink property in the OutlineInput label compared to the TextField if it is empty

Using a TextField component with InputLabelProps={{shrink: true}} keeps the Label at the top of the TextField, cutting out the outline for better Label display. However, when using the Select component: <FormControl variant={ this.props.variant } cla ...