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

103
Visualizações
How to increase the specificity of a class of props?

I have a component with a button. It has a default class and a class passed to its from the props. But sometimes the default class overwrites the class from the props. Is it possible to increase the specificity of the class from the props or lower the specificity of the default class?

  1. Do not suggest using !important
  2. Do not suggest to prescribe default styles for the button tag, I do not want to apply them to all buttons in the project
  3. Assigning an id for each button is also not an option
  4. In the props, it is the class name that needs to be passed, not the style
import React from 'react'
import styles from './Button.module.scss'
import classNames from 'classnames'

const Button = ({ children, className, ...props }) => {
  return (
    <button {...props} className={classNames(styles.btn, className)}>
      {children}
    </button>
  )
}

I can't find a beautiful and convenient solution. Now I'm adding !imortant in a class of props. I just hope there's some css or js trick I'm not aware of.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Any additional selector will be increase priority of passed classname

component.module.scss:

button.passed-class-name {
 color: red;
}

button.module.scss:

.btn {
  color: green
}

Component.js

import styles from './component.module.scss'
import { Button } from 'components/Button'

const Component = () => {
    <Button className={styles.passedClassName} />
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The ideal solution could not be found. I decided not to reinvent the wheel and write a UI kit for the project, like everyone else. UI kit styles are connected earlier, so styles from parents will rewrite them without !important and without increasing specificity.

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