mui / mui/material-ui

Button | Typescript | Add TS overrides for top level button props (in addition to variant, color, and size)

Open
#28,754 13 comments 18 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

scope: button type: new feature
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

theme.components.MuiButton.variants allows you to apply styles based on matching props. This works with arbitrary props, but the TS is only set up to let you add values to variant, color, and size. I believe adding new props here should be valid.

Examples 🌈

https://codesandbox.io/s/mui-5-custom-button-props-sd9pp?file=/src/App.js

Motivation 🔦

My app uses a standard and rounded version of each MUI Button variant. In v4 this was accomplished with a RoundedButton component which wrapped Button. I've been been looking forward to dropping RoundedButton in favor of v5 custom variants.

I found that adding additional variants is not really what I needed. I would need to add contained-rounded, outlined-rounded, and text-rounded, then I would need to duplicate styles from each of the default variants. Using a rounded prop handles this very naturally without needing to duplicate styles or have a wrapping component.

const theme = createTheme({
  components: {
    MuiButton: {
      variants: [
        {
          props: { rounded: true }, // this works great! but there is no way to update the TS
          style: {
            borderRadius: 50
          }
        }
      ]
    }
  }
});

Button.d.ts already has ButtonPropsVariantOverrides, ButtonPropsColorOverrides, and ButtonPropsSizeOverrides. I'm proposing adding a top level ButtonPropsOverrides.

If you think this is a useful pattern, I'd be happy to make a PR updating the TS and adding an example to the docs.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with packages/mui-material/src/Button/Button.d.ts, especially the existing ButtonPropsVariantOverrides, ButtonPropsColorOverrides, and ButtonPropsSizeOverrides declarations. Review the theme.components.MuiButton.variants usage and the linked CodeSandbox example; done means arbitrary top-level button props are accepted by TypeScript and the documented example works.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.