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.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Focuses 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:

  1. keydown: This event is fired when a key is pressed.
  2. keypress: This event is fired when a key is pressed and released.
  3. 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

  1. Exercise 1: Create a checkbox that can be checked and unchecked using the 'Enter' key.
  2. Exercise 2: Create a dropdown menu that can be navigated using the 'Arrow' keys.
  3. Exercise 3: Create a modal that can be opened and closed using the 'Enter' and 'Escape' keys respectively.

Solutions

  1. 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;
  }
});
  1. 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.

  1. 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.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Timestamp Converter

Convert timestamps to human-readable dates.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI 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 article

AI 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 article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help