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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/mantine-core/src/FocusTrap/FocusTrap.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,67 @@ describe('@mantine/core/FocusTrap', () => {
expect(document.body).toHaveFocus();
});

it('traps focus on shift + tab', async () => {
render(
<FocusTrap>
<div>
<input />
<button type="button">Button</button>
<input type="radio" />
</div>
</FocusTrap>
);

await wait(10);
expect(screen.getByRole('textbox')).toHaveFocus();

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",

expect(screen.getByRole('textbox')).toHaveFocus();

userEvent.tab({ shift: true });
expect(screen.getByRole('button')).toHaveFocus();
});

it('traps focus on shift + tab and handles Radio Groups', async () => {
render(
<FocusTrap>
<div>
<label htmlFor="1">
Option One
<input type="radio" name="group" id="1" />
</label>

<label htmlFor="2">
Option Two
<input type="radio" name="group" id="2" />
</label>

<label htmlFor="3">
Option Three
<input type="radio" name="group" id="3" />
</label>

<button type="button">Button</button>
</div>
</FocusTrap>
);

await wait(10);
expect(screen.getByLabelText('Option One')).toHaveFocus();

userEvent.tab();
expect(screen.getByRole('button')).toHaveFocus();

userEvent.tab({ shift: true });
expect(screen.getByLabelText('Option Three')).toHaveFocus();

userEvent.tab({ shift: true });
expect(screen.getByRole('button')).toHaveFocus();
});

it('manages aria-hidden attributes', () => {
const adjacentDiv = document.createElement('div');
adjacentDiv.setAttribute('data-testid', 'adjacent');
Expand Down
14 changes: 13 additions & 1 deletion src/mantine-hooks/src/use-focus-trap/scope-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ export function scopeTab(node: HTMLElement, event: KeyboardEvent) {
}
const finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
const root = node.getRootNode() as unknown as DocumentOrShadowRoot;
const leavingFinalTabbable = finalTabbable === root.activeElement || node === root.activeElement;
let leavingFinalTabbable = finalTabbable === root.activeElement || node === root.activeElement;

// Handle the case of the active element being in a RadioGroup with the finalTabbable element
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);
}
Comment on lines +14 to +23
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.


if (!leavingFinalTabbable) {
return;
Expand Down