Frontend System Design · Lesson 72 of 86 · 3 min
Accessibility in Component Libraries
This isn't about ticking boxes; it's about demonstrating you build robust, professional-grade systems. Answering this well separates senior from mid-level engineers.
Start with Semantic HTML
Your most powerful accessibility tool is using the right HTML element for the job. Native elements like <button>, <input>, <nav>, and <h2> have accessibility built-in for free. A <button> is already focusable, keyboard-operable, and announced as a "button" by screen readers.
A div is none of those things.
The trap: Rebuilding native functionality with divs and spans. If you find yourself adding role="button", tabindex="0", and a keyDown handler to a div, you've almost certainly made a mistake. The first rule of ARIA is: don't use ARIA if a native HTML element already does what you need.
Start with semantics. Only move on to custom solutions when there is no native element for the UI pattern you're building.
Use ARIA for Custom Components
Modern UIs have complex components like tabs, custom selects, and toggle switches that don't have a one-to-one HTML equivalent. This is where you use ARIA (Accessible Rich Internet Applications) to bridge the gap. ARIA provides extra semantics for assistive technologies.
ARIA attributes fall into three types:
- Roles: Define the component's type, like
role="switch"orrole="tablist". - States: Describe the component's current condition, like
aria-checked="true"oraria-expanded="false". - Properties: Provide additional information, like
aria-label="Close"to give a button an accessible name.
Here’s a custom toggle switch. It's a common pattern that requires ARIA to be understood by screen readers. Notice how role, aria-checked, tabIndex, and keyboard handlers work together.
import React, { useState, useId } from 'react';
type ToggleSwitchProps = {
label: string;
};
const ToggleSwitch: React.FC<ToggleSwitchProps> = ({ label }) => {
const [isChecked, setIsChecked] = useState(false);
const id = useId();
const handleToggle = () => {
setIsChecked(prevState => !prevState);
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === ' ' || e.key === 'Enter') {
e.preventDefault();
handleToggle();
}
};
return (
<>
<span id={id}>{label}</span>
<div
role="switch"
aria-checked={isChecked}
aria-labelledby={id}
tabIndex={0}
onClick={handleToggle}
onKeyDown={handleKeyDown}
style={{ display: 'inline-block', cursor: 'pointer', userSelect: 'none' }}
>
{/* Visual representation of the switch would go here */}
</div>
</>
);
};
This is the correct use of ARIA: enhancing a non-native element to behave like a standard UI control that doesn't exist in HTML.
Master Keyboard Navigation and Focus
Many users navigate exclusively with a keyboard. If your component isn't keyboard-operable, it's inaccessible. There are three critical aspects to get right.
First, logical tab order and visible focus. Interactive elements must be reachable in a predictable sequence using the Tab key. The currently focused element must have a highly visible style (e.g., an outline). Never use CSS outline: none; without providing a different, equally clear focus indicator.
Second, focus traps. For any component that overlays the page, like a modal dialog, you must "trap" the user's focus inside it. Tabbing should cycle through the modal's interactive elements and not escape to the page underneath. Pressing Escape should close the modal.
Third, focus restoration. When the user closes the modal, focus must return to the element that opened it. Losing the user's place on the page is a jarring experience.
In an interview, expect deep probes on focus management. Describing how you'd implement a focus trap for a modal (finding all focusable elements, handling
TabandShift+Tabat the boundaries, and restoring focus on close) is a classic test of frontend depth.
Systematize Accessibility, Don't Bolt It On
In a system design interview, your goal is to show you can make the entire system accessible by default, not just one component. You do this by building accessibility into the library's foundation and processes.
In the design system: Define accessible color palettes with sufficient contrast ratios as design tokens. Make sure typography scales are legible.
In the development process: Integrate automated accessibility checks into your CI/CD pipeline using tools like axe-core. This catches low-hanging fruit like missing alt text or insufficient color contrast on every pull request.
In the documentation: For every component, document its expected keyboard interactions and any required ARIA attributes. This teaches consumers of your library how to use the components accessibly.
This lifecycle approach makes accessibility a shared responsibility and a core quality metric, not an afterthought.
Try it
Find a "custom checkbox" component on CodePen or a similar site that is styled with divs and spans. Spend 15 minutes refactoring it. Make it focusable, checkable with the Space key, and ensure it uses the correct role and aria-checked attributes so its state is announced by a screen reader.
Next: Component Library Docs, Testing and Versioning
Lock it in — apply this lesson under interview conditions.