Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

404
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!