AccessibleTabs — Keyboard-Friendly React Tabs
A reusable React and TypeScript tabs component with keyboard navigation, ARIA accessibility and responsive layouts.
By Sayed Risat~3 min read
- accessibility
- react
- typescript
- ui
On this page
Live demo · accessible-tabs
Loading trusted preview…
tsx · 2 files
import { useId, useRef, useState, type KeyboardEvent, type ReactNode } from 'react';export type AccessibleTabItem = { value: string; label: string; content: ReactNode; disabled?: boolean;};export type AccessibleTabsProps = { items: AccessibleTabItem[]; defaultValue?: string; ariaLabel?: string; orientation?: 'horizontal' | 'vertical'; className?: string;};export function AccessibleTabs({ items, defaultValue, ariaLabel = 'Content sections', orientation = 'horizontal', className = '',}: AccessibleTabsProps) { const id = useId(); const enabledIndices = items .map((item, index) => (!item.disabled ? index : -1)) .filter((index) => index >= 0); const initialValue = items.find( (item) => item.value === defaultValue && !item.disabled )?.value ?? items[enabledIndices[0]]?.value ?? ''; const [activeValue, setActiveValue] = useState(initialValue); const tabRefs = useRef<Array<HTMLButtonElement | null>>([]); const activeItem = items.find( (item) => item.value === activeValue && !item.disabled ) ?? items[enabledIndices[0]]; function handleKeyDown( event: KeyboardEvent<HTMLButtonElement>, index: number ) { if (enabledIndices.length === 0) return; const position = enabledIndices.indexOf(index); let nextPosition: number | undefined; if (orientation === 'horizontal' && event.key === 'ArrowRight') { nextPosition = (position + 1) % enabledIndices.length; } else if (orientation === 'horizontal' && event.key === 'ArrowLeft') { nextPosition = (position - 1 + enabledIndices.length) % enabledIndices.length; } else if (orientation === 'vertical' && event.key === 'ArrowDown') { nextPosition = (position + 1) % enabledIndices.length; } else if (orientation === 'vertical' && event.key === 'ArrowUp') { nextPosition = (position - 1 + enabledIndices.length) % enabledIndices.length; } else if (event.key === 'Home') { nextPosition = 0; } else if (event.key === 'End') { nextPosition = enabledIndices.length - 1; } if (nextPosition === undefined) return; event.preventDefault(); const nextIndex = enabledIndices[nextPosition]; const nextItem = items[nextIndex]; if (!nextItem) return; setActiveValue(nextItem.value); tabRefs.current[nextIndex]?.focus(); } if (!activeItem) return null; return ( <div className={['accessible-tabs', className].filter(Boolean).join(' ')} data-orientation={orientation} > <div className='accessible-tabs__list' role='tablist' aria-label={ariaLabel} aria-orientation={orientation} > {items.map((item, index) => { const selected = item.value === activeItem.value; const tabId = `${id}-tab-${index}`; const panelId = `${id}-panel-${index}`; return ( <button key={item.value} ref={(element) => { tabRefs.current[index] = element; }} id={tabId} className='accessible-tabs__tab' type='button' role='tab' aria-selected={selected} aria-controls={panelId} tabIndex={selected ? 0 : -1} disabled={item.disabled} onClick={() => setActiveValue(item.value)} onKeyDown={(event) => handleKeyDown(event, index)} > {item.label} </button> ); })} </div> {items.map((item, index) => { const selected = item.value === activeItem.value; return ( <div key={item.value} id={`${id}-panel-${index}`} className='accessible-tabs__panel' role='tabpanel' aria-labelledby={`${id}-tab-${index}`} tabIndex={0} hidden={!selected} > {item.content} </div> ); })} </div> );}AccessibleTabs: Build Keyboard-Friendly Tabs in React
Tabs help organize related content without navigating away from the current page. A good tabs component should expose its structure to assistive technology and support keyboard interaction.
This component provides horizontal and vertical layouts, disabled tabs, arrow-key navigation and Home/End shortcuts.
Accessibility
The component uses the tablist, tab and tabpanel ARIA roles. Each tab is connected to its panel through aria-controls and aria-labelledby. The selected tab is exposed through aria-selected.
Keyboard navigation
- Horizontal: Arrow Left and Arrow Right.
- Vertical: Arrow Up and Arrow Down.
- Home: first enabled tab.
- End: last enabled tab.
- Enter and Space: native button activation.
Disabled tabs are skipped during navigation. Only the selected tab remains in the normal tab order.
Styling
The component uses isolated CSS classes and theme-friendly custom properties. You can customize the accent, foreground and border colors to match your design system.
Implementation note
The component JSON stores source files for documentation and Code View. Its live preview is rendered separately through your trusted React demo registry.
Conclusion
AccessibleTabs is useful for settings pages, dashboards, documentation and any interface that needs compact, keyboard-friendly content navigation.
AccessibleTabs
AccessibleTabs is a reusable React and TypeScript tabs component featuring keyboard navigation, ARIA accessibility, disabled tabs, and horizontal or vertical layouts. It supports arrow keys, Home and End shortcuts, and semantic tab-panel relationships.
Installation
- Copy AccessibleTabs.tsx into your components directory.
- Copy accessible-tabs.css into your styles directory.
- Import AccessibleTabs and its stylesheet into your React application.
- Pass an items array containing unique values, labels and ReactNode content.
- Optionally configure defaultValue, ariaLabel and orientation.
Requirements: React and TypeScript. No additional package is required.
Usage
Pass an array of tabs containing a unique value, label and React content. Use defaultValue to select the initial tab. Use orientation to choose horizontal or vertical navigation.
Props
| Name | Type | Default | Required |
|---|