Build a keyboard-usable control
Start with native semantics, then manage focus and errors.
Prefer the platform
A button already supports keyboard activation, focus, and disabled state. A clickable div does not. If the visual design can use a real button or link, use it. For a custom composite control, define the expected arrow-key and focus behavior before writing event handlers.
Keep focus predictable
When a list opens, users need to know where focus goes; when it closes, focus should return to the trigger. Expose the state with appropriate labels and expanded state. Do not hide a focused element without moving focus first. Test with keyboard only and a screen reader when available.
<button type="button" aria-expanded="false" aria-controls="options">Choose a role</button>
Connect errors to fields
An inline error should identify the field and be associated through aria-describedby. Set an invalid state when appropriate, and move focus to a useful place after submission. Color alone cannot communicate an error. A test should check both the visible message and its programmatic relationship to the input.
Check your understanding
Why is a click handler on a div usually insufficient for a button?
Show explanation
A div lacks built-in keyboard activation, focus, role, and disabled behavior. Prefer a native button.
You can read for free. Create an account to save your progress.
← Previous lesson: TypeScript types stop at runtime Next lesson: Handle stale async results →Put this lesson into practice
A practice activity at this level in the same module.
Try: Fix a loose-equality bugFurther reading
Primary references for this module. Check the current version when behavior or framework guidance may have changed.