Having an issue with trapping focus working on just the first and last element
See original GitHub issueFIrst 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>
Not sure if you have seen this or any have tips?
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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!
Great! Thanks for posting your solution. I’m going to close this issue now.