Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct focus order for non-autofocusable modals, fixes #340 #350

Merged
merged 1 commit into from
Dec 15, 2024

Conversation

theKashey
Copy link
Owner

There is a race conditions non reproducible via unit test where focus event can happen when the old and the new focus points at the body.

The focus restoration logic will try to "reset" itself, but will do it as many times, as many times body can be focused.

  • applies only for autoFocus:false cases

if (
// it was removed
!document.body.contains(lastActiveFocus)
// or not focusable (this is expensive operation)!
|| isNotFocusable(lastActiveFocus)
) {
lastActiveFocus = null;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

causes a "loop"

@@ -104,15 +104,13 @@ const activateTrap = () => {
const workingNode = observed || (lastPortaledElement && lastPortaledElement.portaledElement);

// check if lastActiveFocus is still reachable
if (focusOnBody() && lastActiveFocus) {
if (focusOnBody() && lastActiveFocus && lastActiveFocus !== document.body) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

focus on the body and the last element is body?
Can happen.

Unfortunately lastActiveFocus is used to hold the state of the Trap, and it is not just "last active element within trap", it's just "last active element recorded".
For autoFocus:false cases it will be body

@theKashey theKashey merged commit a5449ad into master Dec 15, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant