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

[Bug]: No option for declaring a variant for IconButton in `createTheme()`

Open NashJames opened this issue 3 years ago • 2 comments

Duplicates

  • [X] I have searched the existing issues

Latest version

  • [X] I have tested the latest version

Steps to reproduce 🕹

Steps:

  1. Add a MuiIconButton to createTheme()
  2. Attempt to give it a variant prop

It's worth mentioning I'm using TypeScript, but had no trouble setting up a Button variant. It came up as an Intellisense option before I even declared the module.

Current behavior 😯

On IconButton, it does not even appear as a suggested option by VS Code Intellisense. And produces an error.

image

Expected behavior 🤔

No response

Context 🔦

I'd like to customise multiple IconButton styles to match my custom Button styles. I'm currently unable to.

Your environment 🌎

npx @mui/envinfo
  System:
    OS: Linux 5.10 Ubuntu 18.04.5 LTS (Bionic Beaver)
  Binaries:
    Node: 16.18.1 - ~/.nvm/versions/node/v16.18.1/bin/node
    Yarn: Not Found
    npm: 8.19.3 - ~/.nvm/versions/node/v16.18.1/bin/npm
  Browsers:
    Chrome: 107.0.5304.110
    Firefox: Not Found
  npmPackages:
    @emotion/react: ^11.10.5 => 11.10.5 
    @emotion/styled: ^11.10.5 => 11.10.5 
    @mui/icons-material: ^5.10.16 => 5.10.16 
    @mui/material: ^5.10.17 => 5.10.17 
    @types/react: ^18.0.25 => 18.0.26 
    react: ^18.2.0 => 18.2.0 
    react-dom: ^18.2.0 => 18.2.0 
    typescript: ^4.9.0 => 4.9.4 

NashJames avatar Dec 12 '22 22:12 NashJames

it looks like IconButton itself doesn't have variant prop. api docs : https://mui.com/material-ui/api/icon-button/#main-content

sai6855 avatar Dec 13 '22 09:12 sai6855

Is this intended or am I correct in reporting it? I only ask because I'd be curious to know why if it's deliberate.

NashJames avatar Dec 13 '22 23:12 NashJames

What would the variants for this component be? The same ones as Avatar?

samkevin1 avatar Dec 20 '22 19:12 samkevin1

Yes, you can create a custom variant for Material IconButton component.

e.g.,

const theme = createTheme({
  components: {
    MuiIconButton: {
      variants: [
        {
          props: { variant: "test" },
          style: {
            color: "red"
          }
        }
      ]
    }
  }
});

Here is a working Codesandbox: https://codesandbox.io/s/focused-shadow-cbfqwf?file=/demo.tsx

hbjORbj avatar Dec 27 '22 21:12 hbjORbj

@hbjORbj Have you got any explanation for the error then? Just tried it again and got the same.

image I've got a variant working perfectly for MuiButton.

A missing interface declaration like below errors differently so I doubt it's that. Also, not sure why your sandbox doesn't throw a TS error for a custom variant name without the override?

declare module '@mui/material/IconButton' {
  interface IconButtonPropsVariantOverrides {
    test: true
  }
}

If it's missing in the API docs, is it possible part of the implementation is unfinished?

NashJames avatar Dec 28 '22 13:12 NashJames

@samkevin1 , I also have cases for different variants But for my cases need same variants as in Button (text, outlined, contained) with ability to override and add custom variants

@hbjORbj , your example really failed with TypeScript, see last comment from @NashJames

vadimka123 avatar Jan 04 '23 12:01 vadimka123

Sorry for confusion. I was incorrect. variant prop is not supported for Material IconButton component. I would suggest you to simply customize the component.

hbjORbj avatar Jan 04 '23 21:01 hbjORbj

@NashJames I've got a variant working perfectly for MuiButton.

Yes, variant prop is supported for Material Button component. I think some peope expect the two components to support the same API. But they don't. Here is a relevant issue from the past: https://github.com/mui/material-ui/issues/18109

hbjORbj avatar Jan 04 '23 21:01 hbjORbj

@hbjORbj Not sure I understand why this was closed? Even if it isn't high priority, it should be open and tracked with possibly a good first issue tag.

Also, the closing comment for the linked issue is exactly what we can't do...

Feel free to customize it 👍

I'm not asking for the default variants Button has, just the actual variant prop existing on IconButton so we can actually customise it.

NashJames avatar Jan 06 '23 11:01 NashJames

Agreed, just adding variant prop (only one variant - default?) with ability to add custom variants and set all on theme level will working solution

vadimka123 avatar Jan 06 '23 23:01 vadimka123

Agreee need variant prop on MuiIconButton

borstessi avatar Mar 15 '23 10:03 borstessi

@NashJames @vadimka123 @borstessi Just came across the same issue today and found a solution for my project. Just in case if its still a relevant problem, maybe my workaround can help you until an official solution is released :)

Codesandbox https://codesandbox.io/p/sandbox/fragrant-dust-7hcs83?file=%2Fdemo.tsx%3A15%2C21

JulienMa94 avatar Jan 19 '24 15:01 JulienMa94