Utilize worldwide classes within CSS module

Currently, I am immersed in a project that makes use of ReactJS and CSS modules, where each React component is paired with a SASS file.

During the CSS writing process, every class is suffixed with a 'triple underscore random string' like this: .myClass__e7G3A

In my application, a class ('top' or 'down') is added to the body tag based on the scroll position, and I want my component to react accordingly.

The issue I'm facing arises when if I include either .top or .down within the CSS module, the unique identifier is also appended to the end of that class.

For instance:

JSX:

<div className={styles.main}>
  Main content goes here.
</div>

Rendered HTML:

<body class="top">
  <div class="main___iZy2A">
    Main content goes here.
  </div>
</body>

SASS file:

.top .main {
  margin-top: 0;
}
.down .main {
  margin-top: 100px;
}

Compiled CSS:

.top___Gvf3S .main___iZy2A {
  margin-top: 0;
}
.down___kpd3S .main___iZy2A {
  margin-top: 100px;
}

Desired CSS outcome: (.top and .down without unique identifier)

.top .main___iZy2A {
  margin-top: 0;
}
.down .main___iZy2A {
  margin-top: 100px;
}

I believe there must be a simple solution or identifier that I am overlooking to achieve this. Your assistance would be greatly appreciated. Thank you.

Answer №1

Utilize the :global attribute on the selector:

:global(.top) .main___iZy2A {
  margin-top: 0;
}
:global(.down) .main___iZy2A {
  margin-top: 100px;
}

Alternatively, you can use a combination of :global and :local for multiple global selectors:

:global .top .top--left :local .main___iZy2A {
  margin-top: 0;
}
:global .down .down--right :local .main___iZy2A {
  margin-top: 100px;
}

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 combine and merge JSON objects that consist of multiple sub-objects?

I am working with a JSON response that contains multiple objects consisting of two main objects - datacenter and environment: "deployments": [ { "datacenter": { "title": "euw1", ...

Got a value of `false` for an attribute `closable` that is not meant to be a

Here's the code snippet I have: import { Modal } from "antd"; import styled from "styled-components"; export const StANTModal = styled(Modal)` & .ant-modal-content { border-radius: 20px; overflow: hidden; } `; And ...

Developing a MySQL DB-driven Node.js dashboard without the need for a poller

After exploring various StackOverflow posts on the topic, I haven't been able to find a solution that fits my specific situation. We have multiple monitoring instances across our network, each monitoring different environments (such as Nagios, Icinga ...

Access exclusive content by subscribing now!

How can I return a reference to a subject from a service without allowing the receiver to call .next() on the subject? Let's say there is a service with a subject that triggers new events. class ExampleService { private exampleSubject = new Subjec ...

Avoid running another process before the current one finishes in jQuery

I have a situation where I am using $.ajax inside a for loop in jQuery. for(var i=0; i < 2; i++) { $.ajax({ url :"/models/getdata"+i, dataType:"json", success:function(data) { } }); } The issue is that before the success function for i=0 completes, ...

Save to a JSON file

Hey there, I'm having some trouble with pushing a temporary value to a JSON file using the command "MyJSON.name.push". It keeps giving me an error saying "Undefined is not an object". I've tried different approaches and using JavaScript arrays wo ...

Customizing the Class of a jQuery UI ui-autocomplete Combobox Container

Is there a way to customize the ui-autocomplete div by adding a custom class? I have several autocomplete widgets on my webpage, and I need to style their drop-downs differently. Since editing the ui-autocomplete class directly is not an option, I am wor ...

Images appearing blank in Android webview due to CSS background loading issue

Hello everyone! I'm having an issue with loading a web url where everything is working fine except for loading background images. I came across this page: android css background-image not found But my question is slightly different. I don't wa ...

What is the best way to utilize node exec in synchronous mode?

I have a scenario where I need to run exec synchronously. Is there an alternative method to achieve this without using the deprecated execSync library? Can bluebird promises be used for this purpose? for (var i = 0; i < testCasesLength; i++) { va ...

The Mui table offers the optimal solution for incorporating images and organizing columns with code efficiently

Working with Mui Table in React, I am encountering two situations. One involves dynamic tables with images that are stored as URLs. The current approach for rendering these images includes checking if the URL ends with ".jpg" or ".png" and then displaying ...

Hiding a div with Javascript when the Excel dialog box is loaded

I have a piece of JavaScript code that is activated when the user clicks on an excel image. $("#excel").on("click", function () { $('#revealSpinningWheel').reveal(); $(window).load(function () { $('#revealSpinningWheel').hide ...

Adding scripts to a PartialView in MVC using Jquery's append function

My issue is with a JavaScript function that appends a PartialView into a div. Everything seems to work correctly, except for the fact that the PartialView contains some scripts enclosed within <script></script> tags. As a result, when these dyn ...

Is it necessary to specify a placeholder for the reusable input in React when using typescript?

I'm currently developing a React components library using Typescript and I have a query. Must every single input property be declared in the type/interface when creating a reusable component? For instance, I have an input field created with styled co ...

Integrate a post AJAX call into an Angular service for seamless functionality

I have come across an interesting scenario where I have to integrate old ajax code into a new Angular 10 application as per project requirements. Is it possible to directly run the existing ajax calls in the Angular service? Or, is there any node module ...

Anticipating the fulfillment of every promise

I am currently persisting entities to a Postgres database using Sequelize in combination with Node.js, Express, and React. Within my application, I have two main models: Countries and Activities. There is a button labeled "Save to database" that triggers ...

Tips for adjusting row height in a fluid table design using div elements

I'm having trouble making a responsive table (a keyboard) where only the cells are resizing instead of the whole keyboard fitting on the screen without any scrolling required. Here is the code I have so far: https://jsfiddle.net/723ar2f5/2/embedded/r ...

Tips for deleting the drop-down arrow from Mozilla Firefox

Is there a way to eliminate the dropdown arrow that FireFox usually displays? I have included an image below showing what I am referring to: This is the CSS code I'm using: @-moz-document url-prefix(){ .class select { width: 110%; } } .class > ...

What is the best way to maintain a fixed header for a content div?

I recently developed a website using angularjs. <html> <head></head> <body ng-app="app1" ng-controller = "ctrl1"> <header></header> <div ng-view></div> <footer></footer> </body> </ht ...

For optimal functionality, ensure that only one row is expanded at a time when using the React-

Working with React DataTable react-data-table-component, I have implemented an expandable table feature by setting expandableRows to true and providing expandableRowsComponent. The default behavior allows all rows to be expanded simultaneously, but I want ...

Unable to load page redirection

I am experiencing an issue with this page not redirecting to the appropriate mobile or desktop page when accessed. Below is the code snippet in question: <html> <head> <title>Loading...</title> </head> < ...