question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation for extending theme is wrong. Module Augmentation does not work.

See original GitHub issue

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:

declare module '@mui/material/styles' {
  interface Theme {
    status: {
      danger: string;
    };
  }
  // allow configuration using `createTheme`
  interface ThemeOptions {
    status?: {
      danger?: string;
    };
  }
}
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

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:

declare module "@mui/material/styles/createTheme" {
    interface Theme {
        test: string,
    }
    // allow configuration using `createMuiTheme`
    interface ThemeOptions {
        test?: string;
    }
}

Your Environment 🌎

`npx @mui/envinfo`
   System:
    OS: Linux 5.10 Ubuntu 20.04.2 LTS (Focal Fossa)
  Binaries:
    Node: 14.17.5 - /usr/bin/node
    Yarn: 1.22.11 - /usr/local/bin/yarn
    npm: 7.24.1 - /usr/bin/npm

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:13
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
MauricePasternakcommented, Jul 2, 2022

I’ve included a small sandbox that showcases typical use cases:

  1. modifying theme to include extra custom properties

  2. modifying theme.palette to include extra color codes

https://codesandbox.io/s/romantic-gould-sbkc08

^ Codesandbox errors are the side product of it not playing nicely with declaration files; copy-paste both App.tsx and declaration.d.ts into your local project to see it does, in fact, work. I hope this ends up helping someone down the line.

@mnajdova I think what the MUI docs could still offer to explain is:

  • Why should the Theme interface also be extended and not just ThemeOptions. I assume that my sandbox’s commentary approximates the reason.

  • Possibly re-organize the example to showcase separate declarations in .d.ts files rather than featuring them together in one .tsx file with the rest of the code. In fairness, the onus is on the users to read up on declarations & module augmentation and adapt accordingly. It is the translation into this other scenario that might be confusing some users when they try to attempt it.

4reactions
arawmediacommented, May 7, 2022

Any reason this was closed? I’m finding the docs extremely unclear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation for extending theme is wrong. Module ... - GitHub
The issue is present in the latest release. I have searched the issues of this repository and believe that this is not a...
Read more >
Material UI theme module augmentation not working
I've been reading the MUI documentation, blogs, and other posts on Stackoverflow, yet despite all of that, I can't get my vscode intellisense/ ......
Read more >
Extending the theme in Material UI with TypeScript
The documentation tells us to use module augmentation to merge our theme with the built-in theme by creating a index.d.ts file and adding...
Read more >
Server Developer Guide - Keycloak
To simplify upgrading you should not edit the bundled themes directly. Instead create your own theme that extends one of the bundled themes....
Read more >
@kep-ui-kit/ui-theme - npm
Extending the theme ... As per mui documentation, use Module Augmentation to add custom variables. This should take place in theme/theme.d.ts, and ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found