question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Having an issue with trapping focus working on just the first and last element

See original GitHub issue

FIrst off, thanks for such an awesome maintenance of this project! Been reading through all the other issues and your responses have been super insightful and through!

I’ve created a complex component for a context menu and trying to trap focus within this context menu for easy navigation and accessibility reasons. The problem I am having is that focus-trap-react is only locking in the first and last element of my menu and won’t let me tab into anything in the middle. I tried to strip out all the complexities (styled components, array.map()) and still having this issue.

Here is the most simple component I created. I can tab into “label 1” and then shift+tab into “label 4”

      <FocusTrap active={true} focusTrapOptions={{ fallbackFocus: "#menu" }}>
        <div id="menu">
          <button>label 1</button>
          <button>label 2</button>
          <button>label 3</button>
          <button>label 4</button>
        </div>
      </FocusTrap>

https://user-images.githubusercontent.com/10911227/185664313-a41b0ba1-6b1b-46a1-8414-500d9d734216.mov

Not sure if you have seen this or any have tips?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
pushkatelcommented, Aug 29, 2022

Figured out the problem and hopefully this can be a helpful note to others. I had an unneeded event.preventDefault() on all keypresses (not just the Escape key that I needed to close the menu)!

Watch out for any keyboard events!

0reactions
stefcameroncommented, Aug 29, 2022

Great! Thanks for posting your solution. I’m going to close this issue now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When landing on last element of focus trap, it immediately ...
It immediately goes back to the first element more or less because of the timing of events. In your code you are using...
Read more >
A CSS Approach to Trap Focus Inside of an Element
If focus is on the first tab-able element inside the dialog, moves focus to the last tab-able element inside the dialog.
Read more >
focus-trap stuck on elements with positive tabIndex · Issue #375
Hi! It seems elements with positive tabIndex break tab/shift+tab navigation. When such elements receive focus moving forward become unavailable.
Read more >
Using JavaScript to trap focus in an element | hidde.blog
Trapping focus is a behaviour we usually want when there is modality in a page. Components that could have been pages of their...
Read more >
Focus Trapping for Accessibility (A11Y) | by Rahul Kumar
When the modal opens, the first input element (which element should get the focus depends on the page's interaction design) should get the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found