React fix

Before and after

Broken — before
// Broken React input: uses <div> instead of <label htmlFor>
export function EmailField({ value, onChange }: { value: string; onChange: (v: string) => void }) {
  return (
    <div className="field">
      <div className="field-title">Work email</div>
      <input
        type="email"
        value={value}
        placeholder="alex@agency.com"
        onChange={(e) => onChange(e.target.value)}
      />
    </div>
  );
}
Fixed — after
// Fixed React TextField: SSR-safe useId() binds <label htmlFor> to <input id>
import { useId } from "react";

interface EmailFieldProps {
  value: string;
  onChange: (value: string) => void;
  error?: string;
}

export function EmailField({ value, onChange, error }: EmailFieldProps) {
  const inputId = useId();
  const errorId = `${inputId}-error`;

  return (
    <div className="field">
      <label htmlFor={inputId} className="field-label">
        Work email
      </label>
      <input
        id={inputId}
        name="email"
        type="email"
        autoComplete="email"
        value={value}
        aria-invalid={error ? true : undefined}
        aria-describedby={error ? errorId : undefined}
        onChange={(e) => onChange(e.target.value)}
      />
      {error && (
        <p id={errorId} role="alert">
          {error}
        </p>
      )}
    </div>
  );
}
Live preview

Fixed vs. broken example

Passes label
Accessibility tree: role="textbox", accessible name="Work email address" (programmatically bound via <label for="demo-work-email">)

Click directly on the "Work email address" label in the fixed preview: native browsers expand the hit target and immediately move focus into the input.

Automated test

Playwright + axe-core check

tests/a11y-label.spec.ts
import { test, expect } from "@playwright/test";
import AxeBuilder from "@axe-core/playwright";

test("React signup form inputs are bound to visible labels", async ({ page }) => {
  await page.goto("/signup");
  await page.getByLabel("Work email").fill("qa@example.com");
  const results = await new AxeBuilder({ page })
    .withRules(["label"])
    .analyze();
  expect(results.violations).toEqual([]);
});

Template deduplication tip

Fixing your shared <TextField /> component once eliminates unlabeled input failures across login, signup, settings, and checkout routes simultaneously—A11yDrift tracks the regression reduction cleanly across releases.

Screen readers

What NVDA and VoiceOver announce

Placeholder-only inputs lose their hint the moment a user starts typing, cannot be focused by clicking a visible label target, and leave screen-reader and cognitive-disability users guessing which data belongs in each field during checkout or signup.

NVDA (Windows)

Broken

"edit, has auto complete, blank" (no field purpose announced)


Fixed

"Work email address, edit, has auto complete, blank"

VoiceOver (macOS / iOS)

Broken

"edit text, Work email address" (inconsistent placeholder fallback that disappears once filled)


Fixed

"Work email address, edit text"

Common questions

Why use React useId() instead of Math.random() for label IDs?
Math.random() causes React hydration mismatches between server-rendered HTML and client renders. useId() produces deterministic, collision-free IDs across SSR and client hydration.
Does wrapping <input> directly inside <label> also pass axe-core label?
Yes, implicit wrapping (<label>Email <input /></label>) passes axe-core, though explicit htmlFor/id association offers wider compatibility with voice-dictation software such as Dragon NaturallySpeaking.
How should validation errors connect to a React input?
Give the error message a unique id and point the input's aria-describedby to that id alongside aria-invalid={true}, so screen readers read both the label and the error text upon focus.