Fix button-name in Next.js
Next.js App Router sites frequently render icon-only theme toggles, sidebar collapse triggers, and command-palette buttons inside shared app/layout.tsx headers. Giving these client buttons dynamic aria-label values (such as 'Switch to dark theme' / 'Switch to light theme') satisfies WCAG 4.1.2 and communicates current state clearly.
Before and after
// 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>
);
}// 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>
);
}Fixed vs. broken example
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".
Playwright + axe-core check
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.
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)
"button" (silence — no action name announced)
"Open site search, button"
VoiceOver (macOS / iOS)
"button" (unlabeled interactive element)
"Open site search, button"
Common questions
Should a Next.js theme toggle change its aria-label or use aria-pressed?
Why does button-name fail during Next.js hydration if icons load lazily?
How do I test every Next.js App Router layout for button-name in CI?
Test a URL for button-name
Enter any public URL for a free WCAG 2.2 AA check—no account or credit card required.
Shipping Next.js weekly? Block new button-name regressions in PRs with our baseline-diff GitHub Action PR gate (≤20 URLs/PR).