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"
      }
    },

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

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

What is the method for positioning the helperText on the far left in the TextField variant="outlined" in Material-UI for React?

If you find that the helperText "Some important text" is not aligned to the far left by default, what steps can be taken to make the adjustment? https://i.stack.imgur.com/i11ac.png ...

Unable to resubmit form consecutively in React

In the process of creating a registration/login system for a MERN app, I encountered an issue. Everything works fine when registering for the first time - I am redirected to the verification page, submit my OTP, and then redirected to the home page (login/ ...

Managing repeated selections in a dropdown menu - MUI Select

Currently, in my implementation using MUI v5, I am faced with a scenario where I have two options with the same name. The issue arises when both of these options are selected simultaneously using the Select component. I am looking for guidance on how to r ...

Ways to designate choices as chosen

Is there a way to set the selected attribute for options inside the <SelectArrayInput> component? I haven't been able to figure out how to do that yet. I've attempted adding the selected attribute, but it doesn't seem to be working. ...

Efficiently loading images asynchronously for smooth execution of JavaScript using Splide. Resolving Largest Contentful Paint (LCP) issue specifically on mobile

My webpage features a carousel of images at the top, created with Splide. However, I have noticed that my LCP score is significantly lower on mobile devices compared to desktop. The culprit seems to be in the Load Delay section: Time to first byte (TTFB) ...

Place centered items within a flexbox that uses space-between alignment

When designing a navigation section, I am looking to achieve space-between justification. However, for smaller screens where the navigation items may need to wrap onto multiple rows, I want the items to center themselves instead of aligning to the left whe ...

Error: Unable to access the 'props' property of an undefined variable in ReactJS

I'm encountering an issue while developing a basic ReactJS app, and I'm puzzled about the error's origin. https://i.sstatic.net/RCKxy.png This is the code snippet from my App.js file. import React from 'react'; import { connect } ...

The parent div will not accommodate two nested divs

I'm having an issue with two divs inside a parent div not being contained within the container when I inspect the page, even though I've wrapped the div around the other 2 divs. My goal is to apply attributes to the parent div instead of each in ...

setting a variable to true within a form submission function

In the midst of my current project, I am encountered with a challenge involving an accordion containing checkboxes. These checkboxes are supposed to populate the trueItems array when their value is set to true. Additionally, there is another array called f ...

The React Stripe API is functioning perfectly on local servers but encountering issues in the live environment

Just when I thought I was almost finished, reality hits me hard! My deadline is right around the corner! I finally got everything working on my local machine with a stripe payment form. However, when I pushed it live, I received an error message from my A ...

When the mouse hovers over the text inside a div, the link does not work in Internet Explorer

Here is some code for demonstration. I'm facing an issue where the link spans over the entire div, which works perfectly in Firefox. However, in IE, when I hover over the text, the cursor changes to a text cursor and the link doesn't work there. ...

Execute JavaScript function on click event in NextJS

Is it possible to execute a JavaScript function on the client side without using addEventListener? This situation works with addEventListener. MyComponent.js import Script from 'next/script' export default function MyComponent({ props }) { ...

Cannot extract the 'name' property from 'e.target' because it is not defined

I encountered an error message stating that I cannot destructure the property 'name' of 'e.target' because it is undefined within the createform() method. Despite highlighting the line causing the error, I am still unable to comprehend ...

Change from one value to another using a decaying sinusoidal wave

Can someone help me come up with a formula that will smoothly transition from a starting value to an end value over a specified time using a Sin or Cos wave? I'm attempting to replicate a bouncing effect like the one shown in my sample using CSS and ...

How can I create a CSS animation for a box element?

During my current project, I have the task of creating a box that will display an animation within 2 seconds and move from one corner to another. What is the most straightforward way to achieve this? ...

Steps for incorporating a quantity option in ReactJS for PayPal transactions

I am having trouble adding the quantity of an order. The code I have doesn't seem to be working properly. It works fine without specifying the quantity, but then it defaults to 1. Additionally, I'm unsure how to add a second product as it's ...

What is the best way to eliminate the Mat-form-field-wrapper element from a Mat-form-field component

I have implemented Angular mat-form-field and styled it to appear like a mat-chip. Now, I am looking to remove the outer box (mat-form-field-wrapper). <div class="flex"> <div class="etc"> <mat-chip-list i18n-aria-lab ...

What is the best way to display the weather information for specific coordinates in ReactJS?

I've been working on a code that retrieves farm details based on longitude and latitude. My goal now is to fetch the weather information for that specific farm using openweathermap However, each time I attempt to do so, an error message {cod: '4 ...

In Internet Explorer 8, the Radmenu hover menu may appear below surrounding Radmenus

Utilizing a radmenu, I am able to dynamically generate a submenu structure by examining folders in sharepoint document libraries. However, when placing multiple controls on the page, the root menu from other controls overlaps with the submenu of the curren ...

Encountering an "Invalid hook call error" while utilizing my custom library with styled-components

I recently developed my own custom UI tool using the styled-components library, integrating typescript and rollup for efficiency. One of the components I created looks like this: import styled from 'styled-components' export const MyUITest2 = s ...