-
-
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
[Bug or Question] Custom component variants via augmented module is lost when re-export #35743
Comments
🤔 Somehow TypeScript does not pickup the module augmentation. I will try to reproduce it. |
@siriwatknp After hammering it over the weekend and today... I came up with an ugly workaround. In the newly separated library, we have our own theme provider there, something like this, example:
In order to get the
Once I have the above file, I go back to my
Once I built and installed the package... since the I don't know if this is the right way to do things and it seems a little hacky, and there is no official documentation from MUI on how to go with this. Please let me know whether this is ok (or official). I feel like, I should be somehow defining some kind of Thank you in advance! |
Try to put all declarations
in the same file there you have theme creation
|
@totszwai Appreciate the issue and the workaround, I think we need to have a documentation guide on how to build UI library of top of Material UI or Joy UI. |
The module augmentation for library used to work on
However, updating to |
@jaska120 Can you try this? import type {} from "@mui/material/styles";
+import type {} from "@mui/material/Typography";
declare module "@mui/material/styles" {
interface TypographyVariants {
h1Bold: React.CSSProperties;
h2Bold: React.CSSProperties;
}
interface TypographyVariantsOptions {
h1Bold?: React.CSSProperties;
h2Bold?: React.CSSProperties;
}
}
declare module "@mui/material/Typography" {
interface TypographyPropsVariantOverrides {
h1Bold: true;
h2Bold: true;
}
} |
@siriwatknp Thanks, this works indeed! In the previous version of |
I can confirm this bug And the workaround with Before, broken: declare module '@mui/material/Button' {
interface ButtonPropsVariantOverrides {
containedError: true;
}
} After, fixed: import type {} from '@mui/material/Button';
declare module '@mui/material/Button' {
interface ButtonPropsVariantOverrides {
containedError: true;
}
} |
@siriwatknp What is the suggested way of working in a monorepo? I have a package with the theme and its overrides/augmentations, but when installing it in a new package in the same monorepo, TypeScript is lost and complains even though the component is visually taking the correct values from the theme. |
@noeszc-clara We are importing the types from shared package successfully, which contains module augmentation e.g. with:
|
@jaska120 How are you exporting the types? |
We are not exporting them from shared package, but importing them by accessing the file structure of shared package instead. We don't use import map in package.json, and therefore we can reference files relative to main export as I wrote in my previous reply. We are in monorepo environment. I hope this answers your question. |
I also get this issue when I use nx.dev to implement an UI for module federation (Wrap mui to a npm package). I see example of @totszwai in Stack Overflow use with @myproject/theme. When you build Example when use @myproject/theme to another react project:
So in // Only use empty {} with no `.d.ts` in end of line
import type {} from '@myproject/theme/overrides/component/button' |
Duplicates
Latest version
Steps to reproduce 🕹
It is kinda impossible to create a live example since it requires a customized MUI theme library.
TL;DR:
Following the official documentation to define the custom variants:
https://mui.com/material-ui/customization/theme-components/#creating-new-component-variants
It only works if the definition is being used within the same project. Re-exporting a defined MUI theme will not expose/re-export the custom variants... meaning if I have 10 different projects sharing a custom theme, ALL the
declare module
will need to be duplicated everywhere and is a maintenance nightmare.There is a somewhat related issue #24323, is basically the father of what I am facing right now.
See Context below for long version.
Current behavior 😯
Extending the variants as well as the augmented module does not propagate to consumer apps.
Expected behavior 🤔
Extending the variants or the augmented module should propagate to the consumer apps.
Context 🔦
https://stackoverflow.com/questions/75022165/how-to-re-export-overridden-mui-theme-definitions
Long version:
I have a project that has all the MUI theme created, everything is working properly. Now I'd like to extract the defined theme out as a separate library (ex:
@myproject/theme
) such that I could share/redeploy it to various applications, that way, when the theme library gets updated, all downstream apps would inherit all the changes as well.In most cases, it worked fine, however I am having trouble propagating the overridden
variants
with MUI. For example, I have the followingvariants
defined for theButton
:So the above code used to work just fine within the project (before splitting it out as a lib), and VSCode would be able to see the added
toolbar
variant. However, once the theme is moved to a new project/library (ex:@myproject/theme
and then let's say I do,npm install -D @myproject/theme@0.0.1-alpha
), it would no longer recognizing the addedvariants
:Your environment 🌎
Using:
npx @mui/envinfo
The text was updated successfully, but these errors were encountered: