material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

[IconButton] Custom color causes TypeError

Open emlai opened this issue 2 years ago • 18 comments

Duplicates

  • [X] I have searched the existing issues

Latest version

  • [X] I have tested the latest version

Current behavior 😯

When I augment IconButton with new colors:

declare module "@mui/material/IconButton" {
  interface IconButtonPropsColorOverrides {
    textPrimary: true;
    textSecondary: true;
  }
}

and then try to use these colors:

<IconButton color="textPrimary">
  <CloseIcon />
</IconButton>

My app crashes with:

TypeError: Cannot read properties of undefined (reading 'main') 

at the following location:

https://github.com/mui/material-ui/blob/dec32b361714bed9c73ee1360c4b6390769bf9f5/packages/mui-material/src/IconButton/IconButton.js#L78

Expected behavior 🤔

IconButton doesn't throw TypeError when using custom color. Instead it handles it safely, like SvgIcon:

https://github.com/mui/material-ui/blob/dec32b361714bed9c73ee1360c4b6390769bf9f5/packages/mui-material/src/SvgIcon/SvgIcon.js#L54

Steps to reproduce 🕹

No response

Context 🔦

No response

Your environment 🌎

No response

emlai avatar Jun 07 '22 09:06 emlai

The customization capability is not complete. I marked this as an enhancement.

The fix can use optional chaining like in SvgIcon. However, please note that when you customize the color prop, you are opting out of the theme palette so you have to style by:

createTheme({
  components: {
    MuiIconButton: {
      root:  ({ ownerState, theme }) => ({
        ...ownerState.color === 'textPrimary' && {
          // your style
        }
      })
    }
  }
})

siriwatknp avatar Jun 07 '22 11:06 siriwatknp

The customization capability is not complete.

Ah that explains it.

I worked around it by passing the color to the icon component instead, and augmenting SvgIconPropsColorOverrides:

<IconButton>
  <CloseIcon color="textPrimary" />
</IconButton>

emlai avatar Jun 07 '22 12:06 emlai

Is this issue open to working on? I'd like to take it. @emlai

Shubhamchinda avatar Jun 08 '22 01:06 Shubhamchinda

@Shubhamchinda I don't think it's being worked on, so feel free to take it. Thank you!

emlai avatar Jun 08 '22 11:06 emlai

@Shubhamchinda I don't think it's being worked on, so feel free to take it. Thank you!

Great, thanks. I'll take this.

Shubhamchinda avatar Jun 10 '22 02:06 Shubhamchinda

@Shubhamchinda @emlai

Any progress? Would love to implement this PR

geraldaddey avatar Jun 21 '22 15:06 geraldaddey

@Shubhamchinda @emlai

Any progress? Would love to implement this PR

@geraldaddey Give me a day, and I'll submit a PR.

Shubhamchinda avatar Jun 23 '22 08:06 Shubhamchinda

Is this issue still open to work on? Would like to work on it

shachargiladi avatar Jul 23 '22 21:07 shachargiladi

@shachargiladi 4 weeks since the last response, so I think it was abandoned. I'd say go ahead!

emlai avatar Jul 24 '22 12:07 emlai

Hi is anyone working one this .I'd like to take it. @emlai

hilalsidhic avatar Aug 06 '22 17:08 hilalsidhic

Hi is anyone working one this? I'd like to take it. @emlai

cyberGHostJs avatar Aug 08 '22 22:08 cyberGHostJs

Hi anyone still working on this? I'd like to take it. @emlai @cyberGHostJs @hilalsidhic

rizamoyi avatar Sep 08 '22 10:09 rizamoyi

Hey, doesn't seems to be an issue for me :thinking: . I was thinking to pick this up, but it didn't reproduce for me. @emlai

kushagra010 avatar Sep 08 '22 11:09 kushagra010

My bad, reproducible. @rizamoyi have you started work on it or are you working on it? If not, I can pick this up.

kushagra010 avatar Sep 08 '22 13:09 kushagra010

@kushagra010 I started working on it.

rizamoyi avatar Sep 08 '22 13:09 rizamoyi

is this issue still on or can take it over and work on it?

harsh5902 avatar Sep 15 '22 08:09 harsh5902

@harsh5902 you can take it over

rizamoyi avatar Sep 15 '22 11:09 rizamoyi

I have solved the this problem and created a pull request, please do review it.

