When importing both shared-lib and MUI in my ReactJS application, I noticed that my custom MUI styles were being overwritten by the MUI styles

I have developed a shared library using React and Storybook.

Within this library, I am utilizing the MUI V5 Button and making some style modifications using CSS modules.

I executed the "build&pack" script in my library, which generated a tgz package that I then imported into a new React application where it is included in my dependencies.

"dependencies": {
 "shared-react": "/Users/..../shared-react-1.0.0.tgz"
}

When I import it into my app like this:

import './App.css';
import {ButtonCSSModule} from 'shared-react'
import * as React from 'react';

function App() {
    return (
        <div className="App">
            <ButtonCSSModule variant="contained" disabled={false}></ButtonCSSModule>
        </div>);
}

export default App;

it functions correctly: image

However, when I also import the MUI button alongside my custom button

import {Button} from '@mui/material';
import './App.css';
import {ButtonCSSModule} from 'shared-react'
import * as React from 'react';

function App() {
    return (
        <div className="App">
            <ButtonCSSModule variant="contained" disabled={false}></ButtonCSSModule>
            <Button variant={"contained"}>New Button From Material</Button>
        </div>);
}

export default App;

the styling is overridden by MUI styles image

Is there a way to consistently overwrite the button style from @mui/material with my own styles (from my shared library)? perhaps exporting the CSS files and importing them into my new app so whenever someone uses a material button, it will apply the styles I have created?

Here is the link to the library I created

Github link to library: https://github.com/itzikd15/test

Answer №1

Experiment with altering the order of CSS injection Encapsulate your application using

<StyledEngineProvider injectFirst>
from mui, passing in the injectFirst Prop

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

Creating sibling classes with tailwind using the map function

Currently, I am combining tailwind, react, and nextjs to develop a website. My goal is to implement the check radio button technique to dynamically display different content on the website without relying on JavaScript. To streamline data management, I am ...

Can you identify the type of component being passed in a Higher Order Component?

I am currently in the process of converting a protectedRoute HOC from Javascript to TypeScript while using AWS-Amplify. This higher-order component will serve as a way to secure routes that require authentication. If the user is not logged in, they will b ...

Can CSS3 be utilized to craft this specific shape?

Can CSS3 be used to create the shape I need? I came across this website http://css-tricks.com/examples/ShapesOfCSS/, but I am unsure if any of the shapes on that page can be customized to match the specific shape I have in mind. Thank you! ...

How can I prevent the MUI date picker from automatically displaying today's date when the page loads?

When using the MUI Datepicker, I noticed that it displays today's date on page load even though no date is actually selected. I have tried setting the defaultValue prop to null and an empty string like this: defaultValue={null} defaultValue="" Howeve ...

Maximizing Efficiency: Utilizing a Single Panel across Multiple HTML Files with jQueryMobile

Can a panel defined in index.html be used on another page, such as results.html? Or do I have to define the panel on every page and duplicate the HTML code on each page? Because I want the panel to be consistent across all pages. This is the panel in my ...

Click on the link within the Checkbox label on MUI

I am working on creating a checkbox for the "Terms of Use," using FormControlLabel to nest a Checkbox. However, I also need to include a link that opens a Dialog component displaying the terms. The challenge is that clicking on the link checks the checkbox ...

Using jQuery to Generate an Automatically Updating Timer for Slideshow - Pausing on Mouse Click

My first slideshow with jQuery is up and running, but it's very basic and hard coded. I want to add an automatic scrolling effect every 8 seconds to the next item before looping back to the beginning. I have an idea on how to achieve this, but I want ...

It appears that Type 'MenuItemsProps' does not contain a property named 'map'. This might be causing the error message 'Property 'map' does not exist on

Recently, I delved into learning TypeScript and decided to convert my React code into TypeScript. However, I encountered an issue that left me stumped. I tried passing a state through props to a component with a defined value, hoping that the state would b ...

Different ways to manipulate the CSS display property with JavaScript

Having trouble changing the CSS display property to "none" using JavaScript. Check out my code: <div class="mydiv" onClick="clickDiv1()">hello</div> <div class="mydiv" onClick="clickDiv2()">hi</div> <div class="mydiv" onClick=" ...

Animate out Material UI element with zoom effect and then remove it from the

I'm currently working on a dynamic user interface that allows for adding and removing items dynamically. Each item has both an add and remove button, with a special animation effect using Zoom. While this works smoothly when adding new items, I encoun ...

Vercel Deployment is not displaying images properly

After uploading my react/next application to Vercel, I noticed that the images from the public folder are not rendering properly. Interestingly, when I run the application using 'Live Server' locally, the images appear just fine. <div classNam ...

Stop hyperlinks from automatically opening in a new tab or window

I'm having trouble with my website links opening in new tabs. Even after changing the attributes to _self, it still doesn't work. Can someone please review my code below and provide a solution? Feel free to ask for more clarification if needed. ...

I am currently struggling to make the userID route parameter function correctly with react-router-relay

I've been diving into the world of React Relay and GraphQL with react-relay-router, but I'm having trouble getting the params in my routes to function correctly. Specifically, I'm struggling with the "/Maps/:userID" route. Let me share my r ...

Struggling to synchronize animation timing between elements using jquery

When you navigate to an album (for example, Nature because I'm still working on the others) and select one of the images, they all gradually fade out while the selected image appears on the screen. What's actually happening is that the selected i ...

Encountering an issue while attempting to implement Redux Toolkit alongside the MUI ToggleButtonGroup component

The error message initially started as: Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. When I attempted to update the Redux state using a dispatcher, the handleChange function suddenly showed an e ...

Obtain Page Parameters within a Nested Next.js Layout using App Router

My Next.js App Router has a nested dynamic page structure using catchall routes configured like this: app/stay/ |__[...category] | |__page.js |__[uid] | |__page.js |__layout.js Within the 'layout.js' file, there is a con ...

The Redux state does not seem to update within the requestAnimationFrame function

After making updates to the Redux state, the component receives the updated state data, but not within the requestAnimationFrame function. For example, we can observe the updated state (backgroundMode) being logged near the top of the component declaratio ...

Collapsible Rows in Material-UI Table seamlessly integrated with regular rows

While using material-ui v4, I encountered an issue where my collapsible row columns are not aligning properly with my regular columns. Below is a photo and the code snippet for reference. As you can see in the image, when I expand the Frozen Yogurt row, th ...

Issue with implementing styles on Navbar React component

I'm currently learning Next.js, specifically working with version 10.0.3. In my project, I am using react-bootstrap version 1.4.0 and classnames version 2.2.6. Within my project, I have a component called Navbar that I am trying to style in a certain ...

Is there a way to refresh the animation on dougtesting.net?

I'm working with the dougtesting.net library to create a spinning wheel. I've been trying to figure out how to reset the animation once it finishes, but I can't seem to find any information on it. My goal is to completely clear all states so ...