Click in FocusTrap causes first item to focus, causing the view to scroll back to the top
See original GitHub issueHi, I wonder if you can help me
We have a Modal with FocusTrap, initially focus is set to the close button, which is what we’re after.
But we’re seeing an issue where, when ever the modal is clicked focus goes back to the close button, and as our Modal has scrollable content, causes the view to scroll back to the top.
I’ve narrowed it down to:
// In case focus escapes the trap for some strange reason, pull it back in.
function checkFocusIn(e) {
// In Firefox when you Tab out of an iframe the Document is briefly focused.
if (container.contains(e.target) || e.target instanceof Document) {
return;
}
e.stopImmediatePropagation();
tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode()); //<----- Here
}
Can you explain the intention? Is the entire function because of: “In case focus escapes the trap for some strange reason, pull it back in.”
- Thanks Chris
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (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 the keypress happens on the first focusable element, then focus the last focusable element in the chain and vice versa.
Read more >Modal - Bootstrap
Modals are built with HTML, CSS, and JavaScript. They're positioned over everything else in the document and remove scroll from the <body> so...
Read more >Dialog Focus in Screen Readers - Adrian Roselli
Whatever launched the modal receives focus again when the modal closes. Easy-peasy. The trickier question is where does focus go when you open...
Read more >How to fight the <body> scroll. First of all - Medium
Step to prevent scroll on <body/> element: Add overflow:hidden on body element. Handle touch events, for Safari. Keep the scroll bar gap.
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
Hi there,
We would also be grateful to see this released (and pushed out to focus-trap-react).
Many thanks
Catching up here… AFAICT, all the work related to this issue has been merged and long since published now. Closing. LMK if that’s not the case.