harsh5902 avatar Sep 18 '22 12:09 harsh5902

@harsh5902 Why was the PR closed?

paintoshi avatar Sep 28 '22 19:09 paintoshi

The PR to fix this issue should:

  • custom color works with TypeScript
  • make sure that the custom color does not crash
  • add a demo to show that the color prop can be augmented

@ZeeshanTamboli if you are available, this could be one to pick up.

siriwatknp avatar Sep 29 '22 04:09 siriwatknp

@ZeeshanTamboli if you are available, this could be one to pick up.

I am on a bit of a break currently and won't be working next 3-4 weeks much effectively. If somebody else wants to take a look till then, please go ahead.

ZeeshanTamboli avatar Sep 29 '22 06:09 ZeeshanTamboli

this is working fine i guess, i just tested it. at first you need to provide the props in Palette and PaletteOptions interface that lies in the @mui/material/styles/, the color names that you want to add into the project

declare module '@mui/material/styles' {
  interface Palette {
    primaryVariant500: Palette['primary'];
    secondaryVariant500: Palette['primary'];
  }

  interface PaletteOptions {
    primaryVariant500?: PaletteOptions['primary'];
    secondaryVariant500?: PaletteOptions['primary'];
  }
}

and add these lines for IconButton

declare module "@mui/material/IconButton" {
  interface IconButtonPropsColorOverrides {
    primaryVariant500: true;
    secondaryVariant500: true;
  }
}

these enabling the createTheme to be aware of these colors, then you need to provide the implementation in the theme that would be passed in the themeProvider, like so

export const theme = createTheme({
  palette: {
    primaryVariant500: {
      main: '#ff0000'
    },
    secondaryVariant500: {
      main: '#0000ff'
    },
 }
});

because we do want to let the mui know the values of the color. hope it helps. thanks.

code-theme result tsxCode

@emlai the problem for you might be that you're not writing up the color prop in createTheme for palette

the-mgi avatar Sep 29 '22 16:09 the-mgi

@the-mgi Yes, if your palette has the exact same key as the specified color, then this will work fine.

But we're intending to use color="textPrimary" or color="text.primary" to access the usual text.primary.main that we have in our palette. Currently neither of those work.

emlai avatar Sep 29 '22 17:09 emlai

@the-mgi Yes, if your palette has the exact same key as the specified color, then this will work fine.

But we're intending to use color="textPrimary" or color="text.primary" to access the usual text.primary.main that we have in our palette. Currently neither of those work.

got it. thanks.

the-mgi avatar Oct 01 '22 04:10 the-mgi

Hello there, is there any work around ? I'm still having the same problem.

mui.d.ts Screenshot from 2022-10-19 11-34-06

LightTheme.tsx Screenshot from 2022-10-19 11-34-49

Any component that I try to change the color Screenshot from 2022-10-19 11-36-35 Screenshot from 2022-10-19 11-49-04 Screenshot from 2022-10-19 11-53-43

Actually it works with Typography, but I think it's the only one Screenshot from 2022-10-19 11-38-37

@siriwatknp @the-mgi @kushagra010

TCsTheMechanic avatar Oct 19 '22 14:10 TCsTheMechanic

@TCsTheMechanic Workaround for IconButton is to pass the color prop to the icon component instead: https://github.com/mui/material-ui/issues/33054#issuecomment-1148613095

emlai avatar Oct 21 '22 11:10 emlai

@emlai also doesn't work

Screenshot from 2022-10-21 09-35-40

TCsTheMechanic avatar Oct 21 '22 12:10 TCsTheMechanic

@TCsTheMechanic Did you augment the typings:

declare module "@mui/material/SvgIcon" {
  interface SvgIconPropsColorOverrides {
    "textColor.light": true;
  }
}

emlai avatar Oct 21 '22 13:10 emlai

@emlai still nothing, I'm using @mui/icons-material, I don't know if it follows the same module

TCsTheMechanic avatar Oct 21 '22 14:10 TCsTheMechanic

I am not sure what is the issue here. Custom color does work with typescript with module augmentation and can be augmented. If the custom color is not defined in the palette it will crash. CodeSandbox: https://codesandbox.io/s/vigorous-bardeen-7pnhv6?file=/src/App.tsx

ZeeshanTamboli avatar Nov 04 '22 07:11 ZeeshanTamboli