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

139
Visualizações
How to change css when theme changes in React?

I am developing a library of react components and asked an important question. How to correctly change styles (css) when changing the application theme.
Appeal to those who already have experience in this and who can advise their approach or solution.
Now I'll tell you how it works for me:

I have my own themeProvider wrapped in a Context with the ability to change the theme. When the theme changes in the provider, the changeCssVariables method is called.

export const changeCssVariables = (theme) => {
    const root = document.querySelector(':root');

    // root.style.setProperty('--default-color', 'orange');

    const cssVariables = [
        'color', 
        'background',

        'color-uibutton',
        'background-uibutton',
        'box-shadow-uibutton',
        'color-disabled-uibutton',
        'background-disabled-uibutton',

        'background-uiradiobutton',

        'color-uiinputext',
        'color-notes-uiinputext',

        'color-subsection',
        'background-subsection',
        'box-shadow-subsection',
        'alt-subsection',
        'on-subsection',
        'off-subsection',

        
    ];

    cssVariables.forEach(element => {
        root.style.setProperty(
            `--default-${element}`, 
            `var(--theme-${theme}-${element})`
        );
    })
}

What happens in general: it has a global CSS with variables default, light and dark themes. When changing the theme, a method is called that changes the default variable to the variable of the selected theme.
Example css:

--default-color: var(--theme-light-color);
/* Themes */

    --theme-light-color: #000;
    --theme-dark-color: #fff;

I don't like that you have to pull in all the css and change it this way. What is the solution? Thanks for the help!

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

0

Found a solution in using styled-components package
Created my wrapper as ThemeProvider (Context)

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