Skip to content

[checkbox] Fix stale and duplicated control ids - #5457

Merged
atomiks merged 11 commits into
mui:masterfrom
atomiks:claude/festive-ramanujan-ce1d31
Aug 12, 2026
Merged

atomiks merged 11 commits into
mui:masterfrom
atomiks:claude/festive-ramanujan-ce1d31

Conversation

@atomiks

@atomiks atomiks commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Split out of #5448, which is being broken into smaller PRs.

Closes #2691

Checkbox.Root read the labelable scope's selected control id back out and rendered it as its own id. Removing the id prop was therefore a no-op, a keyed replacement inherited the id of the instance it replaced, and checkboxes sharing a scope with their CheckboxGroup all rendered that scope's single id. Each checkbox now resolves its own id through useLabelableId, the same way Field.Control and Switch.Root do.

The other half is server rendering. Field.Label renders htmlFor from the scope's controlId, which is only the scope's own generated id until a layout effect registers a real one, and layout effects don't run on the server. So in server markup the element the label targets has to carry the scope's generated id. That is why an explicit id now lands at hydration, and why a checkbox can no longer take a second id derived from its group.

Changes

  • Checkbox.Root no longer registers its id by hand. It uses useLabelableId, which drops a stale id when the prop is removed and re-picks one for a replacement instance.
  • Checkboxes sharing a labelable scope with their CheckboxGroup render their own ids. A Field.Item opens a scope the checkbox does own, so per-item labels keep working.
  • Field.Label omits htmlFor when its control is a CheckboxGroup. The group is named through aria-labelledby, and for previously pointed at one arbitrary aria-hidden input inside it.
  • A parent checkbox builds aria-controls from ids its children report, rather than deriving ${groupId}-${value} for every entry in allValues. The reference now reaches the exposed role="checkbox" element instead of the aria-hidden input behind it, survives a custom id on a child ([checkbox][CheckboxGroup] Checkbox.Root id not working in "parent checkbox" use case #2691), and no longer names an entry that has no child rendered.
  • useLabelableId releases its registration in the layout phase rather than a passive effect. A replacement control's layout effect ran first and still saw the outgoing control's registration, so the release had to wait for a StrictMode double-invoke to take effect. Without it, a keyed replacement kept the id of the control it replaced, and a Field.Root that swapped a CheckboxGroup for another control was left with no htmlFor at all.

Notes

An explicit id now reaches the DOM at hydration rather than during SSR, which is a deliberate trade. Server markup carries the scope's generated id on both the label and the control, so clicking the label works before hydration. Field.Control, NumberField.Root, Switch.Root and Radio.Root already behave this way, so this makes Checkbox.Root consistent with them.

aria-controls is absent from server markup, because a parent checkbox cannot control a child that has not mounted. It appears once the children register. Previously it was always present and always pointed at ids that either did not exist yet or belonged to hidden inputs.

One known gap: Field.Label still emits its htmlFor in server markup for a Field.Root wrapping a CheckboxGroup, and drops it during hydration. The suppression runs in a layout effect, and the label renders before the group, so the group cannot report it earlier. Base left the same attribute dangling whenever allValues was set.

Two or more controls in one Field.Root still share that scope's single control id, so their explicit id props collapse onto whichever registered first. That is pre-existing behaviour of useLabelableId (a Checkbox and a Switch with distinct ids already collapse on master), and Field.Item is the supported way to give each control its own scope. This PR brings Checkbox.Root onto that shared path rather than changing it.

RadioGroup has the same duplicate-id problem: two radios in one Field.Root share a single input id. It needs the same treatment and is left for a follow-up.

@atomiks atomiks added type: bug It doesn't behave as expected. component: checkbox Changes related to the checkbox component. component: checkbox group Changes related to the checkbox group component. component: field Changes related to the field component. labels Aug 10, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@5457
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@5457
    

commit: 2cbc6e7

@code-infra-dashboard

code-infra-dashboard Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+323B(+0.07%) 🔺+77B(+0.05%)

Details of bundle changes

Performance

Total duration: 944.69 ms -0.62 ms(-0.1%) | Renders: 76 (+0) | Paint: 1,453.57 ms -7.46 ms(-0.5%)

No significant changes — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Aug 10, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 2cbc6e7
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6a7bf19774dc030008a9ee7e
😎 Deploy Preview https://deploy-preview-5457--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks
atomiks force-pushed the claude/festive-ramanujan-ce1d31 branch from 8d2df1f to c304c94 Compare August 10, 2026 14:08
@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Aug 11, 2026
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Aug 11, 2026
@atomiks
atomiks marked this pull request as ready for review August 12, 2026 04:07
@atomiks
atomiks enabled auto-merge (squash) August 12, 2026 04:14
@atomiks
atomiks merged commit a5b699c into mui:master Aug 12, 2026
6 checks passed
AlpenWarden added a commit to AlpenWarden/deckwarden that referenced this pull request Oct 24, 2026
…our own change, `@types/node` 20 → 22 to match the Node 22 runtime with Dependabot told to leave its majors to the runtime, and `dotenv` replaced by Node 22's `process.loadEnvFile` behind one `loadEnv()` across its 29 entry points (LATER row 293, the owner's call of 2026-10-23); `pnpm audit` 7 → 1, the `zod` high closed on the runtime root; `/sets`' "Main sets only" box leaves its ids to the Field again because Base UI 1.8 changed how a Checkbox registers one (row 266 fired)

