Skip to content

[Tabs] Fix modifier keys + Left/Right Arrow key from being consumed by tab navigation #45345

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

Merged
merged 10 commits into from
Mar 19, 2025
Merged
5 changes: 5 additions & 0 deletions packages/mui-material/src/Tabs/Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,11 @@ const Tabs = React.forwardRef(function Tabs(inProps, ref) {
});

const handleKeyDown = (event) => {
// Check if a modifier key (Alt, Shift, Ctrl, Meta) is pressed
if (event.altKey || event.shiftKey || event.ctrlKey || event.metaKey) {
return;
}

const list = tabListRef.current;
const currentFocus = ownerDocument(list).activeElement;
// Keyboard navigation assumes that [role="tab"] are siblings
Expand Down
24 changes: 24 additions & 0 deletions packages/mui-material/src/Tabs/Tabs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,30 @@ describe('<Tabs />', () => {
'-1',
]);
});

['Alt', 'Shift', 'Ctrl', 'Meta'].forEach((modifierKey) => {
it(`does not navigate when ${modifierKey} is pressed with ArrowLeft`, async () => {
const { getAllByRole } = render(
<Tabs value={1}>
<Tab />
<Tab />
</Tabs>,
);

const [firstTab, secondTab] = getAllByRole('tab');
await act(async () => {
secondTab.focus();
});

fireEvent.keyDown(secondTab, {
key: 'ArrowLeft',
[`${modifierKey.toLowerCase()}Key`]: true,
});

expect(secondTab).toHaveFocus();
expect(firstTab).not.toHaveFocus();
});
});
});

describe('dynamic tabs', () => {
Expand Down