utilizing a dynamic value within CSS modules for class naming

Struggling to incorporate a variable into a CSS module class name in Next.js. Finding it challenging to determine the correct syntax.

The variable is fetched from the API:

const type = red

Here's how I'm attempting to achieve it:

<div className={` ${styles.background} ${styles.--type}`}></div>

The desired outcome:

<div className={` ${styles.background} ${styles.--red}`></div>

Any suggestions on how to accomplish this?

Answer №1

Uncertain of your desired outcome.

Consider this approach:

const category = 'red';

<div className={`${styles.background} ${styles.category}`}></div>

Alternatively, if adopting BEM with the -- convention is preferred, transitioning to bracket notation may be necessary.

const category = '--red';

<div className={`${styles.background} ${styles[category]}`}></div>

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 easiest way to compile a single .ts file in my src folder? I can achieve this by configuring the tsconfig.js file and running the yarn

{ "compilerOptions": { "target": "es5", "lib": [ "dom", "dom.iterable", "esnext" ], "allowJs": true, "skipLibCheck": true, ...

Having trouble changing a CSS property (margin-right)?

I have created a simple HTML list that consists of various items: <div id="menu"> <ul> <li>apples</li> <li>&#149</li> <li>bananas</li> <li>oranges</li> <li>grape ...

Discovering the meeting point where two dives converge

Is there a method to change the color of the overlapped common part of two animated circles? ...

design style of an activated material-ui button

I am currently working on implementing appbar buttons that appear differently when active. This is my first experience with material-ui, and I have found that the documentation can be somewhat unclear. My goal is to add an underline to the button that is ...

Is it possible to establish a CSS minimum width that is relative to a different element?

Is it possible to set the min-width of a submenu in CSS to be equal to that of the corresponding link in a navigation menu? I am using LESS for my styling. <ul> <li> <a href="">Item FooBarBaz</a> <ul class="submenu"&g ...

Expanding Nested List Components in Material-UI using ReactJS by utilizing the URL structure

I'm currently working with ReactJS and Material-UI. Is there a way to have the MUI nested list display specific items based on the URL? For example, if a user is on /inbox/starred/, I want the Inbox item to be open and Starred to be highlighted. I att ...

Adjust the width of the lower drawer component using React

After some extensive searching, I've hit a roadblock trying to tweak the width of an imported Material UI Drawer Swipeable edge. My aim was to make it Bottom-type without spanning the entire screen. I managed to adjust the top width using sx. However ...

Sending search queries from a frontend built with React.js to a backend in Express.js: What is the best approach?

I have been attempting to develop a basic search bar using react.js that will communicate with my express.js backend in order to retrieve the accurate data from the database and display it on the front-end. However, I am struggling to grasp how to transmit ...

Modify the parent div's style if there are more than two children present (CSS exclusive)

Is there a way to use the same class, like .outer, for both divs but with different styling for the parent element when there are more than two children? Kindly refer to the example provided below: .outer1{ border: solid 6px #f00; } .outer2{ b ...

Items positioned to the left will not overlap

Having trouble with floating elements? Need to ensure that box 3 aligns under box 1 when resizing the browser window? Use this HTML template: <div class="box"> <p>box 1</p> <p>box 1</p> <p>box 1</p> & ...

Exploring tailored markup features in Next.js version 13

I am trying to optimize my website for social media sharing on platforms like WhatsApp. I have been experimenting with different methods to set custom markup in Next.js 13, but haven't achieved the desired outcome yet. Your help and insight are greatl ...

What is the reason for NextJS causing a permanent redirect when using res.writeHead()?

Within my code, there is a specific section that checks for a certain condition to be true before redirecting. The relevant portion of the code is as follows: static async getInitialProps({ res }) { let accessToken = settings.get('access_toke ...

The bundle.js file encountered an issue while running UglifyJs, expecting a name

I have been attempting to utilize UglifyJS to compress/minimize my bundle.js file. However, when executing webpack -p, I encountered the following error message: ERROR in bundle.js from UglifyJs Name expected [bundle.js:105519,6] The line causing the iss ...

Incorporate the following font into an external HTML file and retrieve the path to the asset

The latest next/font API offers convenient methods for applying fonts using classes, styles, or even CSS Variables directly. But the question is, how can a reference to the font be embedded for external HTML inclusion? For example, if I have this code: // ...

How can I turn off the animation for a q-select (quasar select input)?

I'm just starting out with Quasar and I'm looking to keep the animation/class change of a q-select (Quasar input select) disabled. Essentially, I want the text to remain static like in this image: https://i.stack.imgur.com/d5O5s.png, instead of c ...

I'm having trouble with my Typescript file in Vscode - every time I try to edit the css code, all the text turns red. Can someone

Check out this visual representation: [1]: https://i.stack.imgur.com/9yXUJ.png Followed by the corresponding code snippet. export const GlobalStyle = createGlobalStyle` html { height: 100%; } body { background-image: url(${BGImage}); ba ...

Error: zone.filter is not a valid function and cannot be handled

Attempting to mark a location using Mapbox GL, I've installed react-map-gl and encountered the following error: Unhandled Rejection (TypeError): zone.filter is not a function. Can someone please assist me in resolving this issue? What am I doing incor ...

Unable to locate module: Unable to locate the file './Header.module.scss' in '/vercel/path0/components/Header'

I encountered an error while attempting to deploy my next application on Vercel. I have thoroughly reviewed my imports, but I am unable to pinpoint the issue. Module not found: Can't resolve './Header.module.scss' in '/vercel/path0/comp ...

What are the steps to transforming shorthand CSS into longhand?

Is there a tool available that can automatically convert shorthand CSS to longhand? I am in need of this functionality because I would like to utilize SmartSprites, which is not compatible with shorthand formatting. Additionally, if there is a tool that c ...

Tips for identifying selected rows in Material UI DataGrid

Currently, I am in the process of developing a website using React along with Material UI. My main focus right now is to identify which rows are currently selected in my DataGrid. To achieve this, I want to populate an array with the selected rows using t ...