question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Switch] styleOverrides selector do not appear to work

See original GitHub issue

When writing styleOverrides for a Switch component, writing styles using rule name/global class selectors doesn’t work as expected (compared to writing overrides for other components in the same manner).

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

The overrides are written as they are for other components (e.g., buttons, inputs, etc.), exported, and imported into the top-level createMuiTheme.

function MuiSwitch() {
  return {
    defaultProps: {
      color: 'primary',
    },
    styleOverrides: {
      root: {
        border: '1px solid red !important',
        '&.MuiSwitch-root': {
          border: '1px solid red !important',
        },
        // other selectors defined at
        // the Switch component API doc
        // don't work either
      },
    },
  };
}

export function getSwitchOverrides() {
  return {
    MuiSwitch: MuiSwitch(),
  };
}

Expected Behavior 🤔

The component remains unaffected and reverts to its default appearance, no matter how many selectors/rule names/classes are in the styleOverrides object.

The component should respond to the overrides.

Steps to Reproduce 🕹

https://codesandbox.io/s/thirsty-sun-w3t8h

Steps:

  1. Create a switch component.
  2. Add overrides to it using the rules/selectors outlined in the documentation.

Context 🔦

While it’s possible to style the Switch component with useStyles, I’d rather have this as part of the overrides, so that no additional CSS is added to the application.

Your Environment 🌎

`npx @material-ui/envinfo`
System:
    OS: macOS 11.2.1
  Binaries:
    Node: 12.19.1 - ~/.nvm/versions/node/v12.19.1/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.6.0 - ~/.nvm/versions/node/v12.19.1/bin/npm
  Browsers:
    Chrome: 89.0.4389.128
    Edge: Not Found
    Firefox: 87.0
    Safari: 14.0.3
  npmPackages:
    @emotion/react: ^11.1.5 => 11.1.5
    @emotion/styled: ^11.1.5 => 11.1.5
    @material-ui/core: ^5.0.0-alpha.28 => 5.0.0-alpha.28
    @material-ui/icons: ^5.0.0-alpha.28 => 5.0.0-alpha.28
    @material-ui/styled-engine:  5.0.0-alpha.25
    @material-ui/styles:  5.0.0-alpha.28
    @material-ui/system:  5.0.0-alpha.28
    @material-ui/types:  5.1.7
    @material-ui/unstyled:  5.0.0-alpha.28
    @material-ui/utils:  5.0.0-alpha.28
    @types/react: 16.9.56 => 16.9.56
    react: 17.0.1 => 17.0.1
    react-dom: 17.0.1 => 17.0.1
    typescript: ~4.0.3 => 4.0.7

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
dohomicommented, Apr 29, 2021

Ok found the NextJS issue: Make sure that there is no own declaration of @emotion/cache inside package.json: https://github.com/dohomi/storyblok-mui-v5-webpack5/commit/d4af9ee813ce3e415bef6342a6ce3fa5f3865b24#diff-7ae45ad102eab3b6d7e7896acd08c427a9b25b346470d7bc6507b6481575d519L20 It seems that the cache otherwise gets out of sync

1reaction
mnajdovacommented, Apr 15, 2021

In this component we are not spreading the ...other on the root slot, so there is no theme in the props on the component, hence we get the default theme that doesn’t contain the overrides. This is fixing the issue:

diff --git a/packages/material-ui/src/Switch/Switch.js b/packages/material-ui/src/Switch/Switch.js
index 0914dfb154..1cca404f1d 100644
--- a/packages/material-ui/src/Switch/Switch.js
+++ b/packages/material-ui/src/Switch/Switch.js
@@ -13,7 +13,6 @@ import switchClasses, { getSwitchUtilityClass } from './switchClasses';

 const overridesResolver = (props, styles) => {
   const { styleProps } = props;
-
   return deepmerge(
     {
       ...(styleProps.edge && styles[`edge${capitalize(styleProps.edge)}`]),
@@ -219,7 +218,7 @@ const Switch = React.forwardRef(function Switch(inProps, ref) {
   const icon = <SwitchThumb className={classes.thumb} styleProps={styleProps} />;

   return (
-    <SwitchRoot className={clsx(classes.root, className)} styleProps={styleProps}>
+    <SwitchRoot className={clsx(classes.root, className)} styleProps={styleProps} theme={other.theme}>
       <SwitchSwitchBase
         className={classes.switchBase}
         type="checkbox"

@sparkpunk would you be interested in creating a PR? 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Switch] styleOverrides selector do not appear to work #25773
When writing styleOverrides for a Switch component, writing styles using rule name/global class selectors doesn't work as expected (compared ...
Read more >
Mui v5 styleOverrides not working with themes - Stack Overflow
The issue is that Select doesn't define any styles of its own at the root ... at the theme and applying the corresponding...
Read more >
Material UI style overrides don't work? : r/reactjs - Reddit
I'm trying to override the selected state of the ToggleButton inside a ToggleButtonGroup using the styleOverrides but can't seem to override ...
Read more >
Themed components - Joy UI - MUI
Use styleOverrides to apply styles to each component slots. ... The styles can also contain any CSS selectors (support nested selectors), as such:....
Read more >
switch' (imported as 'switch') was not found in 'react-router-dom
This is a React Router v5 app import { BrowserRouter, Switch, Route, Link, ... mui/material-ui[Switch] styleOverrides selector do not appear to work#25773.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found