Role assignment, keyboard interaction patterns

Role assignment, Keyboard interaction patterns #

Role Assignment and Keyboard Interaction Patterns for Accessible Components #

Role assignment and keyboard interaction patterns are essential aspects of accessible design. By using ARIA roles and defining keyboard interaction patterns for custom components, you can make dynamic and interactive content more accessible to screen reader users, keyboard-only users, and users relying on assistive technology.

1. Role Assignment #

ARIA roles help define the purpose and functionality of elements on a page, especially when creating custom components or non-standard elements. Assigning the correct role improves screen reader interpretation and accessibility for users who rely on assistive technologies.

Common ARIA Roles #

  • role="button": Used to define an element that acts as a button. It should be accompanied by keyboard interaction (like Enter or Space to activate).
  • role="dialog": Defines a modal dialog. Focus should be moved to the dialog when it opens, and keyboard focus should be trapped within it.
  • role="menu" and role="menuitem": Used for navigation menus with sub-items. Keyboard navigation patterns for menus typically include arrow keys for navigation.
  • role="tablist", role="tab", role="tabpanel": Used to create tabbed interfaces, where each tab controls the visibility of its associated panel.
  • role="alert": Announces critical messages immediately. Often paired with aria-live="assertive" to alert users of urgent updates.

Example: Role Assignment in a Tab Component #

In the following example, role="tablist" is assigned to the container holding the tabs, role="tab" is assigned to each tab, and role="tabpanel" to each associated content panel.

<div role="tablist" aria-label="Sample Tabs">
    <button role="tab" aria-selected="true" aria-controls="panel1" id="tab1">Tab 1</button>
    <button role="tab" aria-selected="false" aria-controls="panel2" id="tab2">Tab 2</button>
</div>
<div id="panel1" role="tabpanel" aria-labelledby="tab1">
    <p>Content for Tab 1</p>
</div>
<div id="panel2" role="tabpanel" aria-labelledby="tab2" hidden>
    <p>Content for Tab 2</p>
</div>

2. Keyboard Interaction Patterns #

Keyboard interaction patterns define how users can navigate and interact with components using only the keyboard. This is essential for users who rely on keyboard navigation, including those with mobility impairments.

Common Keyboard Patterns #

  1. Buttons (role="button")
    • Activate with: Enter or Space
    • Example: <div role="button" tabindex="0">Click Me</div>
  2. Links (<a>)
    • Activate with: Enter
    • Note: Do not use role="link" on non-anchors if possible; use <a href="#"> for links.
  3. Tabs (role="tab")
    • Navigation: Use ArrowRight and ArrowLeft to move between tabs.
    • Activate: Enter or Space to switch tabs and update aria-selected state.
  4. Accordions (role="button" or custom roles)
    • Expand/Collapse: Use Enter or Space to expand or collapse sections.
    • Keyboard Focus: Moves to the first item within the section when expanded.
  5. Menus (role="menu", role="menuitem")
    • Navigation: ArrowDown and ArrowUp to move within the menu, ArrowRight and ArrowLeft for submenus.
    • Activate: Enter or Space to activate a menu item.
  6. Dialog/Modal (role="dialog")
    • Open: Move focus to the dialog upon opening.
    • Trap Focus: Keep focus within the dialog; users can exit with Escape.

Keyboard Interaction for a Tab Component : In this example, we implement keyboard interactions for a tab component. Users can navigate between tabs using arrow keys and activate tabs with Enter or Space.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Accessible Tabs with Keyboard Interaction</title>
    <style>
        .tab-content {
            display: none;
        }
        .tab-content[aria-hidden="false"] {
            display: block;
        }
    </style>
</head>
<body>
<div role="tablist" aria-label="Sample Tabs">
    <button role="tab" aria-selected="true" aria-controls="panel1" id="tab1" tabindex="0">Tab 1</button>
    <button role="tab" aria-selected="false" aria-controls="panel2" id="tab2" tabindex="-1">Tab 2</button>
    <button role="tab" aria-selected="false" aria-controls="panel3" id="tab3" tabindex="-1">Tab 3</button>
</div>
<div id="panel1" role="tabpanel" aria-labelledby="tab1" aria-hidden="false">
    <p>Content for Tab 1</p>
</div>
<div id="panel2" role="tabpanel" aria-labelledby="tab2" aria-hidden="true">
    <p>Content for Tab 2</p>
</div>
<div id="panel3" role="tabpanel" aria-labelledby="tab3" aria-hidden="true">
    <p>Content for Tab 3</p>
</div>
<script>
    const tabs = document.querySelectorAll('[role="tab"]');
    const panels = document.querySelectorAll('[role="tabpanel"]');
    tabs.forEach((tab, index) => {
        tab.addEventListener('keydown', (e) => {
            let newIndex;
            if (e.key === 'ArrowRight') {
                newIndex = (index + 1) % tabs.length; // Move right
            } else if (e.key === 'ArrowLeft') {
                newIndex = (index - 1 + tabs.length) % tabs.length; // Move left
            } else if (e.key === 'Enter' || e.key === ' ') {
                activateTab(tab); // Activate tab
            }
            if (newIndex !== undefined) {
                tabs[newIndex].focus();
            }
        });
        tab.addEventListener('click', () => activateTab(tab));
    });
    function activateTab(selectedTab) {
        tabs.forEach(tab => {
            tab.setAttribute('aria-selected', tab === selectedTab ? 'true' : 'false');
            tab.setAttribute('tabindex', tab === selectedTab ? '0' : '-1');
        });
        panels.forEach(panel => {
            panel.setAttribute('aria-hidden', panel.id !== selectedTab.getAttribute('aria-controls'));
        });
    }
</script>
</body>
</html>

Demonstrating bad practices in role assignment and keyboard interaction patterns. Each example highlights common accessibility issues that can confuse users or make components difficult to use with a keyboard or screen reader.

Assigning roles that don’t match the element’s function can mislead screen readers and confuse users. For example, using role="button" on a <div> without adding appropriate keyboard interactions.

<!-- Bad Practice: Using role="button" without keyboard interaction -->
<div role="button" onclick="alert('Clicked!')">
    Click Me
</div>

When using custom elements like <div> or <span> for interactive components, omitting tabindex="0" makes them inaccessible to keyboard users.

<!-- Bad Practice: Interactive div without tabindex -->
<div role="button" onclick="alert('Clicked!')">Click Me</div>

Opening a modal without moving focus to it or trapping focus within it can disorient users and cause them to accidentally interact with content outside the modal.

<!-- Bad Practice: Modal without focus management -->
<button onclick="document.getElementById('modal').style.display = 'block'">Open Modal</button>
<div id="modal" style="display: none;">
    <p>This is a modal dialog.</p>
    <button onclick="document.getElementById('modal').style.display = 'none'">Close</button>
</div>

Using role="button" instead of role="menuitem" for menu items in a menu component can confuse screen readers and users.

<!-- Bad Practice: Menu items using role="button" -->
<div role="menu">
    <div role="button">Menu Item 1</div>
    <div role="button">Menu Item 2</div>
</div>

Without aria-expanded, users may not know whether an accordion section is open or closed.

<!-- Bad Practice: Accordion without aria-expanded -->
<button onclick="document.getElementById('section1').classList.toggle('hidden')">
    Toggle Section
</button>
<div id="section1" class="hidden">
    <p>Accordion content here.</p>
</div>

Screen reader: NVDA
Video:

What are your feelings

Updated on March 5, 2025