Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

374
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda