-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Documentation for extending theme is wrong. Module Augmentation does not work. #29008
Comments
It doesn't look like this bug report has enough info for one of us to reproduce it. Please provide a CodeSandbox (https://material-ui.com/r/issue-template-latest), or a link to a repository on GitHub. Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve |
Here's my attempt at a CodeSandbox version of my encounter with this issue: link I may very well be misunderstanding how TypeScript is supposed to work in this instance but this is the best I've made sense of it. |
I have same problem but fix doesn't work for overriding subfield like adding some fields in mixins : Subsequent property declarations must have the same type. Property 'mixins' must be of type 'Mixins', but here has type 'Mixins & { border: (color: string, width?: string | number | undefined, style?: string | undefined) => string; }'.ts(2717) |
Maybe repository should contain a minimal tested example of theme with module augmentation for better understanding and testing |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
+1 as the docs aren't clear. Would be useful to have more information on extending the theme in typescript (my particular case, extending the palette) |
The |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Can you share specific struggle that you have. We have an example of this in https://mui.com/material-ui/customization/theming/#custom-variables as already linked. What exactly is not clear and what are the suggestion for improvements? |
I've included a small sandbox that showcases typical use cases:
https://codesandbox.io/s/romantic-gould-sbkc08 ^ Codesandbox errors are the side product of it not playing nicely with declaration files; copy-paste both @mnajdova I think what the MUI docs could still offer to explain is:
|
This comment was marked as resolved.
This comment was marked as resolved.
Thanks for sharing your suggestions @MauricePasternak. I agree, we can extend/improve the docs around this. cc @samuelsycamore @siriwatknp |
@MauricePasternak I have done the same thing for mixins as your CodeSandBox but it gives error: Error: btw I have |
I am on this. |
For version v4 this worked for me.
|
#29008 (comment) works. Any idea how to make this work in your codesandbox? <Checkbox color='orange'></Checkbox> After using module augmentation to add a color to the palette, I would like it to appear as an autocomplete option for Edit: I guess this way: declare module '@mui/material/Button' {
interface ButtonPropsColorOverrides {
custom: true;
}
} |
Thanks for this #29008 (comment) . Solved it for me as well. I was looking for a solution for a while now. I agree that documentations could be a little clearer especially regarding overriding Palette, Breakpoints etc. |
@mnajdova Reopening as it seems we agreed to improve this, but there is still some confusion. |
Absolutely agree, I've already spent two days looking through various opened and closed, new and old, MUI issues much like this one; and I still don't have working module augmentation from the library in my consumer application and feel completely clueless as there's no errors to debug, only that the augmentations doesn't happen. I feel like I've tried everything but in reality I barely know what's going on given the poor documentation of Typescript, Vite and MUI with this regard, and many examples either don't apply (like #31097) or wont work anymore. The closest I've been to have it working is via exporting the Interface I am using to augment from the components library and then doing the module augmentation in the consumer application. But then I get errors when using createTheme since for some reason the consumer application's ThemeOptions doesn't match that of the component library, so I can't compile even though it works running the application. |
Update: After three days of trying things, I finally got module augmentation from my library in my consumer application working with ViteJS. I decided to document it:
I am sure there's many things I am missing or still don't understand about node bundlers and typescript modules and globals, but it's the best I've got. To be honest, I wish MUI had more explicit examples of this, but I know ViteJS is also part of my problem. |
After some more tinkering to get it to work every time I stumbled upon the fact that my project was using version 5 of MUI and the test projects that I made were both version 6. I hadn't noticed. Because of this I was trying module augmentation with a MUI v5 project and MUI v6 library components, therefore typescript broke. Using the same version works. |
In https://mui.com/customization/theming/#custom-variables the docs tell you to use following example to extend typescript type but it is not working:
Current Behavior 😯
Current example in docs does not work.
Expected Behavior 🤔
Example should work.
Steps to Reproduce 🕹
Follow the docs.
The following type does work instead:
Your Environment 🌎
`npx @mui/envinfo`
The text was updated successfully, but these errors were encountered: