Next.js fix

Before and after

Broken — before
// app/components/ThemeToggle.tsx (Broken Next.js Client Component)
"use client";
import { useState } from "react";

export function ThemeToggle() {
  const [dark, setDark] = useState(false);
  return (
    <button type="button" onClick={() => setDark((d) => !d)}>
      <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
        <circle cx="12" cy="12" r="5" />
      </svg>
    </button>
  );
}
Fixed — after
// app/components/ThemeToggle.tsx (Fixed Next.js Client Component)
"use client";
import { useState } from "react";

export function ThemeToggle() {
  const [dark, setDark] = useState(false);
  return (
    <button
      type="button"
      aria-label={dark ? "Switch to light theme" : "Switch to dark theme"}
      aria-pressed={dark}
      onClick={() => setDark((d) => !d)}
    >
      <svg aria-hidden="true" focusable="false" width="20" height="20" viewBox="0 0 24 24" fill="currentColor">
        <circle cx="12" cy="12" r="5" />
      </svg>
    </button>
  );
}
Live preview

Fixed vs. broken example

Passes button-name
Accessibility tree: role="button", accessible name="Open site search" (decorative SVG hidden with aria-hidden="true")

Tab to each button and inspect the Accessibility pane in DevTools: the broken control exposes an empty Name while the fixed control exposes "Open site search".

Automated test

Playwright + axe-core check

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

test("Next.js layout header buttons pass axe-core button-name", async ({ page }) => {
  await page.goto("/docs/getting-started");
  const toggle = page.getByRole("button", { name: /switch to (dark|light) theme/i });
  await expect(toggle).toBeVisible();
  const results = await new AxeBuilder({ page })
    .withRules(["button-name"])
    .analyze();
  expect(results.violations).toEqual([]);
});

Template deduplication tip

Because app/layout.tsx wraps every nested route segment in Next.js, one unlabeled theme-toggle button in components/SiteHeader.tsx shows up on every crawled URL. A11yDrift collapses the entire route tree to that single header cluster.

Screen readers

What NVDA and VoiceOver announce

Blind screen-reader users and voice-dictation users (Dragon, Voice Control) encounter unlabeled icon buttons as generic 'button' controls with no clue whether activating them opens search, closes a modal, or deletes an item.

NVDA (Windows)

Broken

"button" (silence — no action name announced)


Fixed

"Open site search, button"

VoiceOver (macOS / iOS)

Broken

"button" (unlabeled interactive element)


Fixed

"Open site search, button"

Common questions

Should a Next.js theme toggle change its aria-label or use aria-pressed?
Use one consistent pattern—if you set aria-pressed={dark}, keep a stable label like 'Dark theme' so screen readers announce 'Dark theme, toggle button, pressed / not pressed'; or keep an action label ('Switch to dark theme') if toggling without pressed state.
Why does button-name fail during Next.js hydration if icons load lazily?
If a button's accessible name comes from a dynamically imported icon component or client-only tooltip library, initial SSR HTML renders an empty <button>. Always pass aria-label directly in the server-rendered JSX.
How do I test every Next.js App Router layout for button-name in CI?
Run A11yDrift's GitHub Actions PR gate across up to 20 representative preview URLs (home, docs, pricing, dynamic [slug] routes) on every Vercel or Cloudflare preview deploy.