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.

Click in FocusTrap causes first item to focus, causing the view to scroll back to the top

See original GitHub issue

Hi, 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:closed
  • Created 4 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
TobyGWilliamscommented, Feb 3, 2020

Hi there,

We would also be grateful to see this released (and pushed out to focus-trap-react).

Many thanks

0reactions
stefcameroncommented, Apr 8, 2021

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.

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 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 >

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