-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
FocusScope: If a focused element unmounts, focus moves to body, breaking focus containment #874
Comments
Thanks for reporting! Where would you expect focus to go in your case? To the next focusable element after the button that was deleted? What if there is no focusable element inside the scope anymore? Feels like it may be challenging to answer those questions in a general way... |
I think at a baseline, I would expect for the FocusScope to contain focus so long as it contains another focus-able element. The least surprising behavior here would probably be for the next element in the focus order to be focused. I think if there were no focus-able elements within the scope, then it probably makes sense for focus to break out of the scope in some way. Thanks so much for your work on this! Your components are life-savers for our team :) |
Is anybody started to work on this? I can try to handle this in 2-3, just want to know if anybody tried to implement this, so I can just continue not from 0, or any tips/comments would be useful too. Thanks! |
@AleshaOleg I don't believe anyone has picked this up yet, feel free to give it a shot! I think it would be a good idea to first come to an agreement for the expected behavior first. IMO I think shifting the focus to the next element in focus order makes sense if it exists, but the question of where the focus should go if there are no focusable elements within the scope still stands. Perhaps in that case it should break out of focus containment and focus the body? Or should it find the last focusable element prior to the one that unmounted? Any suggestions? |
a thought i had on this is that if focus moves forward, the user may not know what was next, so they may think they’ve been dumped somewhere random. would it make more sense for focus to go to the previous item? presumably where they came from? unless of course they were shift tabbing. I don't know if it makes sense to build up a stack of where the user has been in terms of focus, that could get hard to manage. |
The principal use case for which I'd like to see a solution is a dialog opened from a popover, like a menu, that closes when the dialog opens, where closing the FocusScope for the dialog expects to restore focus to an element that no longer exists. For this I think we need a stack of FocusScopes that can be updated when a scope is removed, so that closing the top scope always restores focus to an element that exists in the previous scope. A secondary use case would be for elements in the same scope where the item that has focus can be removed, losing focus to the document body, like a Tag list of keywords for filtering, or items in a collection that can be removed by clicking a button in the item. FocusScope may or may not need to account for this, because focus management when an item is removed might best be handled by the TagList or Collection, setting focus to the previous or next item if one exists. I'm less inclined to arbitrarily set focus to the next or previous tabbable element in the DOM, which may be a different, unrelated element type. |
Possibly resolved by #3323 |
I've confirmed, this has been fixed in recent releases. |
🐛 Bug Report
FocusScope exhibits confusing behavior when a focused element unmounts, potentially allowing focus to travel outside of a FocusScope even when
contain
is set to true.🤔 Expected Behavior
I would expect that when a focused element is unmounted, focus travels to a different element within the FocusScope, when
contain
istrue
.😯 Current Behavior
When a focus element within a FocusScope unmounts, focus travels to
<body>
rather than another element within the focus scope.🔦 Context
I am implementing a modal that renders a form with radio buttons on mount. Prior to the radio buttons being returned from the fetch, I show a loading indicator. I attempted to use the loading indicator as the focus target for the modal, assuming that when the loading indicator unmounts then the first form option would be rendered. Instead, focus traveled to
body
which is outside of my FocusScope.💻 Code Sample
Sandbox with issue reproduced: https://codesandbox.io/s/thirsty-mahavira-dof7z?file=/src/App.js
Code in case sandbox becomes unavailable:
🌍 Your Environment
🧢 Your Company/Team
ButcherBox/Engineering
The text was updated successfully, but these errors were encountered: