0

I'm having an issue with nextjs/reactjs. I have two tsx files: index.tsx and customAlert.tsx. The issue that I'm having is that the alert doesn't seem to change colour even though the css classes are added to the alert html element. For css I'm using tailwind and it works fine and it's not the issue.

pages/_app.tsx

import type { AppProps } from "next/app"
import "tailwindcss/tailwind.css"

export default function MyApp({ Component, pageProps }: AppProps) {
    return <Component {...pageProps} />
}

components/customAlert.tsx

export default function customAlert(status: string, title: string, message: string) {
    const types: {[key: string]: any} = {success: "green", error: "red", warning: "yellow"}
    const colour = types[status]

    return (
        <>
            <div id="alert" className="absolute bottom-0 right-0 mb-4 mr-4">
                <div className={`bg-${colour}-500 text-white font-bold rounded-t px-4 py-2`}>
                    {title}
                </div>
                <div className={`border border-t-0 border-${colour}-400 rounded-b bg-${colour}-100 px-4 py-3 text-${colour}-700`}>
                    <p>{message}</p>
                </div>
            </div>
        </>
    )
}
pages/index.tsx

import customAlert from "../components/customAlert"


export default function Index() {
    return(
        <div>
            {customAlert("success", "hi mom", "hi mom")}
        </div>
    )
}

how it should look like

how it looks like

1 Answers1

0

Please checkout the class-detection-in-depth of tailwind. You should not construct class names dynamically.

Wrong:

bg-${colour}-100

You can creat a function or any other way to make sure any class names you’re using exist in full:

    const getColorClass = (color) => {
  switch (color) {
    case 'success':
      return 'bg-green-100';
    case 'error':
      return 'bg-red-100';
    default:
      return 'bg-yellow-100';
  }
}
saboshi69
  • 689
  • 5
  • 12