Skip to content
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

possibility to override defaultProps of custom styled components #42248

Open
dberardo-com opened this issue May 15, 2024 · 8 comments
Open

possibility to override defaultProps of custom styled components #42248

dberardo-com opened this issue May 15, 2024 · 8 comments
Assignees
Labels
new feature New feature or request package: system Specific to @mui/system

Comments

@dberardo-com
Copy link

dberardo-com commented May 15, 2024

Summary

following this i am creating themed components: https://mui.com/material-ui/customization/theme-components/#creating-new-component-variants

styled overrides work fine, but now i would like to override defaultProps from the theme as well, this is not possible currently.

would it be possible at least to override defaultProps from inside the styled()({ .... }) declaration ?

Examples

No response

Motivation

No response

Search keywords: theme components

@dberardo-com dberardo-com added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label May 15, 2024
@imadx
Copy link

imadx commented May 15, 2024

@dberardo-com
Copy link
Author

thanks for the quick reply.

i would like to keep defaultProps for native MUI element untouched, and change them only for my custom elements

@imadx
Copy link

imadx commented May 15, 2024

In that case, have you considered overriding the default props inside the custom component?

image

@dberardo-com
Copy link
Author

this would work of course, but i am not defining a custom component in that way, but simply using styled:

export const MyButton = styled(Button, {name: "MyButton", slot: "root"})({})

@imadx
Copy link

imadx commented May 15, 2024

Got it, Thanks for clarifying.

I'd say the styled components are for defining the styles.
But your idea of using prop overrides for creating new components is actually interesting. Let's wait for the thoughts from the maintainers.

@dberardo-com
Copy link
Author

i see ... i thought that was the whole point of it :D

thanks for the reply

@zannager zannager added the package: system Specific to @mui/system label May 16, 2024
@brijeshb42
Copy link
Contributor

As of right now, it's not supported the way you are expecting. We could probably add a way to override default props through the second argument of styled -

const MyButton = styled(Button, {
  defaultProps: {
    // new props
  }
})()

But I'd say this would be least on our priorities right now.

@DiegoAndai Your thoughts ?

@brijeshb42 brijeshb42 assigned DiegoAndai and unassigned brijeshb42 May 17, 2024
@brijeshb42 brijeshb42 added new feature New feature or request and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 17, 2024
@dberardo-com
Copy link
Author

thanks for evaluating this suggestion. indeed i can wrap the styled component around a functional one for now, but i just wanted a more elegant solution.

i think it would be enough to make this point more explicit from the documentation, in fact i am still wondering what can be overriden within the theme for themed styled component. i am assuming that only the "stylesOverride" part of the theme has an impact on the themed styled component, but again, that is just my assumption. i guess it would be great if the doc clearly states that, so no misambiguity arise.

btw FYI what i am trying to achieve here is the following:

  • every user of my app has the ability to upload a logo picture
  • the app loads a different MUI theme per each user
  • the link to the logo is defined as a MyCustomCardMedia.defaultProps.image attribute of the theme

for now i use stylesOverride, but just wanted to know whether i could achieve that with defaultProps, which seems not to be the case.

cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request package: system Specific to @mui/system
Projects
None yet
Development

No branches or pull requests

5 participants