TailwindCSS in React.Js does not seem to accept randomly generated color codes

Check out my Messages component below:

import randomColor from "random-color";
import React from "react";
import "../Chat.css";

function DisplayMessage({ username, message }) {
  const changeTextColor = randomColor(0.99, 0.99).hexString();
  console.log(changeTextColor);
  
  return (
    <div className="flex-1 px-5 py-2 chatting_body  ">
      <div className="flex flex-col px-4 py-2 bg-slate-600 border rounded-md max-w-fit ">
        <div className={`text-sm font-bold text-[${changeTextColor}] `}>{username}</div>
        <div className="flex justify-between items-end space-x-3 ">
          <p className="text-white text-lg ">{message}</p>
          <p className="text-gray-400 text-xs " >{new Date().toLocaleTimeString()}</p>
        </div>
      </div>
    </div>
  );
}

export default DisplayMessage;

Upon using console.log(changeTextColor);, I'm able to retrieve the Hexcode, however there seems to be an issue with tailwind fetching it correctly.

Answer №1

When working with Tailwind CSS, it's not possible to use runtime dynamic values directly. In such cases, it is recommended to utilize custom properties instead.

You can find an example here.

<div class="flex-1 px-5 py-2 chatting_body  ">
  <div class="flex flex-col px-4 py-2 bg-slate-600 border rounded-md max-w-fit ">
    <div class="text-sm font-bold text-[var(--changeTextColor,black)]">{username}</div>
    <div class="flex justify-between items-end space-x-3 ">
      <p class="text-white text-lg ">{msg}</p>
      <p class="text-gray-400 text-xs ">{new Date().toLocaleTimeString()}</p>
    </div>
  </div>
</div>
<button type="button" class="rounded bg-blue-400 p-2">change color</button>

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

A guide on altering the color of a badge through programming

I am curious to learn how I can dynamically change the color of a badge in Angular. My goal is to initially set the color of the badge to white, and then if the percVLRiskTotal reaches a specific value, change the color to green as an example. CSS: <sp ...

Leveraging API JSON information in conditional return statements with React

Working on a small Express/React app (not for production), I am validating a hashed pin in express and returning either message: false if validation fails or message: <cardnumber> if validation is successful. Currently, in my react frontend, I am try ...

Resource loading unsuccessful: server returned a 401 status code for React webpage

( ) I'm currently working on a website dedicated to searching for GitHub users as part of my React course. However, I seem to be facing an issue with the website not fetching the response properly, possibly due to an Axios error. When searching for ...

Align content distributed evenly will not display a division

I am currently using React to code and learning by doing so. I have two different images displayed, each followed by a div element with a height of 20px and a brown background color. I have set the height to "100%" and justifyContent to "space-between", bu ...

Whenever I navigate to localhost:3000 while using Next JS, it redirects me to 'http://localhost:3000/public/index' and displays a 404 error

In my pages folder, I don't have a public folder. Instead, I have the index.js file directly inside the pages folder. However, when I start the server at localhost:3000/, it automatically redirects to http://localhost:3000/public/index and displays a ...

The visibility of Material-ui components is not consistent when being displayed on a production environment

In my React project, I've integrated Material-UI components which appear to work fine locally. However, when deploying to Firebase, these same components do not display correctly. I suspect there might be a compatibility issue between Firebase and Mat ...

Ways to determine the current page I am viewing

I am working with a list of tabs and I need to track my current location every time I click on a specific tab. My MainCTRL controller, which is the parent of all tab controllers, needs to be aware of the active tab at any given moment. I attempted to use n ...

I am in the process of creating a dropdown menu for a navbar that will appear when the cursor hovers over a specific element, complete with an arrow pointing upwards

In my current project with NextJS and Tailwind CSS, I've successfully created a dropdown menu but I'm facing an issue with positioning the arrow to point to the specific element being hovered on. In a previous version, I tried rotating a div at 4 ...

Tips for controlling HTML elements using JavaScript

I'm currently working on implementing a mouse-over scale effect for an HTML image. I chose to use JavaScript for this task because I need the ability to manipulate multiple elements in different ways simply by hovering over one element. Below is the J ...

How to Align Tabs and Header in R Shiny Interface

I am currently working on centering both my tabs and header in my R Shiny page. I attempted to center them using div() with class=span7, but I encountered an error stating that an argument was missing. shinyUI(fluidPage( headerPanel("Header"), mai ...

Need a guide to identify HTML elements with a particular attribute?

I attempted to find this information online, but I wasn't successful. Suppose I am interested in knowing all the HTML elements that contain a "background" attribute. I'm aware that some web browsers may not support certain elements. Off the top ...

How come the state update result is triggering two different responses?

I recently developed a basic TicTacToe game using react-hooks. At times, I notice that the result is displayed before the state update on the screen. https://i.sstatic.net/LMDeZ.png Other times, the result shows up after the update is visible on the scr ...

What could be causing the misalignment of the Datepicker calendar in Material UI?

I have integrated a datepicker using the library "@mui/x-date-pickers/DatePicker". import { DatePicker } from "@mui/x-date-pickers/DatePicker"; import { AdapterMoment } from "@mui/x-date-pickers/AdapterMoment"; import { Locali ...

Jumping transitions in thumbnail images

Could you please visit I've noticed that when hovering over the thumbnails in the last column, the images jump a bit after transition, especially in Firefox. Do you have any suggestions on how to resolve this issue? ...

Is it possible to use a specific npm command to automatically scan the dependencies in the current directory and then add them to the package.json file?

Is there a method to automatically scan the dependencies in the current directory and update package.json using an npm command? I am familiar with, $npm install --save $npm install --save-dev Referenced from https://docs.npmjs.com/getting-started/us ...

closing the space between rows at the top

Having trouble adjusting the top margin to match the left and right margins between items. How can I narrow the top gap so it aligns with the spacing of each bootstrap button? https://i.stack.imgur.com/1dZEC.jpg html <div ng-controller="RecipeCo ...

Showing headings in the table vertically

I have a header1 and header2 with corresponding data1 and data2 that I want to display differently. h h e e a a d d e e r r 1 2 data1 data2 To enhance the presentation, I wish to add borders around the head ...

React app experiencing issues with emoji rendering, build failure due to terser plugin

In my React application, emojis are utilized in various places. While everything works correctly when running the app locally in development mode, the emojis display as strange Unicode characters when built in production mode. For example, the ...

Unable to change the value of selected items in checkbox event within a React context

Today marks the beginning of my journey into developing a React application. I am currently faced with the challenge of retrieving the row ID of a checked checkbox in a React table. Utilizing hooks, I have managed to transfer the states to another compone ...

The function fromEvent is throwing an error stating that the property does not exist on the type 'Event'

I'm attempting to adjust the position of an element based on the cursor's location. Here is the code I am currently using: this.ngZone.runOutsideAngular(() => { fromEvent(window, 'mousemove').pipe( filter(() => this.hove ...