Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

373
Views
Making tailwind/react component change color on prop - component only works with certain colors

I have an alert component which gets the prop alert_type based on which it should change it's icon and colour. When I pass 'good' or 'warn' it works fine but if I pass 'bad' it does not accept the colour red. Further when I just edit the colour (both on 'good' and on 'bad') to be 'red' or any other color it does not work except 'yellow' and 'green' always work. How can this be?

/* This example requires Tailwind CSS v2.0+ */
import {
  CheckCircleIcon,
  ExclamationIcon,
  XCircleIcon,
} from '@heroicons/react/solid'

interface AlertProps {
  title: string
  alert_type?: string
  children?: React.ReactNode
}

export function Alert({ title, alert_type = 'good', children }: AlertProps) {
  const [colour, Icon] = (() => {
    switch (alert_type) {
      case 'good':
        return ['green', CheckCircleIcon]
      case 'warn':
        return ['yellow', ExclamationIcon]
      case 'bad':
        return ['red', XCircleIcon]
      default:
        return ['green', CheckCircleIcon]
    }
  })()

  const colours = {
    bg: 'bg-' + colour + '-50',
    icon: 'text-' + colour + '-400',
    title: 'text-' + colour + '-800',
    content: 'text-' + colour + '-700',
  }

  return (
    <div className={'rounded-md p-4 ' + colours.bg}>
      <div className="flex">
        <div className="flex-shrink-0">
          <Icon className={'h-5 w-5 ' + colours.icon} aria-hidden="true" />
        </div>
        <div className="ml-3">
          <h3 className={'text-sm font-medium ' + colours.title}>{title}</h3>
          <div className={'mt-2 text-sm ' + colours.content}>{children}</div>
        </div>
      </div>
    </div>
  )
}

Edit: Without changing anything, green has now also stopped working.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem was that I am using string concatenation to build the classes. When Tailwind parses the file to detect which css classes to create it fails to pick those up. https://tailwindcss.com/docs/content-configuration#class-detection-in-depth

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!