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

405
Visualizações
How to create tab trap in react using js?

I have an modal window that will open when the user press on some button in the process.

I need, for accessibility, that the tab will be trap inside the modal window.

Now , unfortunately, the tab work on all the page.

How can I trap the tab inside the modal?

Demo : https://codesandbox.io/s/jolly-wood-pequk1?file=/src/App.js

The children prop will be two buttons components that I pass dynamically for each page.

import React from 'react'
import E from './Modal.style'

const Modal = (props) => {
  const {
    children,
    modalHeader,
    modalText,
    left,
  } = props

  window.onload = () => {
    const firstAnchor = document.getElementById('dialog-start');
    const lastAnchor = document.getElementById('dialog-end');

    function keydownHandler(e) {
      const evt = e || window.event;
      const keyCode = evt.which || evt.keyCode;
      if (keyCode === 9) { // TAB pressed
        if (evt.preventDefault) evt.preventDefault();
        else evt.returnValue = false;
        firstAnchor.focus();
      }
    }

    if (lastAnchor.addEventListener) lastAnchor.addEventListener('keydown', keydownHandler, false);
    else if (lastAnchor.attachEvent) lastAnchor.attachEvent('onkeydown', keydownHandler);
  }
  return (
    <E.Root left={left}>
      <div className="modal" tabIndex="-1" id="dialog-start">
        <button type="button" className="xButton" onFocus>X</button>
        {modalHeader && <h3>{modalHeader}</h3>}
        {modalText && <span className="modalText">{modalText}</span>}
        <E.ButtonWrapper id="dialog-end">
          {children}
        </E.ButtonWrapper>
      </div>
    </E.Root>
  )
};

export default Modal;
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

If you're open to using a package, then this package is really simple to use to achieve this https://www.npmjs.com/package/focus-trap-react

The example they give uses a modal too :)

<FocusTrap>
  <div id="modal-dialog" className="modal" >
    <button>Ok</button>
    <button>Cancel</button>
  </div>
</FocusTrap>

So you could do:

<FocusTrap>
 <E.Root left={left}>
      <div className="modal" tabIndex="-1" id="dialog-start">
        <button type="button" className="xButton" onFocus>X</button>
        {modalHeader && <h3>{modalHeader}</h3>}
        {modalText && <span className="modalText">{modalText}</span>}
        <E.ButtonWrapper id="dialog-end">
          {children}
        </E.ButtonWrapper>
      </div>
    </E.Root>
</FocusTrap>
about 4 years ago · Santiago Gelvez 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