Dealing with CSS specificity issues when incorporating material-ui for React into a WordPress plugin

Struggling to integrate material-ui for react in a wordpress plugin due to conflict with wordpress's styling in forms.css file. Looking for a solution that doesn't require complete restyling of material-ui components to override the default styles.

For example:

Here is an example(text has been edited for readability(removed selectors that dont matter and styling):

react jsx:

<div id='ad-campaign-edit' style={modalStyle} className={classes.root}>
            <form
                className={classes.form}
                noValidate
                autoComplete='off'
            >
                <TextField
                    id='ad-campaign-name'
                    className={classes.textField}
                    variant='filled'
                    label='Navn på kampanjen'
                />
                <TextField
                    id='ad-campaign-sub-title-text'
                    className={classes.textField}
                    variant='filled'
                    label='Undertekst'
                />
                <TextField
                    id='ad-campaign-sub-price'
                    className={classes.textField}
                    variant='filled'
                    label='Pris'
                />
                <Checkbox
                    defaultChecked
                    className={classes.textField}
                    color='primary'
                    inputProps={{ 'aria-label': 'secondary checkbox' }}
                />
            </form>
        </div>

forms.css(WP):

input[type="text"], {
   ...
}

generated css from material-ui:

.MuiInputBase-input {
   ...
}

Noting that material-ui generates its css after wordpress's, where forms.css specificity is (0-1-1) and material-ui is (0-1-0).

How can I ensure material-ui styles take precedence without starting from scratch with makeStyles hook? Is it possible to enhance material-ui's specificity globally?

edit: Forgot to add html/jsx edit2: revised specificity understanding.

Answer №1

I managed to get it working, although there are still some kinks to iron out. This solution serves as a good starting point.

To make this work, you'll need to install the npm package found at this link: https://www.npmjs.com/package/jss-increase-specificity

After installation, you must create a <StyleProvider> following the instructions provided here: https://material-ui.com/styles/advanced/#jss-plugins

This is what I implemented:

import { create } from 'jss';
import { StylesProvider, jssPreset } from '@material-ui/core/styles';
import increaseSpecificity from 'jss-increase-specificity';


const jss = create({
    plugins: [...jssPreset().plugins, increaseSpecificity({ repeat: 1 })],
});

export default function App() {
  return (
    <StylesProvider jss={jss}>
      ...
    </StylesProvider>
  );
}

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

Utilizing a CSS/HTML div grid to mirror a 2D array in JavaScript

Currently, I am working on a personal project that involves creating a grid of divs in HTML corresponding to a 2D array in JavaScript. However, I am uncertain about the most effective way to accomplish this task. Specifically, what I aim to achieve is tha ...

Using jQuery to decrease the transparency of an image when the mouse hovers over it

I'm trying to set it up so that when you hover over an image, the opacity decreases by half. I know I must be making a simple mistake here, but I just can't figure out what it is. Any suggestions would be greatly appreciated. http://jsfiddle.net ...

What is the rationale behind excluding the constructor in the Time Travel section of the ReactJS tutorial?

As stated in this particular tutorial: The next step is to pass the squares and onClick props from the Game component to the Board component. With a single click handler in Board for multiple Squares, we need to include the location of each Square in the ...

Uncovering a Memory Leak in a ReactJS App

As a newcomer to this framework, I know this question may be repetitive, but I am eager to troubleshoot and resolve the issue in my project. Every page of my project is showing a memory leak warning, and I have been learning about CRUD operations through Y ...

Blockage preventing text entry in a textarea

To enhance the basic formatting capabilities of a textarea, I implemented a solution where a div overlay with the same monospace font and position is used. This approach allows for displaying the text in the div with different colors and boldness. However ...

A versatile Material UI paper that adjusts its dimensions dynamically

Utilizing Material-UI's Paper component (https://mui.com/components/paper/), I've encountered a scenario where the content within the Paper element needs to be dynamic. <Paper className="modal" elevation={3}> ...Content </Paper ...

Unable to adjust metadata titles while utilizing the 'use client' function in Next.js

I have a /demo route in my Next.js 13 application, and it is using the App Router. However, I am facing an issue with changing the title of the page (currently displaying as localhost:3000/demo). The code snippet for this issue is shown below: /demo/page ...

Troubleshooting Socket-io Client Problem After Migrating to TypeScript

I am currently in the process of migrating my MERN stack + Socket.io template to Typescript. However, I am encountering some issues specifically when transitioning the client code to Typescript. The Problem: My socket pings from socket.io-client are faili ...

I found myself continuously revolving four images along a circular border, each time revealing the corresponding content. However, I only required the content from one of the images at a time

I attempted to animate the rotation of a circle and display the content of a specific image by pausing it for a period of time. However, I am unsure how to halt the animation and display the content of the specified image. Here's what I tried: HTML C ...

Every time I switch tabs in Material UI, React rebuilds my component

I integrated a Material UI Tabs component into my application, following a similar approach to the one showcased in their Simple Tabs demo. However, I have noticed that the components within each tab — specifically those defined in the render method ...

Error: the keyword "module" has not been properly defined

Struggling to run this web app, initially encountered: (node:12960) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. C:\Users\J\react-messenger\stream-chat-boilerp ...

Performing multiple requests using Axios library in a React application

My goal is to make 2 requests and define variables using this.setState({}) for future modifications. This is the code snippet I have: class App extends React.Component { constructor() { super(); this.state = {user: false, repository :false} ...

Elements on the page are quivering as a result of the CSS animation

When a user clicks anywhere on a div, I have added a ripple effect to give it some interaction. However, there is an issue where the element shakes and becomes blurry when the page is in full screen mode until the ripple effect disappears. Below is the Ja ...

Incorporating Cascading Style Sheets (CSS) to

I'm looking to style my form with CSS but I'm unsure of the process. Currently, the form is generated using PHP and MySQL, and in the browser it appears like this: http://gyazo.com/5d099ead9bd6ea83859a5114b2438748 I want to align the text and dr ...

Error: Unable to locate the custom module - TS2307

We have recently taken over a Next + TypeScript website from another developer and are attempting to create a new component that is heavily based on an existing one. I have duplicated the entire component and its subfolders at the same level as the origina ...

I'm having trouble with installing Firebase using the npm i firebase command

Currently working on developing an app using React. Planning to implement Firebase for the backend. Facing issues while trying to install firebase, encountering the following errors: npm ERR! code FETCH_ERROR npm ERR! errno FETCH_ERROR npm ERR! invalid js ...

Error occurred when attempting to fetch data from a nested JSON object in React/Next.JS

Having trouble retrieving data from my API and encountering this error message. I suspect it may be related to the JSON structure. Interestingly, when using a different URL with nested arrays, it works fine. However, my current data is not cooperating. Any ...

The system encountered an error while attempting to access the property "getChild" of an unspecified object

I am currently developing a react application and encountering an issue when trying to call a function from the render method. The function I'm trying to call utilizes the getChild method, but I keep receiving an error stating "cannot read property &a ...

What could be causing my table to malfunction in IE8?

Is it just me, or do my CSS tables not work on IE8? <div class="main-table"> <div class="row"> <a class="secondary-table" href="#"> <div class="secondary-row"> <div class="secondary-cell"> ...

Create a dynamic feature in Bootstrap4 where the navigation bar changes color as the user scrolls to different sections of the

Currently building my personal portfolio website with Bootstrap 4, I came up with a great idea: changing the navigation bar color based on different sections of the website. I attempted to achieve this using JQuery's offset and scrollTop functions, bu ...