-
Notifications
You must be signed in to change notification settings - Fork 42
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 Modal
focus jumping from child to itself
#2443
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Modal
/Dialog
focus jumping from child to dialogModal
focus jumping from child to dialog
Modal
focus jumping from child to dialogModal
focus jumping from child to itself
mayank99
reviewed
Feb 20, 2025
mayank99
reviewed
Feb 20, 2025
1 task
mayank99
reviewed
Feb 20, 2025
mayank99
approved these changes
Feb 20, 2025
Merged
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
A user reported that since
3.17.0
, when typing in a controlled input inside aModal
, the focus jumps from the input to therole="dialog"
whenever a character is typed. On investigating it seems to be a regression in3.17.0
because of #2391.The issue was that
onEnter
was being called each time the ref function was being called. Thus,onEnter
was calling.focus()
on the dialog multiple times.Looks like the ref function was being called multiple times because of the
<FocusTrap>
. Since focus trap is enabled by default in Modal but not in Dialog, this issue was there in the Modal but not in the Dialog.The exact issue is the
cloneElementWithRef
part:iTwinUI/packages/itwinui-react/src/utils/components/FocusTrap.tsx
Line 52 in f744dc5
Memoizing children too didn't work. So I removed the cloning by adding the ref to the first focus trap div and then accessing the children element with
firstFocusTrapRef.current?.nextElementSibling
. (4dde95f).Testing
Confimed in a playground similar to the user's to confirm that the focus jump does not happen in
Dialog
andModal
.Code
Also added small e2e tests for Modal and Dialog to confirm this.
Docs
Added a changeset.
After PR TODOs
cloneElementWithRef
causes re-renders and any problems associated with it (#2443 (comment))