Skip to content
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
31 changes: 31 additions & 0 deletions src/components/ui/Tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ describe("Tabs — count badge", () => {
});
});

describe("Tabs — aria-controls only on the active tab (#710)", () => {
it("sets aria-controls on the active tab", () => {
render(<Tabs tabs={sampleTabs} active="active" onChange={() => {}} />);
expect(screen.getByRole("tab", { name: "Active" })).toHaveAttribute(
"aria-controls",
"tabpanel-active",
);
});

it("omits aria-controls on inactive tabs, since their tabpanel isn't in the DOM", () => {
render(<Tabs tabs={sampleTabs} active="active" onChange={() => {}} />);
expect(screen.getByRole("tab", { name: "Completed" })).not.toHaveAttribute(
"aria-controls",
);
expect(screen.getByRole("tab", { name: "Archived" })).not.toHaveAttribute(
"aria-controls",
);
});

it("moves aria-controls to whichever tab becomes active", () => {
render(<Tabs tabs={sampleTabs} active="completed" onChange={() => {}} />);
expect(screen.getByRole("tab", { name: "Completed" })).toHaveAttribute(
"aria-controls",
"tabpanel-completed",
);
expect(screen.getByRole("tab", { name: "Active" })).not.toHaveAttribute(
"aria-controls",
);
});
});

describe("Tabs — keyboard navigation (#24)", () => {
const threeTabs = [
{ key: "first", label: "First" },
Expand Down
8 changes: 7 additions & 1 deletion src/components/ui/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ export function Tabs<T extends string>({
role="tab"
id={`tab-${tab.key}`}
aria-selected={isActive}
aria-controls={`tabpanel-${tab.key}`}
// Only the active tab's panel actually exists in the DOM (a
// consumer like ContributorDashboardClient renders just the one
// tabpanel matching the current tab). aria-controls on an
// inactive tab pointed at a missing id -- an ARIA reference
// error flagged by axe and confusing for assistive tech
// following the relationship (#710).
aria-controls={isActive ? `tabpanel-${tab.key}` : undefined}
tabIndex={isActive ? 0 : -1}
onClick={() => onChange(tab.key)}
onKeyDown={(e) => handleKeyDown(e, tab.key)}
Expand Down