numerous requirements for formatting utilizing Css modules

Can someone help me figure out how to set multiple conditions using a ternary operator to style an element with Css modules? I'm struggling to find the right syntax. Is it even possible?

import style from './styles.module.sass'
const Slider =()=>{
const [hover,isHovered]=useState(false);
const [anyCardHovered,setAnyCardHovered]=useState(false)
return 

<div className={`{ hover? ${style.hoveredBox}: anyCardHovered? ${style.smallBox}: style.box}`>
  
</div>

Answer №1

<div className={hover 
  ? style.hoveredBox
  : anyCardHovered
     ? style.smallBox
     : style.box
}></div>

Here's an alternative approach:

/* Variables: hover, anyCardHovered */
const classNames = [
  style.box,        // 00
  style.smallBox,   // 01
  style.hoveredBox, // 10
  style.hoveredBox  // 11
];

<div className={classNames[hover << 1 | anyCardHovered]}>
</div>

For more information, visit:

Answer №2

One approach could be to define a variable named "classes" and determine its value and conditions before the JSX return statement. This way, you can easily assign classname={classes} to your boxes.

let classes = 'small'; 

if(hover) {
    classes = 'big'
} else if (anyCardHovered) {
    classes = 'medium'
} else {
    classes = 'small'
}

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

How can I eliminate the black outline added to text by IE CSS filter?

Is there a way to add text-shadows to elements in IE without having a black outline around the text when it is not black? I know IE doesn't support text-shadow property but using filter: property gets pretty close. If anyone has any suggestions or so ...

Astro Project experiencing issues with loading SRC folder and style tags

After setting up a brand new astro repository with the following commands: npm create astro@latest npm run dev I encountered an issue where the default project template failed to display correctly on my computer. Here is how the page appeared: https://i. ...

Experience the visually stunning React Charts line chart featuring grouped labels such as months and weeks

Can you create a line chart in recharts with grouped points, such as displaying 6 months data series per day for about 180 days? Each point on the chart represents a day, but I want the X-axis labels to show the corresponding month like Jan, Feb, Mar, Apr, ...

Avoiding the installation of dependencies within npm packages, particularly in projects utilizing Next.js, Node

I've encountered a puzzling issue that seems to have no solution online. When I install npm packages, their dependencies are not automatically installed. This situation is resulting in numerous warnings appearing in the console, such as: Module not ...

Looking for a prerequisite for running this script?

Seeking assistance from a skilled Javascript expert. I specialize in template development and utilize a specific script to display my footer link in all the free templates I create. If someone attempts to remove the footer link, their site will be redirec ...

The router.push function does not properly redirect to the specified path; instead, it just reloads the current page

I am a newcomer to NextJS and facing a challenge where I need to transfer data from the current page to another page. However, instead of loading the defined path in router.push as pathname: "/booking/checkout/", it loads the current page. I wan ...

My desire is for every circle to shift consecutively at various intervals using Javascript

I'm looking to draw circles in a sequential manner. I am trying to create an aimbooster game similar to . Instead of drawing all the circles at once, I want each circle to appear after a few seconds. The circles I've created currently do not g ...

What are the precise steps to create a flawless scrolling area?

I am facing an issue with setting accurate scroll zones for my divs. Here's my ideal scroll zone location: scroll zone in red Currently, I have a maximum of 4 divs and each div should contain a total of 10 rectangles per category (concentration, boos ...

Guide to extracting information from a JSON response with the fetch API

Issue trying to display JSON response after API call using fetch. Response visible in Chrome's response tab, but not found in fetch response object. Client side import React from 'react'; import './App.css'; class App extends ...

Issue with Internet Explorer 7 causing table to display with added height

Currently investigating why there is additional space being added by IE. The only fixed data is the width of the image (171 px). Using Jquery, I checked the height of the div in Firefox, Chrome, and Opera which came out to 164px, but in IE 7 it's 172 ...

Personalizing the React Bootstrap date picker

I am currently working on customizing the react-bootstrap-daterangepicker to achieve a specific look: My goal is to have distinct background colors for when dates are selected within a range and when the user is hovering over dates to make a selection. I ...

Different Styles of CSS Borders for List Items

Hey, I'm facing an issue with using borders in list items. I wanted to add borders to all the elements in the <li>, but when the width of the <li> exceeds the window's width, the right side border disappears. How can I resolve this ...

Prevent Overflow with Hidden Setting, Use Cover for Background Image, and Optimize for Anchors and

I'm currently working on a page that is designed not to scroll vertically. If you'd like to see the page with the issue I'm encountering for reference, you can visit Everything has been running smoothly except for two specific issues: W ...

What is the best way to retrieve cookies using getinitial props in Next.js?

Is there a way to retrieve cookies in getInitialProps and use them to fetch data from an API on a page in Next.js? ...

What is the approach for examining the context within a test file? Also, what is the procedure for retrieving values from the context within a

I recently started working with react context and testing. While using react context in my application, I now want to test the functionality but I'm unsure of how to access values from the context in the test file. Below, I have provided the component ...

Creating a subtle vanishing popup dialog using only CSS (no reliance on jQuery)

Is there a way to add a fade effect to my simple pop-up image using CSS? I've tried various transition properties, but nothing seems to work. Any suggestions would be appreciated. Thanks! ...

Revisiting data with React Apollo after receiving an asynchronous response from a Node.js GraphQL backend

The scenario is straightforward. I currently have a NodeJS GraphQL backend with user data. Whenever a new user is added, I am able to successfully fetch the updated user data using React Apollo. However, when I introduced an async operation to my backend ...

What is the best way to manage user sessions for the Logout button in Next.js, ensuring it is rendered correctly within the Navbar components?

I have successfully implemented these AuthButtons on both the server and client sides: Client 'use client'; import { Session, createClientComponentClient } from '@supabase/auth-helpers-nextjs'; import Link from 'next/link'; ...

What is the best way to arrange the JSON data in React Native using the map function

I have a JSON format like this: { "name":"xyz", "age-group":"bb" } How can I resolve the issue with this JSON? Here's the code I'm currently using: const array = [{ "name":"xyz", &q ...

Use Javascript or Jquery to dynamically change the background color of cells in HTML tables based on their numerical

I am working with a collection of HTML tables that contain numbers presented in a specific style: <table border="1"> <tr> <th>Day</th> <th>Time</th> <th>A</th> <th>B</th> &l ...