Next.js is having trouble loading Tailwindcss colors from a variable value

I am currently working on creating a card component within Next.js, with multiple color options. However, I am facing an issue where the Tailwind colors are not being recognized when I attempt to utilize them.

Card.tsx:

interface Props {
  // Adding supported colors here - can be easily modified
  bgColor: "green" | "orange" | "yellow" | "blue" | "pink" | "purple" | "red";
  title: string;
  content: string;
  symbol?: JSX.Element;
}

const Card = ({ bgColor, symbol, title, content }: Props): JSX.Element => {
  let background: string;
  let border: string;

  // Using a switch statement due to issues with Tailwind color recognition 
  switch (bgColor) {
    case "green":
      background = "bg-green-300 text-green-900";
      border = "border-green-400";
      break;

    ...
    
    default:
      background = "bg-white border-black";
      border = "border-white";
  }

  return (
    <div
      className={
        "w-72 h-72 mb-3 p-8 border-2 rounded-md font-poppins " +
        background +
        " " +
        border
      }
    >
      <span className="text-5xl font-karla">{symbol}</span>
      <h1 className="text-2xl font-bold mt-4">{title}</h1>
      <p className="text-xl font-medium">{content}</p>
    </div>
  );
};

export default Card;

My intention:

interface Props {
  bgColor: string;
  title: string;
  content: string;
  symbol?: JSX.Element;
}

const Card = ({ bgColor, symbol, title, content }: Props): JSX.Element => {
  const background = "bg-" + bgColor + "-300";
  const border = "border-" + bgColor + "-400";
  const text = "text-" + bgColor + "-900";

  return (
    <div
      className={
        `w-72 h-72 mb-3 p-8 border-2 rounded-md font-poppins ${background} ${border} ${text}`
      }
    >
      <span className="text-5xl font-karla">{symbol}</span>
      <h1 className="text-2xl font-bold mt-4">{title}</h1>
      <p className="text-xl font-medium">{content}</p>
    </div>
  );
};

export default Card;

The code for the page:

import type { NextPage } from "next";
import { BiBookBookmark } from "react-icons/bi";
import Card from "../components/Card";

const Home: NextPage = () => {
  return (
    <main className="flex p-3 justify-around">
      <Card
        symbol={<BiBookBookmark />}
        bgColor="yellow"
        title="Services"
        content="We offer 4 different options for your child to choose from."
      />
       <Card
        symbol={<BiBookBookmark />}
        bgColor="green"
        title="Services"
        content="We offer 4 different options for your child to choose from."
      />
       <Card
        symbol={<BiBookBookmark />}
        bgColor="red"
        title="Services"
        content="We offer 4 different options for your child to choose from."
      />
    </main>
  );
};

export default Home;

Is there a way to conditionally load Tailwind colors, or would changing PostCSS be necessary?

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

Utilizing Next.js and Express as middleware, I am looking to configure the routes 'localhost:3000/newpage' and 'localhost:3000/newpage/' to be treated as the same route. How can I achieve this synchronization between the

I recently started using express and next, and encountered an issue while trying to set 'localhost:3000/newpage' and 'localhost:3000/newpage/' as the same route. Whenever I add a '/' at the end, it triggers a 404 error. For d ...

A step-by-step guide on seamlessly moving items between two columns with the help of react-beautiful-dnd and material ui

Exploring new coding techniques. I am interested in implementing a drag and drop feature between two columns (From Morning to Night, vice-versa) using React JS, Material UI, and react-beautiful-dnd. I have successfully implemented dragging and dropping wi ...

I am excited to incorporate superagent into my TypeScript-enabled React project

Currently, I am attempting to integrate superagent into my TypeScript-ed React project. I have been following a tutorial on TypeScript with React and encountered some difficulties when using superagent for server requests, despite successfully utilizing ma ...

Utilizing React TypeScript: Leveraging useRef for Linking purposes

Implementing useRef to Handle Link Clicks import {Link} from 'react-router-dom'; const myLinkRef = useRef<HTMLAnchorElement>(null); ... myLinkRef.current.click() ... <Link to={{pathname: '/terms'}} id='myLink' ref= ...

