Web Accessibility / Keyboard Navigation and Focus Management
Building Keyboard-Friendly UI Components
This tutorial will show you how to build keyboard-friendly UI components. You'll learn to design components that respond to keyboard events and maintain accessible states.
Section overview
5 resourcesFocuses on ensuring proper keyboard navigation and managing focus for accessible web interfaces.
Building Keyboard-Friendly UI Components
1. Introduction
This tutorial aims to guide you through the process of building UI components that are keyboard-friendly. By the end, you will have a better understanding of how to design and implement components that respond smoothly to keyboard events, and maintain accessible states.
What You Will Learn
- Basics of keyboard events
- Building accessible UI components
- Responding to keyboard events
Prerequisites
Basic knowledge of HTML, CSS, JavaScript and accessibility guidelines is required to follow this tutorial.
2. Step-by-Step Guide
Before diving into the code, let's discuss some key principles to keep in mind:
- Focus: Ensure your component can receive focus. This can be accomplished by using the 'tabindex' attribute.
- Keyboard Events: Understand keypress, keydown and keyup events to trigger actions.
- Accessible States: Maintain accessible states using ARIA roles and properties.
Key Events
In JavaScript, there are three key events:
keydown: This event is fired when a key is pressed.keypress: This event is fired when a key is pressed and released.keyup: This event is fired when a key is released.
3. Code Examples
Let's see a practical example of a keyboard-friendly button:
<button id="myButton" tabindex="0" role="button" aria-pressed="false">Click Me!</button>
let button = document.getElementById('myButton');
button.addEventListener('keydown', function(event) {
// 'Enter' or 'Space' is pressed
if(event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
// Toggle 'aria-pressed'
this.setAttribute('aria-pressed', this.getAttribute('aria-pressed') === 'true' ? 'false' : 'true');
}
});
In the above example, we have a button that responds to 'Enter' or 'Space' keypress. The 'aria-pressed' attribute toggles between true and false, representing the pressed state of the button.
4. Summary
In this tutorial, we've learned about keyboard events, how to build accessible UI components, and how to respond to keyboard events. For further learning, explore other ARIA roles and properties and try to build more complex components like modals and dropdowns.
5. Practice Exercises
- Exercise 1: Create a checkbox that can be checked and unchecked using the 'Enter' key.
- Exercise 2: Create a dropdown menu that can be navigated using the 'Arrow' keys.
- Exercise 3: Create a modal that can be opened and closed using the 'Enter' and 'Escape' keys respectively.
Solutions
- Solution 1
<input type="checkbox" id="myCheckbox" tabindex="0">
let checkbox = document.getElementById('myCheckbox');
checkbox.addEventListener('keydown', function(event) {
if(event.key === 'Enter') {
event.preventDefault();
// Toggle checked state
this.checked = !this.checked;
}
});
- Solution 2
This exercise involves more complex coding and understanding of keyboard events. You need to handle 'ArrowDown', 'ArrowUp', and potentially 'Enter' and 'Escape' keys.
- Solution 3
This exercise also requires a more advanced understanding of focus management and event handling. You will need to create a modal, manage its 'open' state, and handle the 'Enter' and 'Escape' keys to open and close the modal.
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article