SkillPilotAI

Accessibility

Accessibility (a11y) means the app works for keyboard users and screen reader users too, not just with a mouse. This note covers the accessibility tree, semantic HTML, labels and alt text, ARIA, keyboard rules and focus management.

55.1 Problem: a mouse-only button

  • A div styled as a button works with a mouse click.
  • Tab never focuses it, and the screen reader reads it as plain text, not a button.
  • The image has no alt text, so a screen reader user has no idea which product it is.
React (JSX)
function ProductCard({ onAdd }) {  return (    <div className="card">      <img src="/shoe.jpg" />      <div className="btn" onClick={onAdd}>        Add to cart      </div>    </div>  );}
Output
(shoe image)Add to cartKeyboard: Tab never focuses itScreen reader: image, Add to cart (not a button)

55.2 How it works: the accessibility tree

  • The browser builds an accessibility tree for every page: each element has a role, a name and a state.
  • The screen reader reads this tree to the user.
  • A button has role button, gets Tab focus and is clicked with Enter or Space by default. A div has no role at all.
  • Not only blind users: many people use only a keyboard (for example with an injured hand).
From HTML to what the user hears
  1. 1.HTML element: <button>Add to cart</button>

  2. 2.Browser builds the accessibility tree: role button, name "Add to cart"

  3. 3.Screen reader reads the tree

  4. 4.User hears: "Add to cart, button"

  5. 5.A <div> gives no role, so the user hears only text

55.3 Fix: semantic HTML

  • Semantic HTML = use the element whose meaning matches the job.
  • The outer div becomes an article; the image gets alt={name}, so the screen reader says Nike Air.
  • A real button replaces the div and gives Tab, Enter and Space for free.
React (JSX)
function ProductCard({ name, image, onAdd }) {  return (    <article className="card">      <img src={image} alt={name} />      <h3>{name}</h3>      <button type="button" onClick={onAdd}>        Add to cart      </button>    </article>  );}
Output
(Nike Air image)# Nike Air[button] Add to cartScreen reader: Add to cart, button

55.4 Forms: label and error

  • Most common problem: an input with only a placeholder and no label.
  • useId() creates a unique id; label htmlFor={id} connects the label to the input.
  • aria-invalid says the value is invalid; aria-describedby connects the error message to the input.
  • The screen reader user hears the error together with the field name.
React (JSX)
import { useId } from "react";function EmailField({ error }) {  const id = useId();  const errId = id + "-error";  return (    <>      <label htmlFor={id}>Email</label>      <input id={id} type="email"        aria-invalid={Boolean(error)}        aria-describedby={errId} />      {error && <p id={errId}>{error}</p>}    </>  );}
Output
Email: rahul@Enter a valid emailScreen reader: Email, invalid entry, Enter a valid email

55.5 ARIA: name, state and announce

  • ARIA = extra attributes that add missing information to the accessibility tree.
  • aria-label="Close" names an icon-only button; aria-hidden hides the decorative icon.
  • aria-expanded tells whether the Filters panel is open or closed.
  • role="status" makes the screen reader announce the text whenever it changes (3 in cart).
React (JSX)
function Toolbar({ open, toggle, count }) {  return (    <>      <button aria-label="Close">        <span aria-hidden="true">✕</span>      </button>      <button aria-expanded={open}        onClick={toggle}>Filters      </button>      <p role="status">{count} in cart</p>    </>  );}
Output
[button] ✕[button] Filters3 in cartScreen reader: 3 in cart
AttributeAddsExample
aria-labelName for an element without visible textIcon button: Close
aria-hidden="true"Hides decoration from screen readersThe ✕ icon
aria-expandedOpen/closed stateFilters toggle
aria-invalidInvalid value stateEmail input with an error
aria-describedbyLinks extra descriptionError message under a field
role="status"Live announcement on text change3 in cart

55.6 Keyboard rules

  • Tab moves to the next element, Shift+Tab to the previous; Tab order should match the visual order.
  • Enter and Space press buttons; Escape closes a modal or a dropdown.
  • tabIndex 0 adds an element to the Tab order, -1 is only for focusing from code, and never use a value above 0.

55.7 Focus management: a modal

  • Focus management = moving keyboard focus to the right place when the UI changes.
  • A native <dialog> with showModal() moves focus to the first focusable element (the search input) and makes the rest of the page inert.
  • Escape or Close closes it and the browser moves focus back to the Search button.
  • A custom div modal has to do all of this by hand.
React (JSX)
import { useRef } from "react";function SearchDialog() {  const ref = useRef(null);  const open = () => ref.current.showModal();  const close = () => ref.current.close();  return (    <>      <button onClick={open}>Search</button>      <dialog ref={ref} aria-label="Search">        <input type="search" />        <button onClick={close}>Close</button>      </dialog>    </>  );}
Output
[button] Search Dialog: SearchFocus: search input[button] Close [button] SearchFocus: Search button
Focus flow of a modal
  1. 1.User presses Search (focus on the button)

  2. 2.showModal(): focus moves to the search input

  3. 3.Rest of the page is inert, Tab stays inside

  4. 4.Escape or Close: dialog closes

  5. 5.Focus returns to the Search button

55.8 Checkpoint: will Enter save?

  • The div has tabIndex 0, so Tab focuses it. Does Enter run onSave? No.
  • tabIndex only makes an element focusable; it does not give it a keyboard click.
  • The browser gives Enter/Space clicks only to a real button. On a div you would need onKeyDown and a role yourself: simplest fix is a button.
React (JSX)
function SaveButton({ onSave }) {  return (    <div tabIndex={0} onClick={onSave}>      Save    </div>  );}
Output
> Tab: focus on Save> Enter: onSave did not runScreen: fix = <button>

55.9 Common mistakes

  • Hiding the focus ring with outline: none: keyboard users cannot see where focus is.
  • Use :focus-visible: the ring shows only for keyboard focus, not on mouse click, and can use your brand colour.
  • Thinking more ARIA = more accessible. Rule 1 of ARIA: if a native element can do the job, do not use ARIA. Bad ARIA is worse than none.
  • role="button" on a div makes the screen reader say button, but keyboard support is still missing.
CSS
/* Wrong: keyboard users can't see focus */button:focus {  outline: none;}
CSS
/* Right: ring only on keyboard focus */button:focus-visible {  outline: 3px solid #2563eb;  outline-offset: 2px;}

55.10 Best practices

  • Semantic HTML; every input has a label, every meaningful image has alt text.
  • Use the whole app once with only the keyboard.
  • Keep text contrast at least 4.5:1.
  • Tools: eslint-plugin-jsx-a11y, Lighthouse, axe; in tests find elements with getByRole (what getByRole cannot find, a screen reader cannot find either).

55.11 Interview tips

  • "How do you make a React app accessible?" Semantic HTML first: native elements give role, name and keyboard support for free.
  • ARIA only where a native element cannot do the job.
  • UI like a modal needs focus management: focus in, trap, and back to the trigger on close.
  • Verify with keyboard testing, jsx-a11y lint and getByRole tests.
  • Never remove the focus ring; use :focus-visible.