The server encountered a [MISSING_ADAPTER_METHODS_ERROR] when setting up Next-Auth in the application directory using Prisma on Supabase Postgres and SendGrid as the Email

Currently, I am integrating Next-Auth with a PrismaAdapter that is linked to a Supabase Postgres database within a Next.js 13 App Directory project. Additionally, I have set up a SendGrid connection, but I am uncertain if it has been configured correctly a ...

Is the useEffect hook executed twice in a production environment?

I'm facing a straightforward question here. I know that during development, running a code twice can help identify bugs faster. However, now I'm wondering if there's something I should configure to prevent this from happening in production w ...

Creating a task list without using JavaScript in the web browser and without relying on a database

Looking for some guidance on building a todo app for a job interview where JavaScript is disabled in the browser and no database can be used. Any JavaScript needs to be handled on the server side. I have some basic knowledge of node/express and serving H ...

Ensure the function has completed setting state before proceeding to the next function

async componentDidMount() { this.loadSelectors(); this.useSelectors(); }; loadSelectors = () => { this.setState({"Selector": "Test"}); } useSelectors = () => { console.log(this.state.Selector); } Is there a way to ensure that loadS ...

When two functions are provided, the onClick event in React / Material UI does not function as expected

Within a React Component, I am utilizing a Material UI Button. The button is expected to trigger both handlePopupClickOpen() and props.function?.(). interface ConfirmBorrowPopupProps{ amount : number; function: Function; } export function Confir ...

An error occurs when trying to execute the "products" function after performing destruct

I designed my homepage to showcase the products using the redux method. However, I did not want to display them all on the home page at once, so I created a single product component instead. But then I decided I wanted to show the products in a carousel us ...

I am experiencing an issue with the checkbox in my React app where the state is not updating after it has been

I am currently building a todo app using React, but I'm encountering an issue where nothing happens when I click the checkbox. I've provided my code below: App.js import './App.css'; import React from 'react' import TodoItem ...

NextJS - Error: Invalid JSON format, starting with a "<" symbol at position 0

After following a tutorial on NextJS, I attempted to make some modifications. My goal was to include the data.json file on the page. However, I kept encountering the error message "Unexpected token < in JSON at position 0." I understand that I need to con ...

If you're using `react-router-dom` v6 and `useNavigate`, you may need to refresh the page

There is a page called /studentprofile where users can view their profile details. When the user clicks the 'edit profile' button, they are taken to /editprofile where they can update their profile using a form. After clicking the 'update&ap ...

Failed to fully install all dependencies for my project with yarn install

After cloning a project from gitlab, I attempted to install the dependencies using the yarn install command. However, there are several dependencies that yarn is unable to install and it keeps showing the error message: info There appears to be trouble wit ...

Encountered CSRF validation error while working with a Python Django backend in conjunction with React frontend using Axios for making POST requests

I recently completed a tutorial at and now I'm attempting to add a POST functionality to it. Despite obtaining the csrf from cookies and including it in the "csrfmiddlewaretoken" variable alongside a test message in json format for the axios function ...

In React JS, the data from my response is not being saved into the variable

My goal is to store the response data in the variable activityType. Within the useEffect function, I am iterating over an API based on the tabs value. The API will return a boolean value of either true or false. While I can successfully log these values ...

Following the latest update to the query-string library, the test:ci process is now encountering errors

The current development environment is utilizing next.js version 13. An issue has arisen after updating the query-string library to version 8.1, causing the test:ci to fail at a specific point. Here are the details of the failure: before "query- ...

Issue encountered when attempting to locate the file path within the getStaticProps function of an internal page in Next Js

I'm currently implementing a multi-language feature on my Next JS app. Below is the folder structure of my project. https://i.stack.imgur.com/5tD2G.png On the Home page (index page), I successfully retrieved the locales module using getStaticProps: ...

Switching the class from "untoggled" items to the selected item in React

Recently, I developed a toggle component known as Accordion. Within this component, I am iterating through an array of objects and displaying them in the following format: {object.map((o) => ( <Accordion key={o.id} title={o.question} className="i ...

Display modal within a React list

I need to display a list of items with an edit button for each item, which should trigger a modal showing the details of that specific item. Initially, I had a single modal component in the parent element and passing the visible values to the parent state ...