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

114
Visualizações
Invert two React component text colors when clicking on input tag

I am new to React and did not find any solution to this simple problem.

I want to revert LangText colors when the toggle button is clicked - initially ENG is white and EST is black and every time input is clicked the colors invert.

I have separate files for returning and styling components.

I tried to change the color of a variable (located on top of the style-components file) inside the styled component tag, so with CSS - inside Input pseudo-class &:checked + span. Looked surreal and didn't work in any way.

I would be very thankful for an example of how to change two colors at the same, time in this case. There are always too few examples with components, usually, CSS is used separately, but for me this method is more readable and logical.

import React from 'react'
import { Input, InputWrapper, Slider, ToggleContainer, LangText} from './LangugageToggleElements';

const LanguageToggle = ({onChange}) =>  {
 
    return(
        <ToggleContainer>
            <InputWrapper>
                <Input type="checkbox" onChange={onChange}/>
                <Slider>
                    <LangText>ENG</LangText>
                    <LangText>EST</LangText>
                </Slider>
            </InputWrapper>
        </ToggleContainer>
)} ;


export default LanguageToggle

import styled from "styled-components";

let toggleBackground = "#000";
let textColor = "#fff";

export const ToggleContainer = styled.div`
    position: absolute;
    top: 10px;
    right: 20px;
`
export const InputWrapper = styled.label`
    position: relative;
    display: flex;
    justify-content: center;
    text-align: center;
`

export const Input = styled.input`
    position: absolute;
    left: -9999px;
    top: -9999px;

    &:checked + span {
        &:before {
            left: 52px;
        }
    }
`

export const Slider = styled.span`
  display: flex;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  width: 105px;
  border-radius: 100px;
  position: relative;
  transition: background-color 0.2s;
  color: ${textColor};

  &:before {
      content: "";
      position: absolute;
      top: 15px;
      left: 2px;
      width: 50px;
      height: 20px;
      border-radius: 45px;
      transition: 0.2s;
      background: ${toggleBackground};
      box-shadow: 0 2px 4px 0 rgba(0, 35, 11, 0.2);
      }
`;

export const LangText = styled.p`
    padding-right: 5px;
    padding-left: 5px;
    z-index: 1;
    letter-spacing: 3px;
`;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think you can simply use a useState hook to maintain checkbox and the use it for your LangText.

<ToggleContainer>
    <InputWrapper>
        <Input type="checkbox" onChange={()=>{
          onChange();
         //use state hook to maitain the checkbox value
        }}/>
        <Slider>
            <LangText className={isChecboxValue === x ? color : invert }>ENG</LangText>
            <LangText className={isChecboxValue === y ? invert: color}>EST</LangText>
        </Slider>
    </InputWrapper>
</ToggleContainer>
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