Skip to content

Fix focus scope with changing children #2791

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

Merged
merged 5 commits into from
Feb 1, 2022

Conversation

snowystinger
Copy link
Member

@snowystinger snowystinger commented Jan 31, 2022

Closes #2415

Keep focus scope up to date with changing children and auto focus children.
The two cases observed are:

  1. Open a dialog, change the children in the dialog, hit esc, focus is lost.
    This was fixed by using scopeRef.current as pointed out in the issue description because the scope was out of date.
  2. Open a dialog where a child has autofocus, hit esc, focus is lost.
    This was fixed by changing when nodeToRestore is set so that it runs before autoFocus takes place.

We are preferring this PR over #2416 right now because it's difficult to review due to the refactor. Not that the refactor is bad, but by breaking this up, we can make sure we know what the fix is, and we can refactor it separately such that no changes to behavior occur.

Thank you @kherock for your hard work on this!

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Test steps (case 1):

  1. Go to https://reactspectrum.blob.core.windows.net/reactspectrum/f976e1d87682ba3a61027506d360b91d8976e267/storybook/index.html?path=/story/focusscope--keyboard-navigation-no-contain
  2. Tab to "Open dialog" and hit enter. A new FocusScope row should appear and focus should be moved to the first input element.
  3. Tab to "replace focusscope children" and hit Enter. The children in the 2nd row should be replaced by 3 inputs and focus should move to the middle input element
  4. Hit Escape key. The 2nd "dialog" row should close and focus should be moved to the trigger child

Case 2 is covered by the unit test

🧢 Your Project:

RSP

Keep focus scope up to date with changing children and auto focus children
@adobe-bot
Copy link

Build successful! 🎉

Copy link
Member

@LFDanLu LFDanLu left a comment

Choose a reason for hiding this comment

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

LGTM

{showNew && (
<div role="dialog" onKeyDown={onKeyDown}>
<input />
<input autoFocus />
Copy link
Collaborator

Choose a reason for hiding this comment

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

autoFocus on this input means that focus will be maintained within the FocusScope when the children change, however, without the autoFocus prop, focus will be lost to the document.body. #2763 has been opened to account for this and keep focus within the FocusScope when contain is true.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, #2763 is the reason we aren't addressing this issue here

Copy link
Member

@ktabors ktabors left a comment

Choose a reason for hiding this comment

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

This looks good, I just have the one question in tests.


rerender(<Test />);

expect(document.activeElement).toBe(outside);
Copy link
Member

Choose a reason for hiding this comment

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

Do we care about proving the focus scope doesn't goto .outside when it isn't focused before we rerender it twice?

@adobe-bot
Copy link

Build successful! 🎉

@adobe-bot
Copy link

Build successful! 🎉

@adobe-bot
Copy link

Build successful! 🎉

@snowystinger snowystinger merged commit bef1b1d into main Feb 1, 2022
@snowystinger snowystinger deleted the simplify-restore-focus-changes branch February 1, 2022 19:54
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.

FocusScope restoreFocus doesn't work when its immediate children change
5 participants