Here's a fully accessible (a11y-compatible) React TypeScript button group component. This component allows users to select one option from a group of buttons and ensures proper keyboard navigation, screen reader support, and semantic structure.
✅ Features:
Uses role="group" for accessibility
Each button has correct ARIA attributes (aria-pressed, tabindex)
Supports keyboard navigation (left/right arrows)
Fully typed with TypeScript
🧩 Button Group Component (React + TypeScript)
tsx Copy code
import React, { useState, KeyboardEvent } from 'react';
interface ButtonGroupOption {
value: string | number;
label: string;
}
interface ButtonGroupProps {
options: ButtonGroupOption[];
selectedValue: string | number | null;
onChange: (value: string | number) => void;
ariaLabel?: string;
}
const ButtonGroup: React.FC<ButtonGroupProps> = ({
options,
selectedValue,
onChange,
ariaLabel = 'Select an option',
}) => {
const [activeIndex, setActiveIndex] = useState<number>(
options.findIndex((opt) => opt.value === selectedValue)
);
const handleKeyDown = (
e: KeyboardEvent<HTMLButtonElement>,
index: number
) => {
if (e.key === 'ArrowLeft') {
const prevIndex = index <= 0 ? options.length - 1 : index - 1;
setActiveIndex(prevIndex);
onChange(options[prevIndex].value);
e.preventDefault();
} else if (e.key === 'ArrowRight') {
const nextIndex = index >= options.length - 1 ? 0 : index + 1;
setActiveIndex(nextIndex);
onChange(options[nextIndex].value);
e.preventDefault();
}
};
return (
<div
role="group"
aria-label={ariaLabel}
className="button-group"
style={{
display: 'inline-flex',
borderRadius: 8,
overflow: 'hidden',
border: '1px solid #ccc',
}}
>
{options.map((option, index) => (
<button
key={option.value}
type="button"
role="radio"
aria-checked={selectedValue === option.value}
aria-pressed={selectedValue === option.value}
tabIndex={selectedValue === option.value ? 0 : -1}
onKeyDown={(e) => handleKeyDown(e, index)}
onClick={() => {
onChange(option.value);
setActiveIndex(index);
}}
style={{
padding: '8px 16px',
backgroundColor:
selectedValue === option.value ? '#007bff' : '#f8f9fa',
color: selectedValue === option.value ? '#fff' : '#333',
border: 'none',
cursor: 'pointer',
outline: 'none',
fontWeight: selectedValue === option.value ? 'bold' : 'normal',
}}
>
{option.label}
</button>
))}
</div>
);
};
export default ButtonGroup;
✅ Usage Example
tsx Copy code
import React, { useState } from 'react';
import ButtonGroup from './ButtonGroup';
const App = () => {
const [selected, setSelected] = useState<string | number>('week');
const options = [
{ value: 'day', label: 'Day' },
{ value: 'week', label: 'Week' },
{ value: 'month', label: 'Month' },
];
return (
<div style={{ padding: '2rem' }}>
<h2>Select View</h2>
<ButtonGroup
options={options}
selectedValue={selected}
onChange={setSelected}
ariaLabel="View selector"
/>
<p style={{ marginTop: '1rem' }}>Current selection: {selected}</p>
</div>
);
};
export default App;
📌 Accessibility Notes
Feature Why It's Important role="group"Groups related controls together for screen readers role="radio"Indicates each button is part of a single-choice set aria-checked / aria-pressedCommunicates the state to assistive technologies Arrow key navigation Ensures keyboard-only users can navigate the group tabIndexOnly one button is in tab order at a time Semantic structure & labels Helps screen readers understand purpose and context
Let me know if you'd like a vertical version or multi-select variant!