Skip to content

Commit 46fd4a4

Browse files
soroush5flacoman91
andauthored
fix(Tabs): make roving tabindex follow the spec (selected tab = 0) (#642)
Hey! Noticed the Tabs component sets `tabindex="0"` on every tab, which breaks the roving-tabindex pattern — keyboard users have to Tab through every single tab instead of landing on the selected one and using arrow keys. This is the exact pattern the WAI-ARIA tabs spec calls out. ## What I changed - `tab.tsx` — only the selected tab gets `tabindex="0"`, the rest get `-1`, and focus moves with arrow-key navigation ## How I verified Added a test: render 3 tabs, assert only the selected one is tabbable, arrow-right moves selection and tabbability together — red before the fix, green after. Neighboring component tests still green. Relates to #643 --------- Co-authored-by: soroush5 <[email protected]> Co-authored-by: Richard Dinh <[email protected]>
1 parent 28c35b8 commit 46fd4a4

2 files changed

Lines changed: 153 additions & 4 deletions

File tree

‎src/components/tabs/tab.test.tsx‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
import '@testing-library/jest-dom';
2-
import { render, screen } from '@testing-library/react';
2+
import { fireEvent, render, screen } from '@testing-library/react';
3+
import { useState } from 'react';
34
import { Tab, TabList } from './tab';
45

6+
const TabsDemo = () => {
7+
const [activeTab, setActiveTab] = useState('one');
8+
const onClick = (event: React.MouseEvent<HTMLButtonElement>) => {
9+
setActiveTab(event.currentTarget.value);
10+
};
11+
return (
12+
<TabList>
13+
<Tab id='one' value='one' isActive={activeTab === 'one'} onClick={onClick}>
14+
One tab
15+
</Tab>
16+
<Tab id='two' value='two' isActive={activeTab === 'two'} onClick={onClick}>
17+
Second tab
18+
</Tab>
19+
<Tab
20+
id='three'
21+
value='three'
22+
isActive={activeTab === 'three'}
23+
onClick={onClick}
24+
>
25+
Third tab
26+
</Tab>
27+
</TabList>
28+
);
29+
};
30+
531
describe('<Tabs />', () => {
632
it('renders tabs', () => {
733
render(
@@ -27,4 +53,63 @@ describe('<Tabs />', () => {
2753

2854
expect(screen.getByRole('tablist')).toHaveClass('tablist--inverted');
2955
});
56+
57+
it('uses roving tabindex: only the active tab is in the tab order', () => {
58+
render(
59+
<TabList>
60+
<Tab id='one' isActive>
61+
One tab
62+
</Tab>
63+
<Tab id='two'>Second tab</Tab>
64+
</TabList>,
65+
);
66+
67+
// Per WAI-ARIA tabs pattern, the active tab keeps tabindex 0 so
68+
// keyboard users can reach it; inactive tabs are tabindex -1.
69+
expect(screen.getByRole('tab', { name: 'One tab' })).toHaveAttribute(
70+
'tabindex',
71+
'0',
72+
);
73+
expect(screen.getByRole('tab', { name: 'Second tab' })).toHaveAttribute(
74+
'tabindex',
75+
'-1',
76+
);
77+
});
78+
79+
it('moves selection and focus with arrow keys, Home and End', () => {
80+
render(<TabsDemo />);
81+
82+
const one = screen.getByRole('tab', { name: 'One tab' });
83+
const two = screen.getByRole('tab', { name: 'Second tab' });
84+
const three = screen.getByRole('tab', { name: 'Third tab' });
85+
86+
one.focus();
87+
fireEvent.keyDown(screen.getByRole('tablist'), { key: 'ArrowRight' });
88+
89+
expect(two).toHaveAttribute('aria-selected', 'true');
90+
expect(two).toHaveAttribute('tabindex', '0');
91+
expect(one).toHaveAttribute('tabindex', '-1');
92+
expect(two).toHaveFocus();
93+
94+
fireEvent.keyDown(screen.getByRole('tablist'), { key: 'ArrowRight' });
95+
expect(three).toHaveAttribute('aria-selected', 'true');
96+
expect(three).toHaveFocus();
97+
98+
// Wraps around the end.
99+
fireEvent.keyDown(screen.getByRole('tablist'), { key: 'ArrowRight' });
100+
expect(one).toHaveAttribute('aria-selected', 'true');
101+
expect(one).toHaveFocus();
102+
103+
fireEvent.keyDown(screen.getByRole('tablist'), { key: 'End' });
104+
expect(three).toHaveAttribute('aria-selected', 'true');
105+
expect(three).toHaveFocus();
106+
107+
fireEvent.keyDown(screen.getByRole('tablist'), { key: 'Home' });
108+
expect(one).toHaveAttribute('aria-selected', 'true');
109+
expect(one).toHaveFocus();
110+
111+
fireEvent.keyDown(screen.getByRole('tablist'), { key: 'ArrowLeft' });
112+
expect(three).toHaveAttribute('aria-selected', 'true');
113+
expect(three).toHaveFocus();
114+
});
30115
});

‎src/components/tabs/tab.tsx‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import classnames from 'classnames';
2-
import type { HTMLAttributes, ReactNode, MouseEvent } from 'react';
2+
import type {
3+
HTMLAttributes,
4+
KeyboardEvent as ReactKeyboardEvent,
5+
ReactNode,
6+
MouseEvent,
7+
} from 'react';
38
import type { JSXElement } from '../../types/jsx-element';
49
import { Button } from '../buttons/button';
510
import type { ButtonProperties } from '../buttons/button';
@@ -50,7 +55,7 @@ export const Tab = ({
5055
id={`tab-${id}`}
5156
aria-controls={`tabpanel-${id}`}
5257
aria-selected={isActive}
53-
tabIndex={isActive ? -1 : 0}
58+
tabIndex={isActive ? 0 : -1}
5459
{...properties}
5560
>
5661
{children}
@@ -72,14 +77,73 @@ export const TabList = ({
7277
className,
7378
children,
7479
isInverted = false,
80+
onKeyDown,
7581
...properties
7682
}: TabListProperties): JSXElement => {
7783
const cname = classnames('tablist', className, {
7884
'tablist--inverted': isInverted,
7985
});
8086

87+
/**
88+
* WAI-ARIA tabs keyboard pattern (automatic activation): arrows move
89+
* selection with wrap, Home/End jump to the ends. Clicking the target
90+
* tab reuses the consumer's own onClick, so isActive, aria-selected
91+
* and tabindex stay in sync without TabList owning selection state.
92+
*/
93+
const handleKeyDown = (event: ReactKeyboardEvent<HTMLDivElement>) => {
94+
onKeyDown?.(event);
95+
96+
if (event.defaultPrevented) {
97+
return;
98+
}
99+
100+
const tabs = [
101+
...event.currentTarget.querySelectorAll<HTMLElement>('[role="tab"]'),
102+
];
103+
104+
if (tabs.length === 0) {
105+
return;
106+
}
107+
108+
const currentIndex = tabs.indexOf(document.activeElement as HTMLElement);
109+
110+
let targetIndex;
111+
switch (event.key) {
112+
case 'ArrowRight': {
113+
targetIndex = (currentIndex + 1 + tabs.length) % tabs.length;
114+
break;
115+
}
116+
case 'ArrowLeft': {
117+
targetIndex = (currentIndex - 1 + tabs.length) % tabs.length;
118+
break;
119+
}
120+
case 'Home': {
121+
targetIndex = 0;
122+
break;
123+
}
124+
case 'End': {
125+
targetIndex = tabs.length - 1;
126+
break;
127+
}
128+
default: {
129+
return;
130+
}
131+
}
132+
133+
event.preventDefault();
134+
const target = tabs[targetIndex];
135+
target.focus();
136+
target.click();
137+
};
138+
81139
return (
82-
<div role='tablist' className={cname} {...properties}>
140+
<div
141+
role='tablist'
142+
className={cname}
143+
onKeyDown={handleKeyDown}
144+
tabIndex={-1}
145+
{...properties}
146+
>
83147
{children}
84148
</div>
85149
);

0 commit comments

Comments
 (0)