Skip to content

[Button] styled overrides of variants as generated by the jss-to-styled codemod is not appliedΒ #29872

@Lani

Description

@Lani

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

Style overrides made with the styled function are not applied to the Button component when the styles are specified with the button class name with the variant suffix, like the & .MuiButton-contained selector.

const StyledButton = styled(Button)<ButtonProps>(
  ({ theme, color = "primary" }) => ({
    "& .MuiButton-contained": {
      backgroundColor: "red",
      "&:hover": {
        color: theme.palette[color].main,
        backgroundColor: "white"
      }
    }
  })
);

export default function MyButton(
  props: Pick<ButtonProps, "variant" | "color" | "children">
) {
  return <StyledButton {...props} />;
}

This is how the jss-to-styled code mod genreates the upgrade from the old JSS solution. With the difference of the class name prefix is matching the created component name, in this case MyButton instead of MuiButton, but this does not work either.

Expected behavior πŸ€”

The styles should be applied to the contained variant. In the example this means that the background color should be red and the hover color white.

Steps to reproduce πŸ•Ή

See codesandbox example: https://codesandbox.io/s/mui-v5-styled-button-override-probs-hdr79

Edit1:

Context πŸ”¦

I'm trying to run the jss-to-styled codemod to minimize the manual work that needs to be done when upgrading from Material-UI v4 to MUI v5.

Your environment 🌎

`npx @mui/envinfo`
 System:
    OS: Windows 10 10.0.19042
  Binaries:
    Node: 14.15.4 - C:\Tools\nodejs\node.EXE
    Yarn: Not Found
    npm: 6.14.10 - C:\Tools\nodejs\npm.CMD
  Browsers:
    Chrome: 95.0.4638.69
    Edge: Spartan (44.19041.1266.0), Chromium (96.0.1054.29)
  npmPackages:
    @emotion/react: ^11.5.0 => 11.5.0
    @emotion/styled: ^11.3.0 => 11.3.0
    @mui/icons-material: ^5.1.0 => 5.1.0
    @mui/lab: ^5.0.0-alpha.56 => 5.0.0-alpha.56
    @mui/material: ^5.1.0 => 5.1.0
    @mui/styles: ^5.1.0 => 5.1.0
    @mui/system: ^5.1.0 => 5.1.0
    @mui/types:  7.1.0
    @types/react:  17.0.34
    react: ^17.0.2 => 17.0.2
    react-dom: ^17.0.2 => 17.0.2
    typescript:  4.4.4

Note: Local MUI version tested is v5.1.0, but the codesandbox is using v5.2.0.

Tested on Windows in:
Microsoft Edge: 96.0.1054.29 (Official build) (64-bit)
Firefox: 94.0.2 (64-bit)

Metadata

Metadata

Assignees

No one assigned

    Labels

    not plannedThe problem seems valid, but we don't intend to fix it (won't fix).package: codemodSpecific to codemod.scope: buttonChanges related to the button.type: new featureExpand the scope of the product to solve a new problem.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions