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

[material-ui] Two themes customized using TypeScript theme augmentation are conflicting #40672

Closed
anasyo10 opened this issue Jan 18, 2024 · 4 comments
Labels
customization: theme Centered around the theming features package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information

Comments

@anasyo10
Copy link

anasyo10 commented Jan 18, 2024

Steps to reproduce

I am using mui v5 and creating a customised theme. I have two different frontend F1 and F2 and seperate theme for each frontend F1_theme and F2_theme both of them are customised using typescript via module augumentation. everything works fine if repo has one theme at a time either F1_theme or F2_theme but when repo has both theme at the time then it picks types on one theme and start validating both themes using that type.

Current behavior

No response

Expected behavior

No response

Context

No response

Your environment

npx @mui/envinfo
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: mui , mui theme , theme augmentation

@anasyo10 anasyo10 added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 18, 2024
@zannager zannager added package: material-ui Specific to @mui/material customization: theme Centered around the theming features labels Jan 18, 2024
@danilo-leal danilo-leal changed the title Two mui5 theme customized using typescript theme augmentation are conflicting [material-ui] Two themes customized using TypeScript theme augmentation are conflicting Jan 18, 2024
@ZeeshanTamboli
Copy link
Member

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), a link to a repository on GitHub, or provide a minimal code example that reproduces the problem.
Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve.

@ZeeshanTamboli ZeeshanTamboli added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Feb 6, 2024
@kyptov
Copy link

kyptov commented Feb 10, 2024

@ZeeshanTamboli don`t know why you need something to reproduce. This is how module augmentation works. MUI uses Typescript Declaration Merging and it works great when users merge MUI internal theme and one custom theme. But we can do it twice (or more times). The problem is that Typescript silently merges all themes in one global super theme. Which, of course, totally unwanted.

interface Theme {
  common: string;
}

// theme1.ts
interface Theme {
  theme1: string;
}

const myTheme1: Theme = {
  common: 'common',
  theme1: 'theme1',
};

// theme2.ts
interface Theme {
  theme2: string;
}

const myTheme2: Theme = {
  common: 'common',
  theme2: 'theme2',
};

Monorepo is become more popular nowdays, so this question will appear more often I suppose.

My workaround: do not use module augmentation.

  1. I use interfaces for custom Theme, ThemeOptions, Palette,... which extend mui types
  2. Create custom theme and give it to ThemeProvider
  3. I have useCustomTheme hook which returns my custom theme
  4. I reexport styled as CreateMUIStyled
    This solution can fail in runtime, so testing must prevent failing

Copy link

Since the issue is missing key information and has been inactive for 7 days, it has been automatically closed. If you wish to see the issue reopened, please provide the missing information.

@ZeeshanTamboli
Copy link
Member

@kyptov, we need reproductions because the issue might stem from another cause, and having them will help us find a solution. Are you suggesting that this is a TypeScript limitation? Unsure what actions can be taken on Material UI's side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customization: theme Centered around the theming features package: material-ui Specific to @mui/material status: waiting for author Issue with insufficient information
Projects
None yet
Development

No branches or pull requests

5 participants