The function designed to create in-line TailwindCSS classNames may work inconsistently in React and NextJS environments

Currently, I am utilizing TailwindCSS to style a web application. One frustrating aspect is that when attempting to add before: and after: pseudo-elements, it must be done individually.

For instance, take the navigation drop-down button styling:

<span className={'block relative border-2 w-full h-3 content-["test"]' + tagProps('before', ['block', 'absolute', 'top-2', 'border-2', 'w-full', 'h-0', 'content-[""]']) + tagProps('after', ['block', 'absolute', 'bottom-2', 'border-2', 'w-full', 'h-0', 'content-[""]'])}></span>

Along with this function:

const tagProps = (tag, prop) => {
let x = ' ';
for(const property of prop)
{
    x = x.concat(tag.concat(':').concat((property).concat(' ')));
}
return(x);

Mysteriously, the styles appear to apply randomly. Closing the browser, VSCode, then reopening them yields a 25% chance of successful style application. The reason for using a function here is due to TailwindCSS not allowing custom properties inside brackets for pseudo-elements.

If each pseudo-element were explicitly written out, the line of code would stretch extensively:

<span className='block relative border-2 w-full h-3 content-["test"] before:block before:absolute before:top-2 before:border-2 before:w-full before:h-0 before:content-[""] after:block after:absolute after:top-2 after:border-2 after:w-full after:h-0 after:content-[""]'></span>

This approach seems unreasonable. Additionally, I am unable to format the JSX statement in multiple lines when utilizing the class name generation function.

The question arises - why is this library praised highly? How does it differ from using CSS modules in nextjs?

Your feedback and suggestions on how to tackle this dilemma are greatly appreciated.

Answer №1

Dynamic class names may not be recognized by Tailwind CSS

To learn more about handling dynamic class names, visit this link

If you encounter this issue, consider using safe-listing to create class names

For information on safe-listing classes, check out this resource

Credit to @stickyuser for the tip

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

The find function within $(this) is malfunctioning

I'm having issues with displaying/hiding content when clicking on a table row. Here is the simplified code snippet: HTML: <table> <tr onclick="showDetails()"> <td>Some text <br> <span class="hiddenC ...

I am looking to utilize React and Next.js to map my array from an object. The component is a function employing hooks

I am attempting to map an array within an object and encountering some challenges. The structure of the object is as follows: const data = {data: (6) [{…}, {…}, {…}, {…}, {…}, {…}], page: 2, per_page: 6, total: 12, total_pages: 2} I have been ...

Making a column in a Vue data grid return as a clickable button

My goal is to utilize vue.js grid to display multiple columns with calculated text values, along with a clickable column at the end that triggers a dynamic action based on a parameter (such as calling an API in Laravel). However, when I include the last c ...

Unable to generate upload link using apollo-file-upload due to credential issues

After implementing the credential option in apollo-file-upload to accept and handle cookies in the browser, I encountered a crash. Interestingly, the crash occurred with cookies set in Firefox but not in Chrome: apollo-client: import { createUploadLink } ...

Tips for displaying an HTML page using JavaScript

In my project, I am working with an .html file (File X) that needs to immediately open another .html file (File Y) based on a certain value. Could someone provide guidance on the best way to achieve this using JavaScript? Additionally, I would like the pa ...

Next.js server component allows for the modification of search parameters without causing a re-fetch of the data

I have a situation where I need to store form values in the URL to prevent data loss when the page is accidentally refreshed. Here's how I am currently handling it: // Form.tsx "use client" export default function Form(){ const pathname ...

What is the best way to use JavaScript to conceal a section of a form div?

After receiving some code from a certain platform and implementing it to hide part of my form div element, I noticed that the element hides and unhides quickly when running in a browser. This rapid hiding and showing behavior occurs when clicking on the bu ...

Changing the format of PHP SQL results from vertical to horizontal

Is there a way to convert the vertical table generated by the following code into a horizontal one with stylish formatting? Additionally, is it possible to display certain columns in a popup window based on query parameters using jQuery or CSS? <html&g ...

javascript design pattern - achieving unexpected outcome

In the code snippet provided, the variable a is turning out to be undefined. Are you expecting it to display the parameter value passed in the parent function? function test(a) { return function(a) { console.log('a is : ' + a); // Ou ...

What is the best way to dynamically adjust my circular progress based on my data in React JS?

Currently, I'm working on a MERN application and I'd like to incorporate a circular progress feature into the dashboard. The goal is to have the circular indicator update based on the data retrieved from an API endpoint. While I've made some ...

Can you create asynchronous code or functions without using Web APIs at all?

Even though JavaScript is single-threaded, I can't help but wonder about a function that takes an unusual amount of time without any involvement from web APIs like this: console.log("start") function banana() { let bananaCount = 10; while (b ...

NodeJs - Issue: Headers cannot be changed once they are sent & TypeError: req.next is undefined

My goal is to retrieve data from a MySQL database by calling methods to insert and read information. The reason I am doing this is because node.js operates asynchronously. Here is my code: exports.list = function(req, res){ var moduleRows; req.getCo ...

Customizing the appearance of selection dropdown options in React

Is it possible to customize the styling of the choices in a React input dropdown? For instance, I am interested in creating an autocomplete dropdown that presents the options neatly arranged in columns. Essentially, I want to design a dropdown data grid t ...

Extracting specific key-value pairs from JSON data

Working with JSON data, I encountered a need to pass only specific key-value pairs as data to a component. Initially, I resorted to using the delete method to remove unwanted key-value pairs, which did the job but left me feeling unsatisfied. What I truly ...

Incorporating Socket.io with Express.js for real-time communication

I am currently working on developing an application where users can request services and receive notifications once a service provider accepts their request. The technology stack I am using includes Node.js, Express.js, Socket.io, and React.js. Below is t ...

Filtering data in an antd table by searching

Just starting out with React hooks, specifically using TypeScript, and I'm struggling to implement a search filter with two parameters. Currently, the search filter is only working with one parameter which is 'receiver?.name?'. However, I wo ...

The functionality of nested dynamic routing in the API is experiencing issues

Looking to extract product details from a specific category of products? My folder structure appears as follows: https://i.stack.imgur.com/1UCy3.png In "productId/index.jsx" => This snippet is utilized to retrieve individual product details: ...

Unexpected JavaScript behavior triggers Safari's crash on iOS platforms

In my Sencha Touch application, I am implementing a feature where users can download 5000 records in JSON format and display them in an Ext.List control. The downloading of records works smoothly using JSON.parse() and storing the data locally. However, u ...

Does CSS padding-top: 100% take into account the parent's width?

I'm a bit perplexed by this discovery and I can't quite comprehend the rationale behind it. The provided code snippet showcases two nested DIVs. The outer DIV has specific dimensions and a relative position set, while the inner DIV also has fixe ...

Learn how to trigger the keydown function in VUE programming

I am trying to create a method that will be triggered whenever any key on the keyboard is pressed, regardless of where the focus is. I want to be able to determine which key was pressed in this method. Currently, I have set up an event listener for keydow ...