-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[FocusTrap] Correctly focus on the correct element on initial focus #37672
base: master
Are you sure you want to change the base?
[FocusTrap] Correctly focus on the correct element on initial focus #37672
Conversation
Netlify deploy previewhttps://deploy-preview-37672--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
tabIndex={-1}
Test Casesare not written. Tests are numbered and bullet points note "expected input" -> "expected output" FocusTrap
Dialog
|
Please help me find a better name for |
How this affects other ComponentsIn general The question is should I fix the tests so if you use the modal you are expected to do that manually or should I allow FocusTrap to gracefully handle it instead? Probably the former.
|
For whoever review this, I appeased the CI gods as much as I can, but I can't seem to generate the docs for some reason. Also it's not possible for me spend more time on this, if someone can take over to write the new tests based on the test cases I've written and generate the docs I would appreciate that. |
@Greg-NetDuma Thanks for working on this 💜 I've pushed a commit to generate the docs Would you mind giving me a TLDR of the state of the tests before I dig into this? 🙏 |
@mj12albert All existing tests are fixed and passing. (Most of the changes are to make sure all test None of the new tests in #37672 (comment) are written, these are all the cases that I can think of for the changes in this PR. They need to be implemented. I tried to write the description for them as simple as possible. |
Any updates on this PR? |
This is the current state of this PR. I'm using this fix in production for the past 6 months and it also passed an accessibility audit as well. |
@mj12albert Since you assigned this to yourself, are you planning to work on it by adding the new tests and reviewing the implementation? |
I added all the tests needed and implemented the fix across all your duplicated codebase. I still can't generate the docs properly because Will I need to wait another year? It looks like the interval for the accessibility PRs. |
@michaldudak @DiegoAndai Can you take a look? |
I'll add 2 more tests to FocusTrap today involving expected behaviour when tabbing inside.
As far as I remember that's the expected behaviour but I'll re-read the guidelines and check how it behaves now. Edit: it seems there are some more test failures because of the changes (or maybe not?) I'll have a look at them too. |
I'm seeing some typecheck failures as well. I'll try to figure out what's going on. |
While I was trying to write the test I realized that I'm trying to test is just native browser behaviour. So I only implemented/amended tests for things Initially I thought my implementation logic was wrong but I can see now I implemented it just like https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex#sect2 says. I'll look at the failure in |
I looked at it ( |
Should this pointed against |
I believe so. cc @DiegoAndai |
Is master going to be v6 or is it still going into v5? If I have to do a backport after it's merged into master I rather open a new PR for it so I don't have to do the work again (for v5) |
|
Yes. |
I did except the test cases, they fail without those changes. PS. still can't generate the documentation |
Generated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also fixes #42989 bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could just reverse the how roles and aria-props applied from DialogPaper to Dialog instead of all of this. We still can't implement something that controls the initial focus, but it is much simpler.
This would be a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initial review
packages/mui-material/src/Unstable_TrapFocus/FocusTrap.test.tsx
Outdated
Show resolved
Hide resolved
Co-authored-by: Zeeshan Tamboli <[email protected]> Signed-off-by: Attila Greguss <[email protected]>
Co-authored-by: Zeeshan Tamboli <[email protected]> Signed-off-by: Attila Greguss <[email protected]>
…om/Greg-NetDuma/material-ui into fix-dialog-incorrect-initial-focus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Greg-NetDuma I pushed some changes here: commit 1 and commit 2. The PR looks good to me, but I'd like others to review it — cc @DiegoAndai @aarongarciah. I think this should be released in a minor Material UI version once merged.
with a fallback to the root component.
The Problem:
Dialog has a DOM layout something like this:
Modal applies tabindex to dialog for accessibility reasons (see link for possible explanation) and
FocusTrap
automatically focuses on it's direct child (Dialog
). But Dialog has an incorrectrole
set inDialog.js
for this specific case.According to the guidelines the modal should focus on either:
tabindex={-1}
The Solution
Make sure
FocusTrap
immediately focuses on the first tabbable element including elements withtabIndex={-1}
set. Regular tabbing should ignore them. I also removedModal
injectingtabindex
to the root child forcefully (the only place we seem to useFocusTrap
)This means
Modal
doesn't need to forcefully applytabindex="-1"
to it's childFocusTrap
will still applytabindex="-1"
to its direct child if no better candidate was found (and complain, currently)tabIndex={-1}
to the element that actually has"role="dialog"
inDialog
will be announced correctly by screenreaders.Dialog
where users can disable addingtabIndex={-1}
to the content they can properly control the focus order including the first focus, where they can just choose to settabIndex={-1}
on another element inside the dialog or just letFocusTrap
use the implicit/explicit tabindex order.Alternate Solution
We could just reverse the how roles and aria-props applied from
DialogPaper
toDialog
instead of all of this. We still can't implement something that controls the initial focus, but it is much simpler.This also fixes #42989