The bumps (package.json floors as #10 wrote them, two taken one release past it):
`@base-ui/react` 1.7.0 → 1.9.0, `better-auth` 1.7.10 → 1.7.12 (#10 had 1.7.11; 1.7.12, published
2026-10-20, hardens ID-token and OAuth-proxy checks this app's Discord/Google redirect flow
already satisfies), `drizzle-orm` 0.45.2 → 0.45.4, `lucide-react` 1.33.0 → 1.61.0 (#10 had
1.59.0), `react`/`react-dom` 19.2.8 → 19.3.1, `tailwind-merge` 3.6.0 → 3.7.0, `zod` 4.4.3 → 4.6.7,
`@testing-library/dom` 10.4.1 → 10.4.2, `@testing-library/react` 16.3.2 → 16.3.3, `@types/react`
19.2.18 → 19.3.0 and `@types/react-dom` 19.2.4 → 19.3.0 (ranges stay `^19`), `@vitejs/plugin-react`
6.1.0 → 6.1.2, `drizzle-kit` 0.31.10 → 0.31.11, `jsdom` 30.0.1 → 30.1.3, `prettier` 3.9.6 → 3.9.10
(nothing reformats), `shadcn` 4.22.0 → 4.22.1, `tsx` 4.23.12 → 4.23.15. Their own ranges moved
`better-call` 1.4.2 → 1.4.4, `@base-ui/utils` 0.3.2 → 0.5.0, `scheduler` 0.27 → 0.28,
`use-sync-external-store` 1.6 → 1.7 and jsdom's tree (`whatwg-url` 17.3.0, `tr46` 7, `data-urls` 8,
`html-encoding-sniffer` 7, the `@csstools` set); `jose` and `symbol-tree` left the lockfile.
`drizzle-kit` keeps its own `tsx` 4.23.12 beside the root's 4.23.15 (no `pnpm dedupe`: it would also
re-key the whole eslint peer set).

