-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Button | Typescript | Add TS overrides for top level button props (in addition to variant, color, and size) #28754
Comments
<Button rounded /> |
That's an excellent point! My codesandbox example is throwing a warning about just that, so I guess we would also need a way to specify additional props for I really wasn't sure how the community would feel about this pattern. I don't know of anywhere else where MUI lets you add props like this. It just seems to fit my use case so naturally I thought it was worth discussion. |
We also came across this issue. Also I would like to point out: There is also no possibility to introduce custom variants/props for a lot of other components. We would like to have this also for ToggleButton(Group), Tab(s), etc. Having a high Level Prop Override like ButtonPropsOverride for every component would be a good thing to have and would solve all of our current issues we are facing while implementing a complete styleguide in components. |
The only way (at this moment) to extend props on a component is to wrap the component with // ButtonWithRounded.tsx
import { shouldForwardProp as baseShouldForwardProp } from '@mui/system';
const shouldForwardProp = prop => baseShouldForwardProp(prop) && prop !== 'rounded'; // to prevent the prop from spreading to DOM
const ButtonWithRounded = styled(Button, { shouldForwardProp })<{ rounded?: boolean }>(...)
export default ButtonWithRounded and now you have to always import Button from this path instead of My proposed solution is to make props extendable at the global config level (cannot do it via theme), something like this: import { NonForwardedProps } from '@mui/material';
// to not spread `rounded` prop to DOM
NonForwardedProps.set('Button', ['rounded']);
// to have typed safe, Button need to provide extra type that can be augmented
declare module '@mui/material/Button' {
interface ButtonExtraProps {
rounded?: boolean
}
}
createTheme({
components: {
MuiButton: {
variants: [
{
props: { rounded: true },
style: {
borderRadius: 50
}
}
]
}
}
}) Now, I can use the Button as usual. import Button from '@mui/material/Button';
<Button rounded>Rounded</Button> POC branch: siriwatknp@5d4a951 |
Thanks for your work on this. It looks like you tried to add |
Thanks a lot, that you work on this, @siriwatknp !! What I do not understand: You defined Sidenote: |
lol |
Just to add another voice of support here, my team would also love the addition of interfaces we could extend for adding custom props to any component through the |
Any update on this? It would be great if we could add custom props to the top level interface, this way we could just use overrides to customize all those components that don't need custom logic. We can do this already with a few components like the |
Another option I can think of is to leverage the Developers won't have to deal with forwarded props because What does it look like with this approach? Developers have to extends the declare module '@mui/material/Button' {
interface ButtonOwnerState {
rounded?: boolean;
}
}
<Button ownerState={{ rounded: true }} /> // ✅ typed-safe
// theming should also work with no afford
theme={{
components: {
MuiButton: {
styleOverrides: {
root: ({ ownerState }) => ({
...ownerState.rounded && {
borderRadius: '20px',
}
})
}
}
}
}} What's needs to be done?
|
@siriwatknp it could work, but I am not sure that is the way we should teach people to use going forward. If we want to support this feature we should make it official and allow adding new props as first class prop, make sure we whitelist which props are going on the DOM elements etc. From semantics point of view, |
@siriwatknp are there any updates for this issue? (Add TS overrides) |
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 tovariant
,color
, andsize
. 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 aRoundedButton
component which wrapped Button. I've been been looking forward to droppingRoundedButton
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
, andtext-rounded
, then I would need to duplicate styles from each of the default variants. Using arounded
prop handles this very naturally without needing to duplicate styles or have a wrapping component.Button.d.ts already has
ButtonPropsVariantOverrides
,ButtonPropsColorOverrides
, andButtonPropsSizeOverrides
. I'm proposing adding a top levelButtonPropsOverrides
.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.
The text was updated successfully, but these errors were encountered: