Revised: "Redesigned CSS class names taken from a

I created a React component library using mantine with @rollup package. This library comprises custom components styled with styled-components and CSS module (.module.css) files.

All the css modules are bundled in javascript and injected into the body html element: Inject css

The library is packaged in esm, cjs, and umd. The css is generated using the rollup plugin: rollup-plugin-styles:

styles({
    modules: true,
    autoModules: true,
    mode: [
      "inject",
      { container: "body", singleTag: true, prepend: true, attributes: { id: "global" } },
    ],
  })

An issue I am facing is that the class names utilized in my components get altered by next.js during server-side preprocessing before being sent to the client. For instance: In my esm module file:

var css = ".Layout_module_container__61ae088e {\n    display: flex;\n    flex-direction: column;\n    height: 100vh;\n}\n.Layout_module_main__61ae088e {\n    display: flex;\n    flex-direction: column;\n    flex-grow: 1;\n    overflow-y: auto;\n    padding: 0 1.5rem;\n}";
var modules_e616121b = {"container":"Layout_module_container__61ae088e","main":"Layout_module_main__61ae088e"};
n(css,{"container":"body","singleTag":true,"prepend":true,"attributes":{"id":"global"}});

In the js loaded stylesheet (in body html):

<body>
<style type="text/css" id="global">
@media (min-width: 36rem) {.Footer_module_container__fe0fb20d {
        padding: var(--mantine-spacing-md)
}
    }
    @media (max-width: 36rem) {.Footer_module_container__fe0fb20d {
        padding: var(--mantine-spacing-xs)
}
    }

