Frontend School
DashboardProblemsLearnPracticeMock InterviewReportsStatsBuy Credits
DashboardProblemsLearnPracticeMock Interview
HTML & Accessibility (a11y)
  1. 1.HTML Fundamentals: Structure & Core Elements
  2. 2.Lists, Tables & Basic Forms
  3. 3.Semantic HTML5 & Document Outline
  4. 4.Advanced Forms & Input Validation
  5. 5.Multimedia & Embedded Content Accessibility
  6. 6.Introduction to ARIA: Roles & Landmarks
  7. 7.ARIA States & Properties for Dynamic Content
  8. 8.Keyboard Accessibility & Focus Management
  9. 9.Advanced Accessibility Patterns & Custom Controls
  10. 10.Accessibility Testing, WCAG & Interview Prep
HTML & Accessibility (a11y)
09

Web Fundamentals · Lesson 9 of 10 · 4 min

Advanced Accessibility Patterns & Custom Controls

This is how an interviewer tests if you can build a complex UI. Anyone can style a div, but can you make it work for a screen reader and a keyboard?

When to build a custom control (and when not to)

You build custom controls when native HTML elements can't meet your design requirements. A standard <select> is hard to style, and it can't support a search input or complex option layouts. This is when you reach for divs and spans to build a custom combobox, multi-select, or tree view.

The trade-off is that you become responsible for everything the browser used to give you for free: semantics, keyboard interaction, and state management.

The trap: Overusing ARIA. The first rule of ARIA is to avoid ARIA if a native element will do. If <button> or <input type="range"> works for your use case, use it and style it. Don't rebuild a button from a div just because you can. Explaining this restraint shows seniority.

Announcing role, state, and properties

When you build a component from generic elements like divs, it has no semantic meaning. You must provide that meaning to assistive technologies using ARIA.

Think of it as a contract. You tell the browser what your component is (role), what condition it's in (state), and how it relates to other elements (property).

  • Role: What is this thing? A div with role="switch" tells a screen reader it's a toggle switch, not just a generic container.
  • State: What is its current condition? aria-checked="true" says the switch is on. aria-expanded="false" says a dropdown is closed.
  • Property: What are its relationships? aria-labelledby="label-id" links a control to its text label.

Here is the bare minimum for an accessible custom toggle switch.

<label id="notify-label">Enable Notifications</label>
<div id="myToggle"
     role="switch"
     tabindex="0"
     aria-checked="false"
     aria-labelledby="notify-label">
</div>
const toggle = document.getElementById('myToggle');

function toggleSwitch(event) {
  const isChecked = toggle.getAttribute('aria-checked') === 'true';
  toggle.setAttribute('aria-checked', String(!isChecked));
}

toggle.addEventListener('click', toggleSwitch);
toggle.addEventListener('keydown', (event) => {
  // The Space key should also activate the switch.
  if (event.key === ' ') {
    event.preventDefault();
    toggleSwitch(event);
  }
});

This div is now in the tab order (tabindex="0"), identifies itself as a switch (role="switch"), announces its state (aria-checked), and has a proper label (aria-labelledby).

Managing keyboard interaction and virtual focus

For simple controls, tabindex="0" is enough. For composite widgets like menus, grids, or listboxes, managing focus gets more complex.

The trap: Making every single item in a long list focusable. If you have a list of 100 options and give each <li> a tabindex="0", you've just added 100 stops to the user's tab order. This is a terrible user experience.

The correct pattern is to manage a single focus point. For many widgets, this is done with aria-activedescendant.

Here’s how it works:

  1. The main container (e.g., the <ul> with role="listbox") gets a tabindex="-1" so it can be focused programmatically. The trigger element (like a button) remains in the tab order.
  2. When the list is opened, you keep DOM focus on the trigger button or move it to the listbox container.
  3. As the user presses arrow keys, you don't move DOM focus. Instead, your JavaScript updates the aria-activedescendant attribute on the focused container to point to the id of the currently active child element.

The screen reader sees this attribute change and announces the new active element as if it were focused. This keeps the DOM clean, improves performance on large lists, and provides the expected keyboard model.

In an interview, explaining why you'd use aria-activedescendant over moving DOM focus is a significant signal of seniority. Talk about performance with large lists and keeping the tab order clean. It shows you think about the user experience beyond just the visual layer.

Announcing asynchronous updates with aria-live

Your application state changes. A form submission succeeds, search results appear, an item is added to a cart. A sighted user sees this. A screen reader user needs an announcement.

This is the job of ARIA live regions. You place an aria-live attribute on a container element, and assistive technologies will monitor it for DOM changes and announce them.

There are two politeness levels:

  • aria-live="polite": Announces the change when the user is idle. This is what you want 99% of the time. It's for status updates, notifications, and other non-critical information.
  • aria-live="assertive": Interrupts the user immediately to make an announcement. Use this only for critical, time-sensitive information like "Error: Your session has expired." Overusing this is disruptive and frustrating.

A common pattern is to use a dedicated, visually hidden container for announcements. When you need to make an announcement, you inject the text into this container, and it's read out.

Shortcuts exist for common use cases. An element with role="status" has an implicit aria-live="polite". An element with role="alert" has an implicit aria-live="assertive".

Try it

Implement a basic tab list using divs and buttons. Follow the keyboard interaction patterns in the ARIA Authoring Practices Guide (APG) for tabs. Ensure left/right arrow keys navigate between tab buttons, and Tab moves focus from the active tab into the active tab panel.

Next: Learn how to verify your work in "Accessibility Testing, WCAG & Interview Prep".

Lock it in — apply this lesson under interview conditions.

Try an AI mock interview
Previous

Keyboard Accessibility & Focus Management

Next

Accessibility Testing, WCAG & Interview Prep