Is conditional CSS possible with NextJS?

While working on an animated dropdown for a navbar, I came across this interesting dilemma.

In a strict React setup, you can use an inline if/else statement with onClick toggle to manage CSS animation styles. To ensure default styling (no animation) when the state is null, you can add a class before the inline operation:

<div className={`navbar ${reactState ? "expanded" : "collapsed"}`}>

How can I achieve the same in NextJS?

I searched through the documentation but couldn't find anything relevant. I tried the following approaches without success:

<div className={styles.navbar (reactState ? `${styles.expanded}` : `${styles.collapsed}`)}>
<div className={styles.navbar [reactState ? `${styles.expanded}` : `${styles.collapsed}`]}>
<div className={styles.navbar `${reactState ? `${styles.expanded}` : `${styles.collapsed}`}`}>

The only thing that worked was the following complex solution, which seems like too much:

<div className={
  reactState != null ?
    (reactState ? `${styles.navbar} ${styles.expanded}` 
    : `${styles.navbar} ${styles.collapsed}`)
  : styles.navbar
}>

I'm clearly struggling to grasp how NextJS handles React styling. Any assistance would be greatly appreciated.

Answer №1

Everything should be functioning properly, much like React. The only issue appears to be with incorrectly escaped values.

<div className={`${customStyles.navbar} ${isStateActive ? customStyles.active : customStyles.inactive}`}>...</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

Guide to displaying a canvas as an image in a new tab using Reactjs

My current project involves using a canvas barcode generated by the react-barcode library. At the moment, I can only right-click on the canvas to open it as an image in a new tab. Is there a way to achieve this functionality with just a click of a button i ...

As the browser window is resized, the gap between images widens while the images themselves decrease

Hey there, I'm encountering some trouble with the image links at the top of my page. As I resize the browser or change screen resolutions in media queries using percentages, the images are resizing accordingly. However, I'm noticing that as the i ...

Can you show me the steps for linking the next method of an EventEmitter?

After emitting an event, I am looking to run some additional code. Is there a method to chain the .next() function in this way? @Output() myEvent = new EventEmitter<string>(); this.myEvent.next({‘test string’}).onComplete(console.log('done& ...

The image is failing to animate according to the PNG sequence function

Enhanced Functionality: Upon clicking the "Tap Here" image button, a function called "GameStart()" is triggered. This function ensures that the main image "Star" descends down the page from the top through an animated sequence using png logic. The propose ...

Guide on efficiently navigating a website: utilize selenium python to effortlessly hover the mouse over a sub menu and click on desired options

Seeking to automate a web application called "Maximo" using Selenium in Python, I've successfully managed to log in with credentials and click on buttons. However, I'm stumped on how to hover the mouse over a submenu and perform a click action. ...

Turn off the highlighting for an external event in FullCalendar

Hey there, I'm currently working with the fullcalendar jquery plugin v2.6.1 and I have a question about preventing external events from being highlighted while they are being dragged onto the calendar. Is there a way to stop the fc-highlight from app ...

How can I ensure the end of the list is clearly visible?

I'm experiencing an issue where the last element in my list is getting cut off. When I check the console logs, I can see that it's rendering properly. If I remove the 'position: fixed' style, I can see the element at the bottom of the l ...

Having trouble getting the `nth-of-type` and `nth-child` selectors in CSS to function

I am attempting to apply a green color for the first nth child flag element <div id="axis"> <div class="super"></div> <div class="flag">flag 1</div><!-- I want this text to be green--> <div class="supe ...

When I tap anywhere outside the Material UI Autocomplete input field, the text inside the field disappears

I'm currently utilizing the Material UI Autocomplete component. However, I've noticed that if you enter text into the input field and then click outside of it, the text gets cleared. How can I resolve this issue? import React from 'react&apo ...

Steps for creating an AJAX request to a variable defined within the local scope

I want to create a list using a JSON object that I already have stored in a variable. I have been exploring the dynatable library and its documentation on populating a table using AJAX to receive JSON data. However, I am stuck on how to make it work with ...

Is there a potential security flaw in jQuery when using $.get(code_url) to execute the returned code without relying on eval() or appending it to the DOM

As I was debugging my NodeJS application, I stumbled upon a surprising discovery: when using jQuery with a simple $.get(code_url) where code_url is a URL leading to server-side JavaScript code, the code gets executed. The expected behavior should be: $.g ...

"Looking to log in with NextAuth's Google Login but can't find the Client Secret

I am attempting to create a login feature using Next Auth. All the necessary access data has been provided in a .env.local file. Below are the details: GOOGLE_CLIENT_ID=[this information should remain private].apps.googleusercontent.com GOOGLE_CLIENT_SECR ...

Tips for effectively managing the timeout of server-side AJAX calls1. Maxim

I'm currently developing server code in ASP.NET with the MVC framework. The client-side code is written in javascript. When sending an AJAX request from the browser to the server, whether using JQuery or not, timeouts can be set. Additionally, the br ...

The transition in Material-UI Swipeable Drawer is a bit choppy and not as

import { ThemeProvider } from '@material-ui/core/styles' import React, { useState } from 'react' import { Link } from 'react-router-dom' import AppBar from '@material-ui/core/AppBar' import Toolbar from '@mate ...

Tips for accessing and displaying JSON data using jQuery

Data in JSON format: [ { "ID":"25", "Serial":"1", "Purchase_id":"8", "Item":"23", "Unit":"1", "HSN":"84212120", "Quantity":"10", "Purchase_rate":"100", ...

Partially translucent electron window

Is there a way in Electron to create a view similar to the finder in macOS, where the sidebar is opaque and the main content is not? https://i.stack.imgur.com/UKXg2.jpg I am aware of how to make an entire window opaque, but I'm unsure if it's p ...

Issue with Angular-cli: typescript files not being generated when using the --dev option

Currently using angular-cli version 1.0.0-beta.14 with node version 6.6.0 on a Windows 32 bit x64 operating system. This setup utilizes the webpack version of angular-cli and I can successfully use ng build to compile. The output of ng build indicates that ...

Is it possible to reference a .js file within an HTML file using AngularJS?

Having a slight issue with an email function. I experimented with the 'nodemailer' package and successfully sent an email when coding in a .js file. Upon calling the .js file (node emailfile.js), the email was received as expected (code provided ...

Utilize CSS to create a column layout

I have some HTML output that I would like to style using CSS, but unfortunately I have no control over how the HTML is generated and cannot make any changes to it. Right now, I have a two-column layout set up here: http://jsfiddle.net/uvg6f3tr/ I'm ...

What is the best way to navigate back to the home page when the previous path does not exist in react router v6?

If a user is not authenticated on the checkout page, I guide them to the login page. After logging in, they are brought back to the previous page. However, my aim is to direct them to the home page if they were not redirected from a prior page. ...