Incorporating a CSS Module into a conditional statement

Consider the following HTML structure

<div className={
   `${style.cell} 
      ${cell === Player.Black ? "black" : cell === Player.White ? "white" : ""}`} key={colIndex}/>

Along with the associated CSS styles

.cell {
  width: 30px;
  height: 30px;
  border: 1px solid #000;
  background-color: burlywood;
  cursor: pointer;
}
  
.black {
  background-color: #000;
  color: white;
  text-align: center;
  font-weight: bolder;
}
  
.white {
  background-color: #fff;
  color: black;
  text-align: center;
  font-weight: bolder;
}

Is there a way to reference the .cell.black and .cell.white classes within the conditional statement using style... like you would if className was

{`cell ${cell === Player.Black ? "black" : cell === Player.White ? "white" : ""}`}

even when they are not stored inside a module (e.g.

import style from './Game.module.css'
)?

Answer №1

If you're looking for options, take a look at the following examples:

<div className={`${cell === Player.Black? style.cell + " black" : cell === Player.White ? style.cell + " white" : style.cell}`} key={colIndex}/>

Alternatively, you could create a function to generate a CSS string like this:

const getCellStyle = () => { return

cell ${cell === Player.Black ? " black" : cell === Player.White ? " white" : ""}
}

<div className={getCellStyle} key={colIndex}/>

Answer №2

This successfully fulfilled my requirements while also maintaining the characteristics of .cell

className={`${style.cell} ${cell === Player.Black ? style.black: cell === Player.White ? style.white : ""}`}

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 JavaScript to arrange the dots around the circle proves to be glitchy

I am having trouble positioning dots around a circle. The top and bottom points are not aligning properly, resulting in a buggy display. What could be causing this issue? How can I fix this problem? $(function(){ var globe = $('#center') ...

Cannot read property 'focusVisible' of null error is occurring on the radio

Having a complex setup involving Gatsby 1.9, react-next, React 16.5.2, and Material UI 3.2.2. Encountering issues with Radio and RadioGroup components consistently causing a strange error to pop up (refer to the image below). Unfortunately, unable to upg ...

Is it possible to increase the delay in the nth-child selector?

I'm attempting to create a compact checklist that displays in a single row with items appearing sequentially. To achieve this, I utilized the :nth-child() selector in CSS as shown below: .animated { -webkit-animation-duration: 0.8s; animation-d ...

I have no other option but to execute the command npm install --force

Whenever I run npm i in my React project, a dependency conflict arises. This project was not originally created by me, but rather by an external company. Now, it's my responsibility to make modifications as per the assigned tasks. The issue I encoun ...

What is the best way to cycle through a nested JS object?

I am currently utilizing useState and axios to make an API call, retrieve the response, and pass it to a Component for rendering. const [state,setState] = useState([]); const getCurrData = () => { axios.get('working api endpoint url').then(r ...

Create a series of actions that do not depend on using only one occurrence of the WriteBatch class

My goal is to create a series of batch actions using functions that do not require a specific instance of WriteBatch. Currently, I am passing an instance of the WriteBatch class to the functions so they can utilize the .set(), .update(), or .delete() metho ...

Enhancing the Rendering of React's props.children

I have set up my code like this: // Parent.js class Parent extends React.Component { render() { return { this.props.children } } } // Child.js class Child extends React.Component { render() { let message; const greet = ...

fixed width div with width proportion

experts. I'm currently experimenting with a spinning cube effect for my gallery and I recently came across Paul Hayes' experimental cube on his website (it's best viewed in Google Chrome). However, I am facing a challenge in getting it to w ...

What is the reason behind FieldSelect returning a string instead of an object like FieldCheckbox?

FieldSelect component from Sharetribe documents is giving me a string, while FieldCheckbox is returning a JSON object. In a specific scenario, I want FieldSelect to store a JSON object. How can I achieve this? Below is the code snippet for reference: I& ...

ESLint was unable to definitively identify the specific "import" plugin in the React environment

Every time I run the Lint test in my NextJS project, I encounter this error: ESLint: 7.32.0 ESLint is unable to determine the plugin "import" uniquely. - /Library/...[FOLDER_PATH]..../node_modules/eslint-plugin-import/lib/index.js (loaded in ".eslintrc.j ...

What is the best way to set up the correct pathway for displaying the image?

I'm currently facing a challenge with displaying an image from my repository. The component's framework is stored in one library, while I'm attempting to render it in a separate repository. However, I am struggling to determine the correct p ...

Navigating with buttons in React JS

In my material-ui, I have a button set up like this: <Button style={green} raised="true" label="Continue to create group"}> CREATE NEW GROUP </Button> I am looking to make it so that when the button is clicked, it will take me ...

Align the responsive image in the center of a container

I am facing a challenge in centering an image wrapped by an anchor tag inside a div. I have tried using display flexbox and justify-content:center for the image, which works to some extent. However, the height of the image remains constant while the width ...

A guide on grouping an entire CSS file under one class umbrella

Is there a way to encapsulate a large number of CSS declarations so they only apply when the parent has a specified class? div { color: #ffffff; } ... a { color: #000000; } Can it be structured like this, with a parent class containing all the CSS r ...

Arranging Select Dropdown Options in a Specific Order using Angular 7 and Typescript

My select dropdown is populated dynamically with options fetched from a service using *ngFor. I am looking to customize the order of these options. Can this be achieved through Angular code? The array structure is as follows: console.log(this.paymentTyp ...

Using useState to initialize a long value

Obtaining a very large state from JSON can be challenging, especially when it consists of at least 50 lines. During page generation, an error like "there is no such value" may occur if the initial value is not set and the interface is not assigned properl ...

Enable the ability to scroll and click to navigate an overlapping Div element

A customer has a website with a dark teal design and is not pleased with the appearance of the scroll bar, as it disrupts the overall style. The client requested that I find a solution without using third-party libraries, and one that they can easily under ...

Using nodemailer to send an email with a dynamic variable that holds the HTML content

I am attempting to send a variable containing HTML code from a Vue component using the POST method. My technology stack includes TypeScript, Nuxt.js, Node.js, and Vue.js. const order_list = document.querySelector('table') as HTMLInputElement | n ...

Delete the option to alter button in the Jasny file input tool

I recently incorporated the Jasny Fileinput Widget into my existing form. However, I noticed that when I select a new file, it displays two buttons: Change and Remove. In this case, I believe the Change button is unnecessary and would like to remove it com ...

Incorporate a vertical scrollbar in the tbody while keeping the thead fixed for smooth vertical scrolling

I'm seeking a solution to implement horizontal and vertical scroll for my table. Currently, the horizontal scroll is working fine, but when trying to add vertical scroll, the table header also moves along with it. Is there a way to keep the table hea ...