Building Keyboard-Friendly UI Components

Tutorial 4 of 5

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.