New feature in Next.js 13: Utilizing a string within className

Looking for a way to dynamically generate radio buttons in Next.js using a list of strings? Utilizing Tailwind CSS classes, you can modify the appearance of these buttons when checked by leveraging the peer/identifier classname. But how do you include the string from the list within the className? Check out my solution below.

<fieldset>
    <legend>Some Radio Buttons</legend>
    {names.map((name: string) => (
        <>
            <input id={name} className="peer/{name} appearance-none absolute h-0 w-0 opacity-0" type="radio"/>
            <label htmlFor={name} className={"rounded-button peer-checked/{name}:bg-indigo-500"}>
                <span>{name}</span>
            </label>
        </>
    ))}


</fieldset>

Answer №1

It is doubtful that Tailwind has the ability to manage dynamically created class names starting with "peer-*". One way to work around this issue is to enclose each input + label pair in a div or span and utilize the "peer" class names instead.

<fieldset>
  <legend>Some Radio Buttons</legend>
  {names.map((name: string) => (
    <span key={name}>
      <input
        id={name}
        type="radio"
        name="names"
        className="peer appearance-none absolute h-0 w-0 opacity-0"
      />
      <label htmlFor={name} className="rounded-button peer-checked:bg-indigo-500">
        {name}
      </label>
    </span>
  ))}
</fieldset>

Is this solution suitable for your needs?

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

Having trouble with the margin for the first/last child in your Next.js and Tailwind CSS setup?

A similar situation arose in relation to this GitHub issue I came across. https://github.com/tailwindlabs/tailwindcss/issues/1930 In the context of Next.js and Tailwind CSS, there seems to be an issue with the First / Last child for margin not functionin ...

What could be causing the sluggish hot reloading in my Next.js 14 application that utilizes TailwindCSS, Shadcn, and React Icons?

I've been struggling with slow hot reloading times in my Next.js 14 project that incorporates TailwindCSS, Shadcn, and React Icons. Below is a comprehensive list of all the packages I have installed: radix-ui/<a href="/cdn-cgi/l/email-protection" c ...

The Angular Material Autocomplete component fails to show items upon upgrading the angular/material package to the newest version

Issue with Angular Material Autocomplete component not displaying items after updating angular/material package to the latest version. The autocomplete was functioning correctly with "@angular/material": "^2.0.0-beta.10" but encountered issues when update ...

Transform TypeScript class into an object

Is there a way to transfer all values from one typescript class, Class A, to another matching class, Class B? Could there be a method to extract all properties of Class A as an object? ...

Changing a const variable to true based on a condition in React

Is there a way in react to reassign a constant variable to true based on a condition being met? I keep encountering an error saying 'displayInternalForm' is declared but its value is never read. export async function getStaticProps({ params }) { ...

Updating Angular model remotely without relying solely on the controller

I am struggling to call the addRectangleMethod method from my Javascript code in order to retrieve server-side information into the Angular datamodel. However, I keep encountering an error stating that the method I'm trying to call is undefined. It&ap ...

Utilizing Angular 4's piping feature to manipulate data sourced from an API observable within

I am currently working on setting up a filter for my stories. After subscribing to the API call, I receive the data in the form of an array of objects. However, I am encountering an error while trying to apply filters. Here is a snippet of relevant inform ...

The SortKey<> function is ineffective, even though the individual types it uses work perfectly fine

After my initial inquiry about TypeScript, the focus shifted to this current topic. In my previous question, I was attempting to develop a generic sort() method that could function with an array of sort keys defined by the SortKey type featured there (and ...

Encountered an error while loading resource: net::ERR_CONNECTION_REFUSED in Nodejs

I am currently working on a form in my angular 6 app with nodejs, utilizing nodemailer for sending emails. Unfortunately, I am encountering an error that reads: Failed to load resource: net::ERR_CONNECTION_REFUSED : :3000/contact/send:1 Below is the form ...

Is there a way to activate decorator support while running tests using CRA 2.1?

Struggling to set up testing for a React application utilizing decorators and Typescript within Create React App v2.1.0 Aware that official support for decorators is lacking. Successfully running the application with the help of React App Rewired and @ba ...

Enhancing Performance by Optimizing Module Loading for Frontend Component Rendering in Next.js

When building a Nextjs app, it is common to use the same package across multiple components on a page. However, in the case of client-side rendering, the default behavior does not optimize the loading of the common package. This can result in a significant ...

Is there a way to subscribe to various observables simultaneously in Angular 2, and then pause until fresh data is available on each of them?

I have an Angular component that relies on 3 services, each of which has an observer I can subscribe to. The view of the component needs to be updated whenever there are changes in the observed data, which occurs through websockets (feathers.js). I want th ...

Exploring the functionality of API routing in Next.js

Seeking assistance from someone well-versed in Next.js as I encounter an issue while transitioning my express API routes to Next.js's internal API routes within pages, which appears to be a promising solution. The problem lies in getting it to functio ...

The Next JS Image component is functioning properly on the local server, but encountering issues when deployed to

I have successfully deployed my Next.js project on Firebase Hosting. Within the project, I am utilizing the Next.js image tag to display an image from Firebase Storage. To ensure proper integration, I included the Firebase domain in the next.config.js file ...

Using Iframe for WooCommerce integration and implementing Facebook login within an Ionic application

I have created an Ionic application that includes an iframe from a Wordpress website. Here is the code snippet from my home.page.ts file: import { Component } from '@angular/core'; import { DomSanitizer } from "@angular/platform-browser"; @Com ...

The eslint rule 'import/extensions' was not found in the definition

I'm encountering two errors across all TypeScript files in ESLint on VS Code: Not able to find definition for rule 'import/extensions'.eslint(import/extensions) Not able to find definition for rule 'import/no-extraneous-dependencies&apo ...

Reusing methods in Javascript to create child instances without causing circular dependencies

abstract class Fruit { private children: Fruit[] = []; addChild(child: Fruit) { this.children.push(child); } } // Separate files for each subclass // apple.ts class Apple extends Fruit { } // banana.ts class Banana extends Fruit { } ...

Error: Unable to locate module: 'fs' - NextJS

Currently, I am working on integrating node-jsencrypt with NextJS (tsx): index.tsx import JSEncrypt from 'node-jsencrypt'; package.json "node-jsencrypt": "^1.0.0" Log error - ./node_modules/node-jsencrypt/index.js:2:0 Modu ...

Dealing with code in Angular when transitioning to a different component

I have an Angular component that displays data and includes a button called "Go to Dashboard". I want to implement a feature where the user can either click on this button to navigate to the dashboard or have the application automatically redirect them aft ...

Issue with firing Facebook pixel after router.push() in Next.js

Within this code block is FB pixel tracking code <Script id="some-id" strategy="afterInteractive">some fb pixel code</Script> The issue arises when navigating to a page containing the script using router.push(SOME_ROUTE). T ...