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

425
Visualizações
Best way to change className in Next.js when a button is clicked, with CSS Modules?

I am creating a basic nav bar and I want to change it based on screen size. Once it hits 600px i'd like to hide the links and display a clickable nav button that will expand those options.

After console logging my list Elements I found that the className was given this 'Nav_floatLeft__H1YZ8'. So based on that finding, my code is as follows. However, my navigation does not display any changes when clicking the button.

I'm sure React has a better way of handling this situation, but I'm fairly new to it. Should I be using some kind of state/effect hook?

Nav:

const openCloseMenu = () => {
    console.log(document.getElementsByClassName(styles.floatLeft).className);
    let elements = document.getElementsByClassName(styles.floatLeft);

    if (elements.className === "Nav_floatLeft__H1YZ8"){
        alert("Changed to: Menu Bar Expanded");
        elements.className = styles.menuBarExpanded;
    }
    else {
        alert("Changed Back to: Float Left")
        elements.className = styles.floatLeft;
    }
}

return (
    <div className={styles.topNav}>
        
        <nav>
            <ul className={styles.inlineListItem}>
            
                <li className={styles.floatLeft}>
                    <Link href="/">
                        <a>Home</a>
                    </Link>
                </li>

                <li className={styles.floatLeft}>
                    <Link href="/search">
                        <a>Search</a>
                    </Link>
                </li>

                <li className={styles.menuBar}>
                    <button onClick={openCloseMenu}>Expand Nav</button>
                </li>

            </ul>
            
        </nav>
    </div>
)

CSS:

.inlineListItem{
    display: inline;
    list-style: none;
}

.floatLeft{
    float: left;
    margin: 1rem;
    padding-left: 1rem;
    text-align: center;
    color: white;
}

.floatRight{
    display: inline;
    float: right;
    color: white;
    margin: 1rem;
    padding-right: 2rem;
}

.menuBar{
    display: none;
    float: left;
    margin: 1rem;
    padding-left: 1rem;
    text-align: center;
    color: white;
}

.menuBarExpanded{
    display: block;
}

@media screen and (max-width: 600px) {
    .menuBar{
        display: block;
    }
    .floatLeft{
        display: none;
    }
    .floatRight{
        display: none;
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

ways are there to solve this problem. You can use material UI. To detect the breakpoint. Like for you case. If want to detect 600px. you can do something like that ---

const themeBreakpoint = theme.breakpoint.down('600px) // themeBreakpoint will be true under 600px.

using this flag you can change state and show what ever you want by using condition rendering.

Second Problem -- you can change any state based on onClick event.

like -- const[clicked,setclicked] = useState(false)

const handleClick = (e) =>{ setclicked(true) } now when you make that clicked flag true, You change you css class based on that flag.

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