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

[@mantine/hooks] use-focus-trap: shift + tab bug #4679

Closed

Conversation

Devon-Dickson
Copy link
Contributor

When using shift + tab to go back into a Radio Group, the last option will be selected, which doesn't line up with finalTabbable, and breaks out of the focus trap.

To fix, we can check to see if the active element is a radio button, find its radio group, and then check to see if the finalTabbable is in the same group. If so, set leavingFinalTabbable to true.

Addresses #4678

When using shift + tab to go back into a Radio Group, the last option
will be selected, which doesn't line up with finalTabbable, and breaks
out of the focus trap.

To fix, we can check to see if the active element is a radio button,
find it's radio group, and then check to see if the finalTabbable is in
the same group. If so, set leavingFinalTabbable to true.

Addresses mantinedev#4678
userEvent.tab();
expect(screen.getByRole('button')).toHaveFocus();

userEvent.tab({ shift: true });
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This doesn't appear to be working as expected, though it does pass when I run as part of a different codebase. We use the following versions of testing library. Was there a breaking change?

"@testing-library/dom": "^8.19.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^12.1.5",
"@testing-library/user-event": "^13.5.0",

Comment on lines +14 to +23
if (root.activeElement instanceof HTMLInputElement && root.activeElement.type === 'radio') {
const activeRadioGroup = tabbable.filter(
(element) =>
element instanceof HTMLInputElement &&
root.activeElement instanceof HTMLInputElement &&
element.type === 'radio' &&
element.name === root.activeElement.name
);
leavingFinalTabbable = activeRadioGroup.includes(finalTabbable);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This, obviously, is only a solution for Radio Groups, but I suspect there's similar behavior for other elements.

Another idea I had was to get the next target, and check if it's in tabbable at all, but I wasn't sure if there was an API exposed to do that easily, or if we'd need to do a lot of work to calculate it on our own.

@rtivital
Copy link
Member

@Devon-Dickson are you planning to finish working on this PR?

@rtivital rtivital changed the base branch from dev to master September 24, 2023 09:41
@Devon-Dickson
Copy link
Contributor Author

@Devon-Dickson are you planning to finish working on this PR?

I believe this is an accessibility bug, and I'd like to see the fix merged. However I'm a bit busy at the moment and don't have the bandwidth to debug the RTL issue (userEvent.tab({ shift: true }) doesn't seem to work).

I may be able to revisit in a few weeks, unless you're familiar with the issue and can recommend an immediate solution?

@rtivital
Copy link
Member

I believe it should be resolved by #4856

@Devon-Dickson
Copy link
Contributor Author

I believe it should be resolved by #4856

Looks like another route to the same solution 👍.

No tests yet though, which is the blocker on this PR.

@Devon-Dickson
Copy link
Contributor Author

I see #4856 has been merged, but is still missing tests.

If I get the time in the next couple of weeks, I can look at this again and get some tests added and passing. In the meantime, we can close this PR.

@rtivital
Copy link
Member

rtivital commented Oct 3, 2023

Thanks!

@Devon-Dickson
Copy link
Contributor Author

Issue with the tests ended up being a small thing, but comes with a performance cost
#4969

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.

2 participants