How can I export custom MUI theme definitions after overriding them?

I have successfully created a MUI theme for my project, and everything is functioning as expected. Now, I want to extract this theme as a separate library (e.g. @myproject/theme) so that I can easily share and redeploy it across multiple applications. This way, when I update the theme library, all connected apps will automatically inherit the changes.

While this approach has worked well in most cases, I am facing difficulties in propagating the overridden variants with MUI. For instance, I have specific variants set up for the Button component:

declare module '@mui/material/Button' {
  interface ButtonPropsVariantOverrides {
    toolbar: true;
  }
}

export const myThemeOptions = {
  components: {
    MuiButton: {
      variants: [
        {
          props: { variant: 'toolbar' },
          style: { ... }
        }
      ]
    }
  }
}

export myLightTheme = createTheme(myThemeOptions, 'light'));

In the original project, the above code functioned perfectly, and VSCode recognized the added toolbar variant. However, after moving the theme to a new project/library (e.g. @myproject/theme) and installing it with npm install -D @myproject/theme, the added variants are no longer being recognized:

https://i.sstatic.net/lY3Wy.png

https://i.sstatic.net/xUlrn.png

How can I properly re-export the definition and ensure that consumer apps automatically load and override the MUI definitions? What is the correct approach to achieve my goal?

Feel free to suggest a different question title if needed.

Answer №1

It is advisable to consolidate all declarations in one place

declare module '@mui/material/Button' {
    interface ButtonPropsVariantOverrides {
        toolbar: true;
    }
}

within the file where you are creating the theme

export myLightTheme = createTheme(myThemeOptions, 'light'));

Answer №2

In case your theme lacks a .d.ts file containing all overrides and declarations, it is important to include it. Also, remember to add the file path to your tsconfig.json under the compilerOptions -> typeRoots list.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to eliminate the border color on a drop-down menu's bottom border?

I need help removing the border color from a dropdown with the style border-bottom: 1px solid rgba(0, 0, 0, 0.42); After debugging, I discovered that it is originating from the class MuiInput-underline-2593. However, the CSS class MuiInput-underline-2593 ...

Omit functions from category

This question reminds me of another question I came across, but it's not quite the same and I'm still struggling to figure it out. Basically, I need to duplicate a data structure but remove all the methods from it. interface XYZ { x: number; ...

Issue with React Material-UI ProgressBar not updating when value is changed

I have a functional component with an MUI progress bar that I want to display. However, when the progress bar loads, it remains at the initial progress level that I set in the first step. Additionally, I am making an API call and processing the results i ...

A guide to simulating ngControl in a Custom Form Control for effective unit testing in Angular

I need some guidance on creating unit tests for a Custom Form Control in Angular 9. The issue arises with this line of code: constructor(@Self() private ngControl: NgControl), which triggers an error: Error: NodeInjector: NOT_FOUND [NgControl]. It seems th ...

Why is TS1005 triggered for Redux Action Interface with an expectation of '=>'?

I'm finding it difficult to identify what's causing this issue, as shown in the esLint error from Typescript displayed in the screenshot below: https://i.stack.imgur.com/pPZa7.png Below is the complete code, which consists of actions for redux. ...

Guide on adding a Link component from react-router-dom to a Material-UI table

In my current project, I am facing an issue where I need to place a Link inside a table from Material UI. However, I am encountering two errors: "<td> cannot appear as a child of <a>" and "<a> cannot appear as a child of <tr>". Whil ...

Can we specify the type of a destructured prop when passing it as an argument?

I have implemented Material UI's FixedSizeList which requires rendering rows in the renderRow function and passing it as a child to the component. The renderRow function accepts (index, style, data, scrolling) as arguments from the FixedSizeList comp ...

Interconnected props in Material UI components interact with one another

Is there a way to have the value of one property in a Material UI component reference another property within the same component? For example, can I make the value property in a Switch component refer to the checked property? <Switch checked={sing ...

I am trying to include the Css Baseline from @mui/material in my project. However, even though it is present in my JSON file, I am encountering an error stating that '@mui/material' needs to be included in the project

Struggling to import Css Baseline from @mui/material, it's listed in my json but I keep getting an error saying '@mui/material' should be included in the project's dependencies. I've been stuck on this issue for a while now! { &q ...

Experiencing a problem with updating records in angular?

angular version: Angular CLI: 9.0.0-rc.7 I have encountered an issue while trying to update a record. After clicking on the edit icon, I am able to make changes to the record in the form. However, when I click on the Edit Button, the record gets updated i ...

Best practices for initializing model objects in a Redux or NgRx application architecture

Context: The development team is currently working on a sophisticated REST-based web application using Angular and the @ngrx library for managing state effectively. In order to represent entities retrieved from the server, such as accounts and users, we ...

How do I define two mutations in a single component using TypeScript and react-apollo?

After exploring this GitHub issue, I have successfully implemented one mutation with Typescript. However, I have been unable to figure out how to use 2 mutations within the same component. Currently, there is only a single mutate() function available in t ...

Building responsive grids in React using Material-UI components

https://i.sstatic.net/d2CIo.png On the left side, there is a map with controls, and on the right side, there are fields displaying the last update, a table, and an input field. I am looking to create a responsive design where when it reaches a certain si ...

Can you explain the key distinction between the backtick (`) and the ampersand-hash-39

I am completely new to TypeScript, JavaScript, and Angular. As I follow some tutorials, I often encounter code snippets like the one below: class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return `(${this.x}, ${th ...

Unexpected box-shadow issue with Material UI's Box component

During the development of my application, I encountered an issue with rendering a list of items. The state consists of a simple array containing elements with a name, an identifier, and a selected key that determines whether special styles should be applie ...

Numerous unspecified generic arguments

Imagine a collection of functions, each capable of taking an argument and returning a value (the specifics don't matter): function convertToNumber(input: string): number { return parseInt(input) } function convertToBoolean(input: number): boolean { ...

Unable to preserve the value of react material-ui autoCompletion using formik

Greetings everyone! Today, I wanted to address an issue that has been causing some trouble for me recently. I've been utilizing the autoCompletion feature from materiall-ui along with formik in reactjs. However, I've been facing difficulties when ...

Is it common for material-ui and react to exhibit different behavior following the `npm run build` command?

I’m currently in the process of developing a content library using React-app with create-react-app. Here’s a look at the configuration in my package.json: { "name": "demo", ... } After running yarn install && yarn start, everything appear ...

Guide on how to switch a class on the body using React's onClick event

There's a button in my code that triggers the display of a modal-like div element. When this button is clicked, I aim to apply a class to the body element; then when the close button is clicked, I'll remove this class. I'm looking for guid ...

Refreshing the entire page upon modifying a single state

I am currently in the process of constructing a page that includes numerous Charts, and I am utilizing a Material UI menu to switch between graphs. Each time I click on a new MenuItem, it alters my state and presents a new array of components. The pr ...