.Footer_module_infosContainer__fe0fb20d {
    flex-direction: column;
    flex-grow: 1;
    align-items: start;
}
@media (min-width: 768px){
...
</style>
</body>

The correct class name should be displayed here: div

<div style="gap:var(--mantine-spacing-md);align-items:center;justify-content:space-around" class="m-8bffd616 mantine-Flex-root __m__-R1d5bsm"/>

The classname gets changed in the html to something like: __m__* but remains the same in the stylesheet.

A similar issue occurs with styled-components as well. styled-component error

next-dev.js:20 Warning: Prop `className` did not match. Server: "m-8a5d1357 mantine-Title-root" Client: "WebsiteSelector_module_websiteName__547dd6e4 m-8a5d1357 mantine-Title-root"

I have tried multiple solutions found online, but none seem to work for me. These include:

  • Removing css injection in js and creating a separate css file imported in App.tsx on next.js
  • Adding babel-plugin-styled-components plugins in babel.rc file. However, this led to disabling swc to enable babel (resulting in performance loss)

The library works perfectly fine with vite.js, indicating that the issue lies solely on the next.js side.

Versions of my app:

dependencies: {
  "next": "12.3.4",
  "react": "18.2.0",
  "react-dom": "18.2.0",
}

Version of my library:

"dependencies": {
  "react": "18.2.0",
  "react-dom": "18.2.0",
},
"devDependencies":{
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "styled-components": "^6.1.8"
}

Any help would be greatly appreciated 🙏🏻.

Answer №1

After much searching, I finally discovered the solution.

The issue originated from the next.js SSR functionality. I found that I needed to insert "use-client;" at the beginning of each of my library components. To accomplish this, I utilized the rollup-plugin-banner2 package:

banner((chunk) => {
    if(!excludedUseClientFiles.includes(chunk.fileName)){
      return "'use client';\n";
    }
    return undefined
  })

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

Master the art of using useMutation from react-query: Learn how to handle error messages and success notifications effectively!

Currently in my Next.js project, I have integrated react-query to retrieve data from MongoDB. Within my form, a POST request is sent using the useMutation hook: /*** components ***/ import {Button, InputGroup} from "../index"; import {useMutatio ...

Facing an issue where the data returned in React is showing up as undefined

I am currently facing an issue with passing data down to a component using react router. After confirming that the endpoint is functioning correctly, I have ruled out any server-related problems. Here is the function responsible for fetching page data bas ...

What can I do to adjust the position of the div wrapper if the floating sidebar is taller than

Check out this example http://jsfiddle.net/NuzDj/ When you resize the window, the sidebar overlaps with the wrapper and footer. Is there a way to automatically adjust the height of the wrapper when the sidebar overlaps with it? ...

Trouble with CSS loading due to div in Visual Studio MVC

Currently, I am working with Visual Studio 2013 MVC and have a situation regarding the styling of my navbar in _Layout. The navbar includes a messages dropdown menu that utilizes CSS and JS extensively. Interestingly, when I load the messages as a partial ...

Should pages be indexed to index.php or should the top and bottom parts of the page be created and included in all content pages?

I've been out of the webpage creation game for a while, but I've recently decided to get back into it. Like everyone else, I want to learn the best way to do this. However, I find myself facing a dilemma on how to structure my pages. My initial i ...

What sets usePreloadedQuery apart from useQueryLoader?

I've been exploring graphQL and the react-relay library. These are the key points I've covered: Rendering Queries: where usePreloadedQuery is introduced. Fetching Queries for Render: where useQueryLoader is introduced. To keep it simple, I&apo ...

React.js Form Validation issue: When using input type "number", the text field remains a single value even after attempting to delete characters with the backspace key

Experiencing difficulty in clearing the input field value, even after pressing backspace the value remains constant. One particular value persists in the input field. import * as React from "react"; import { Button, Form } from "react-boots ...

When attempting to upload an image file to my backend server, I am facing the issue of receiving an empty object upon its arrival

I'm currently working on a form that captures an image, sends it to the backend, and then saves it to my local disk. However, I am encountering an issue where when I send the image file to the backend, it shows up as an empty object. Frontend import ...

The background image constantly shifts while scrolling on a mobile device

Just sharing my first post here. There's been something bothering me on my personal website for quite some time. Whenever I visit the site on my Android phone and start scrolling through the page, the background image seems to 'adjust' itse ...

Unable to retrieve component name using React.Children

While working with react in the nextjs framework, I attempted to create my own dropdown component structured as follows: <Dropdown> <DropdownToggle>Action</DropdownToggle> <DropdownMenu> <DropdownItem>Menu 1</Dr ...

The h3 element is not responding to the adjacent sibling selector

Below is my HTML DOM structure. I am trying to remove the margin and padding for all h3 elements that are immediate siblings of a div with the id "successor". I attempted to achieve this using the adjacent sibling selector "+", but unfortunately, I'm ...

What is the best way to hand off a component to a helper function?

I am trying to create a helper function in TypeScript that takes a component as an argument and returns an array of objects, each containing a component node... // helpers.ts import { LINKS } from '../constants'; // error on the next line: Cannot ...

Steps for Deactivating HTML Links

I am facing an issue with a link button inside a <td> that needs to be disabled. It is working fine on Internet Explorer but not functioning properly in Firefox and Chrome. I have tried various methods, but nothing seems to work on Firefox (using ve ...

Oxygen-style with a sleek, shadow-free frame

Currently, I'm utilizing the qtoxygen style and I am curious if anyone knows how to remove the shadow frame on QPlainTextEdit. Even after attempting to customize the CSS with { border: none }, the shadow continues to be displayed. ...

Creating a paginated table with Nextjs, Prisma, and SWR: A step-by-step guide

I am attempting to set up a paginated table utilizing Nextjs, Prisma, and SWR. The table will display a list of invoices sorted by their ID. Here is an example of what it would look like: https://i.sstatic.net/WymoH.png To fetch all the data to the api r ...

Issues with the functionality of the Customer ListItem on a Selectable List have been identified within the material-ui framework

When using the Selectable List, I encountered an issue where if I wrote a custom list item, the list wasn't selectable. However, when I used the list item directly, it was selectable. var DataCenterRow = React.createClass({ render: function () { ...

Utilize Flexbox's column direction to ensure that all flex items within the same column have uniform height

Having trouble creating a responsive web page. Here are the specifics: Sharing my HTML and CSS code below: - <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA ...

Displaying a submenu upon hovering within its designated CSS region

I'm encountering an issue with my submenu. It's supposed to appear when hovering over the parent menu li, but it also shows up when the mouse hovers over its area. Let's take a look at some images. First screenshot below shows that it works ...

Utilizing SASS for customizable color schemes

I am in the process of creating a website using Rails 3 which will allow users to customize their profiles with different layouts and color schemes. I have already implemented SASS, and I believe it would be incredibly useful if I could achieve something l ...

What is the best way to combine two menu plugins in Wordpress?

Currently, I am in the process of creating a logo with a drop down menu for my website, which can be found at . After discovering a widget called Menu Image that successfully transformed a menu item into a photo, I decided to add another widget called Ma ...