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

Running npm start in a React development environment on Linux without automatically opening a browser can be achieved by configuring

As I dive into learning React, I've noticed that I keep running npm start in the terminal multiple times. However, it can be quite frustrating how a new browser window opens each time. I'm currently attempting to prevent this from occurring on my ...

How to open a new tab in ReactJS

When attempting to open a component in a new tab using the Link in React router, it results in a 404 error page being displayed instead of the desired react component. The main React entry point file (js), import React from 'react'; import { re ...

Ways in which breakpoint.value can be utilized?

I've created a platform to manage employee activities, and I want it to be fully responsive across all devices. For instance, if you wish to tailor the site for screens labeled as "sm" and "xs," you can use the following code: [theme.breakpoints.down ...

Ways to eliminate bottom margin of text area in vuetify

Greetings, I am facing an issue with the following code snippet: <v-text-field :rules="rules" v-model="exam.title" class="exam-title ma-0 pa-0" ></v-text-field> <v-text-field class="exam-title ma-0 pa-0"></v-text-field& ...

The CSS property object-fit: cover is unable to properly display JPEG images with EXIF orientation greater than 1

I'm having trouble with my Angular app and creating a gallery of photos from my Samsung Galaxy. I am using the "object-fit: cover" css attribute for a nice design, but it only seems to work correctly when the image has an EXIF "orientation" property e ...

Navigating with React and MUI on Mobile Devices

I discovered a helpful tutorial on setting up navigation using Drawer on YouTube, and I have created a code sandbox with the implementation here: https://codesandbox.io/s/musing-hofstadter-rj4vn?file=/src/App.js My goal is to maintain a two-pane design fo ...

The issue with two different link styles displaying correctly in Internet Explorer but not in other browsers is that when a link is clicked, all links appear

Looking for a testing ground? Check out my website: While everything works smoothly on IE, I've noticed a glitch in other browsers. Clicking on one link causes all links on the page to appear as visited links. Take a look at the CSS code below: @ch ...

Is there a way to determine the orientation of an image in React-Native, whether it is horizontal or vertical

When working with react-native, I aim to utilize the 'contain' feature for vertical images and the 'stretch' feature for horizontal images. What would be the best way to determine the orientation of an image as either horizontal or vert ...

Implementing a universal (click) attribute for Angular 2 in CSS

When using Angular 1, it was as easy as following this syntax: [ngClick], [data-ng-click], [x-ng-click] { cursor: pointer; } This snippet ensured that any tags with the ng-click attribute displayed a pointer cursor. How can we achieve the same effect ...

Can you incorporate personalized icons in ReactJS?

Is there a way to incorporate my personally designed custom icons, available in both SVG and TTF formats, into a React project? I am interested in using these icons in my navigation bar, such as creating a unique home icon for the home button. ...

What is the best way to incorporate this type of input layout with MUI v5?

https://i.stack.imgur.com/eLLHq.png Struggling to figure out how to achieve a design like this. While the outlined input in Material UI places the label on the top border, I'm looking to have it inside the border instead. ...

Tips for notifying a user about leaving a page without saving their information

I created a small table where users can input product names and numbers. My question is, how can I notify the user if they try to leave the page without saving the entered information? <form action="index.php" method="post"> <input type="text" ...

Adjustable Text Size with HTML5 and CSS3

Looking to enhance the accessibility of a website by implementing text size adjustment buttons ' A A A ' using just HTML5 and CSS3. Avoiding the use of jQuery or Javascript for this task. Inspiration drawn from this link. Appreciate any assistan ...

Add color to the cells within the table

I need help with dynamically adding colors to my table based on the results. If the result is 'UnSuccessfull', the color should be 'red'. If the result is 'Successful', the color should be 'green'. The challenge I a ...

splitting up the lengthy list into manageable chunks according to the screen dimensions

How can I make the blue color row-based list (the divs inside a div with id phrase) break on the next line based on the screen size? Any suggestions on additional changes needed in the following CSS? #phrase { color: #fff; position: ...

Issue with Bootstrap 3 Modal: Missing Title and Input Labels

I'm currently working on customizing a bootstrap template for my friend's church website. My goal is to create a simple web presence for them, and I am in the process of setting up a contact form. I want this form to appear as a modal where users ...

Generating random numbers using click events in a React application.Let's explore how to create

My goal is to generate random numbers between 1 and 50 when a button is clicked. Below is the component I have created for this functionality: import React from 'react' import './Board.css'; const Board = () => { let rand; f ...

What causes variations in the output of getClientRects() for identical code snippets?

Here is the code snippet provided. If you click on "Run code snippet" button, you will see the output: 1 - p.getClientRects().length 2 - span.getClientRects().length However, if you expand the snippet first and then run it, you will notice a slight dif ...

How come the inclusion of a DOCTYPE tag impacts the styling of my CSS?

I am puzzled why the code below renders correctly without any issues until I add a doctype declaration: <body style="margin:0; padding:0; background-color:#eeeeee"></body> <div id="HeaderContainer" style="background-color:#eeeeee; color:Bl ...

React warning: Make sure every child element in a list has a distinct "key" property assigned

Here is a breakdown of my code that consists of 2 components and index.tsx: NewsFeed.tsx import React, { useState } from 'react' import Feeds from './Feeds' export default function NewsFeed({ news }: any) { const [date, setDate] ...