141Build a fully accessible component library including modal, dropdown, combobox, and keyboard interactions
Problem
Build three React components that follow the WAI-ARIA Authoring Practices, so they work with a keyboard and screen readers, not just a mouse.
<Dialog open onClose title>: rendered into document.body; role="dialog", aria-modal="true", labelled by its title. On open, focus the first focusable element inside; Tab and Shift+Tab cycle inside; Escape calls onClose; on close, focus returns to the element that opened it.
<MenuButton label items>, where items are { label, onSelect, disabled }:
- a
<button aria-haspopup="menu" aria-expanded aria-controls>; the menu isrole="menu"withrole="menuitem"items (aria-disabledfor disabled ones); Enter,Space,ArrowDownor a click open the menu and focus the first enabled item;ArrowUpopens it on the last;- inside the menu:
ArrowDown/ArrowUpmove (wrapping, skipping disabled),Home/End, typing a letter jumps to the next item starting with it,Enter/Spaceactivates (callsonSelect, closes, focuses the button),Escapecloses and focuses the button,Tabcloses; - a pointer down outside closes it.
<Combobox label options onChange>: an <input role="combobox" aria-autocomplete="list" aria-expanded aria-controls aria-activedescendant> filtering options (strings) case-insensitively into a role="listbox" of role="option" items with aria-selected; ArrowDown/ArrowUp move the active option (opening the list if needed), Enter picks it (sets the input, calls onChange, closes), Escape closes; options can also be picked with the mouse.
Try it yourself
Write your solution, run it, then submit to check it against the tests.
Step-by-step
The Authoring Practices define the roles, states and keys users of screen readers already know (dialog, menu button, combobox). Matching them exactly is what makes components "accessible", far more than adding aria-labels.
Unlock the full solution
What's inside
- Step-by-step
- Approach
- Solution
- Complexity
- Edge cases
- Follow-up questions
- Tests
Includes 2 code listings, 5 edge cases, 4 follow-up questions.
One-time payment by UPI, card or net banking. Launch price.