Audit (LATER row 269, re-run by its last trigger): 7 at the pre-flight → 1. `zod` 4.4.3
GHSA-mrhx-c7xc-m3w5 (high, runtime root) closed by the group's 4.6.7, `shadcn`'s copy deduplicated
onto it; `body-parser` 2.3.0 → 2.4.0 (GHSA-27r3-jxmh-p828, high, under `shadcn`'s MCP server) and
`fast-uri` 3.1.8 → 3.1.9 (four advisories, under `shadcn` and Sentry's never-wired webpack plugin)
by deleting their lockfile entries, no `pnpm.overrides`. `braces` left the audit because GitHub
withdrew GHSA-vfj7-8cjw-p6xm on 2026-10-23; the one accepted line left is `esbuild` 0.18.20 under
`drizzle-kit`'s loader (alert #1, dismissed). `pnpm advisories:watch` on the new tree: [email protected]
clean of 72, [email protected] clean of 37 but GHSA-fmh4-wcc4-5jm3, still accepted (row 271).

`@types/node` `^20` → `^22` (22.20.5, the newest 22.x). `.github/dependabot.yml` now ignores its
`version-update:semver-major`, and `src/lib/github-workflows.test.ts` pins the ignore and that the
range's major is ci.yml's and the nightly's `node-version` — a runtime move changes all three
together.

`dotenv` → `src/lib/load-env.ts`: `loadEnv()` runs `process.loadEnvFile` over `.env.local`, then
`.env`, each only if present — dotenv's `config({ path: [".env.local", ".env"], quiet: true })`
precedence exactly (the environment wins over both files, `.env.local` over `.env`, a missing file
is skipped, so the nightly runs on its secrets alone). It is a call on the line dotenv's call held in
all 29 files (17 scripts, 9 ingest scripts, `src/db/migrate.ts`, `src/db/seed.ts`,
`drizzle.config.ts`), never `node --env-file`: the flag fills `process.env` before the first import,
and `scripts/smoke/guard.ts` reads `BASE_URL` and `SMOKE_ALLOW_REMOTE` at import time so a line in
`.env.local` can't move a smoke's target or make the opt-in standing. `guardDatabaseUrl()` still
runs on the next line (the wiring test's `LOAD_ENV` is now `loadEnv();`), and the comments in
the guard, the database guard, the session minting and the wiring test that named dotenv now
name `loadEnv()`. `src/lib/load-env.test.ts` pins the precedence in a temp
directory, that the 29 files load through `loadEnv()` once each, that nothing in src, scripts or
the root configs imports dotenv, and that package.json lists it nowhere. `dotenv` 17.4.2 stays in
the lockfile only as a transitive of `shadcn` and `@sentry/bundler-plugins`; the 95 gitignored
scratch scripts in `scripts/.tmp/` were moved to the helper too. Proof it loads the same: Node's
`util.parseEnv` and dotenv's `parse` agree key for key on `.env.local` (9) and `.env.example` (8),
and every entry point, run under HEAD and under this change with a preload that stops it at its
first network attempt, held all nine `.env.local` variables there (the drizzle config's URL set in
both; `sentry-probe` finds no DSN in `.env.local` in both).

`/sets` (row 266 FIRED): re-running U10a's SSR id probe on both versions found 1.8.0's "Fix stale
and duplicated control IDs" (mui/base-ui#5457) moved a Checkbox inside a Field to the Input's rule:
an explicit id reaches the server HTML as the Field's generated id and becomes the explicit one only
after hydration, so "Main sets only"'s `label[for="sets-main-only"]` named no element until then.
The box and its label now leave the id to the Field, which shares its generated id between them in
the server HTML on either version; `sets-index-view.test.tsx` pins that from `renderToString` (it
fails on the old markup under 1.9.0). An explicit id outside a Field (the four filter boxes) still
renders as given, and no case logs a hydration warning. Row 241 re-read: no Drawer note moves focus
into a drawer whose `modal` turns on.

Checks: `pnpm check` 2,875 tests / 270 files / 5 warnings (baseline 2,866 / 269 / 5 at 9fbc1c1, the
parent), proven alone in a scratch worktree at the parent with only this commit's files; `pnpm build` route
table identical (83 lines); the local battery against a dev server on the Neon dev branch
(`counters:reset` before each third, then the sixteen `smoke:*` and `search:canned`): all
green, 718 checks and the 15 canned searches, the six session-minting smokes signing in through
better-auth 1.7.12 and better-call 1.4.4; `/sets`' server HTML on that server names the box from
its label, and after hydration the label toggles it. LATER rows 241,
266, 269, 270, 271 and 293 dated. Dependabot #10 and #6 are superseded by this commit.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: checkbox group Changes related to the checkbox group component. component: checkbox Changes related to the checkbox component. component: field Changes related to the field component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[checkbox][CheckboxGroup] Checkbox.Root id not working in "parent checkbox" use case

1 participant