Build an accessible React component with TypeScript
Generate a typed, accessible React component with tests, following good composition and state practices. Designed for ChatGPT, Claude and Microsoft Copilot. It includes 3 customizable variables and you can copy it for free in one click.
Intermediate · Code · Pending editorial review
Prompt
You are a senior frontend developer specialized in React, TypeScript and accessibility (WCAG 2.2 AA).
Build the {{component}} component with these requirements:
- Functionality: {{functionality}}
- Styling with: {{styling}}
- It must handle these states: loading, empty, error and success.
Technical requirements:
1. Props typed with an exported interface and sensible defaults.
2. Accessibility: ARIA roles and attributes only when needed, visible focus, full keyboard navigation and screen-reader text.
3. No runtime dependencies other than React and the chosen styling system; tests may use only React Testing Library and user-event.
4. A usage example of the component.
5. Tests with React Testing Library covering keyboard interaction and the states.
At the end, explain which accessibility decisions you made and why.How to use it
Mention the styling library your project already uses so the output fits. Always check the component with a screen reader or the browser accessibility panel.
Usage example
Component: date picker with minimum and maximum range.
Sample output
Component (excerpt)
export interface CityComboboxProps {
options: string[];
label?: string;
status?: 'loading' | 'error' | 'success';
onSelect: (city: string) => void;
}
export function CityCombobox({ options, label = 'City', status = 'success', onSelect }: CityComboboxProps) {
const [query, setQuery] = useState('');
const [active, setActive] = useState(-1);
const listId = useId();
const results = options.filter((c) => c.toLowerCase().includes(query.toLowerCase()));
function onKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
if (e.key === 'ArrowDown') setActive((i) => Math.min(i + 1, results.length - 1));
if (e.key === 'ArrowUp') setActive((i) => Math.max(i - 1, 0));
if (e.key === 'Enter' && results[active]) onSelect(results[active]);
if (e.key === 'Escape') setActive(-1);
}
return (
<div className="relative">
<label htmlFor={`${listId}-input`} className="block text-sm font-medium">{label}</label>
<input
id={`${listId}-input`} role="combobox" aria-expanded={results.length > 0} aria-controls={listId}
aria-activedescendant={active >= 0 ? `${listId}-${active}` : undefined}
className="w-full rounded border px-3 py-2 focus-visible:ring-2 focus-visible:ring-blue-600"
value={query} onChange={(e) => setQuery(e.target.value)} onKeyDown={onKeyDown}
/>
<ul id={listId} role="listbox" className="absolute z-10 w-full rounded border bg-white">
{results.map((city, i) => (
<li key={city} id={`${listId}-${i}`} role="option" aria-selected={i === active}
className={i === active ? 'bg-blue-100 px-3 py-1' : 'px-3 py-1'} onMouseDown={() => onSelect(city)}>
{city}
</li>
))}
</ul>
<p role="status" className="sr-only">{status === 'loading' ? 'Loading…' : `${results.length} results`}</p>
</div>
);
}
Accessibility decisions
- Focus stays on the input and
aria-activedescendantpoints to the active option, as the ARIA combobox pattern recommends. - The result count is announced in a
role="status"region, so screen readers report it without moving focus…
Key facts
- Category
- Programming & development · React
- Output type
- Code
- Level
- Intermediate
- Designed for
- ChatGPT, Claude and Microsoft Copilot
- Verified in
- Pending editorial verification
- Variables
- Component, Functionality and Styling system
- Language
- English
- Price
- Free, no sign-up
- Updated
- Nov 7, 2026
Frequently asked questions
Does this make my component WCAG compliant?
Not on its own. It gives you a solid starting point, but you still need to test with the keyboard, a screen reader such as NVDA or VoiceOver, and an automated checker such as axe.
Does it work with Next.js?
Yes. The component is plain React; with the Next.js App Router, add 'use client' at the top of the file if it uses state or event handlers.
What is this prompt for?
Generate a typed, accessible React component with tests, following good composition and state practices.
Which AI tools does it work with?
It is designed for ChatGPT, Claude and Microsoft Copilot. We have not verified it in these tools yet, so results may vary between them.
How do I customize the prompt?
Click “Customize” and fill in the variables (Component, Functionality and Styling system). The text updates instantly and you can then copy it in one click. Values are stored only in your browser.
Do I need experience to use it?
It is intermediate: it helps to know the topic so you can provide good context and review the result critically.
Is this prompt free?
Yes. Every HomeCasa Prompts prompt is free, with no sign-up or subscription. You can also use them in professional projects.
Can I trust the AI output?
Treat it as a high-quality draft: always check data, figures and important claims before publishing or making decisions.