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

205
Visualizações
Press and hold arrow keys to scroll in grid in react

Take the following React (17.0.2) component:

const Grid = () => {
     return (
          <div id='grid' style={{display: 'flex', flexDirection: 'column'}}>
               <div style={{display: 'flex'}}>
                   <div>
                       1
                   </div>
                   <div>
                       2
                   </div>
                   <div>
                       3
                   </div>
               </div>
               <div style={{display: 'flex'}}>
                   <div>
                       4
                   </div>
                   <div>
                       5
                   </div>
                   <div>
                       6
                   </div>
               </div>
               <div style={{display: 'flex'}}>
                   <div>
                       7
                   </div>
                   <div>
                       8
                   </div>
                   <div>
                       9
                   </div>
               </div>
          </div>

     )
}

It's just a simple grid with some simple data. It's easy enough to add event listeners to it that allow you to scroll with arrow keys if you tap them. How do I make it so holding down the arrow keys will scroll continually?

The main thing I've tried to far is:

  1. Save the selected cell in React state.
  2. On a keydown event, set an interval that that moves the selected cell in the right direction.
  3. When the keyup event, clear this interval

This suffers from the problem that it is really inconsistent - the interval is only sometimes cleared and sometimes scrolling just continues!

Bonus points if you can make the grid bigger, and then keep the element that is scrolled in the viewport!

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

0

Just simple with this - use ref to handle current element focus and user press keydown

const myRef = useRef();

useEffect(() => {
    const node = myRef.current;
    node.addEventListener('keydown',(e) => {
      const active = document.activeElement;
      if(e.keyCode === 40 && active.nextSibling) {
        active.nextSibling.focus();
      }
      if(e.keyCode === 38 && active.previousSibling) {
        active.previousSibling.focus();
      }
    });

} , [])

<div id='grid' ref={myRef} style={{display: 'flex', flexDirection: 'column'}}>

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