Skip to content

fix(field-label): forward description clicks to checkbox/switch without name pollution - #4574

Merged
cixzhang merged 5 commits into
mainfrom
fix/field-label-clickable-description
Aug 1, 2026
Merged

cixzhang merged 5 commits into
mainfrom
fix/field-label-clickable-description

Conversation

@freddymeta

Copy link
Copy Markdown
Contributor

What

Clicking a CheckboxInput or Switch description now toggles the control. Today the description renders as a sibling <span> after the </label>, so native label-click activation never covers it — only the label line toggles.

This is the review-adjusted take on #4152, incorporating @cixzhang's feedback there.

Why not just nest the description in the <label>

That was #4152's approach, and the blocking concern was correct: for a single control, the <label htmlFor> supplies the control's accessible name, so nesting the description inside it makes the name become "Label + full description" — and since the description is also wired via aria-describedby, screen readers announce it twice (name + description).

Approach (cixzhang's option 2)

Keep the description a sibling <span> — so it's never folded into the accessible name — and forward its click to the control by id. The whole label area becomes a hit target with no accessible-name change.

  • New FieldLabel prop hasClickableDescription (default false), set by CheckboxInput and Switch.
  • Off for Field-backed text inputs (TextInput, Selector, …): clicking a text input's label only focuses it — no toggle benefit — so there's no reason to risk the name. Ignored for group labels too.
  • Forwarding is a mouse-only convenience: the control remains the keyboard path, the description isn't focusable, so there's no new tab stop and no a11y-tree change. Guarded (e.target === e.currentTarget, defaultPrevented) so a click on an interactive element inside a ReactNode description isn't hijacked.

Tests (both types cixzhang asked for)

  • Click-to-toggle: clicking the description toggles the control — CheckboxInput + Switch.
  • No name pollution (the assertion the original PR was missing): toHaveAccessibleName excludes the description while toHaveAccessibleDescription includes it — CheckboxInput + Switch.
  • Scope: FieldLabel forwards only when opted in, not for the default text-input path, not for group labels, and keeps the description a sibling of the <label>.

Testing

  • CheckboxInput (37) + Switch (48) + FieldLabel (16) = 101 passed; Field/TextInput/RadioList (188) unaffected.
  • typecheck:docs, check-sync, sync-exports --check, changeset check, eslint — all green.

Note: scoped to CheckboxInput + Switch (the controls sharing FieldLabel). RadioListItem uses a different (Item-based) structure — a follow-up if wanted.

Supersedes the approach in #4152.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jul 31, 2026
@vercel

vercel Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 1, 2026 2:10pm

Request Review

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Jul 31, 2026
@github-actions

github-actions Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

CheckboxInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 455 -
Complexity N/A Very High (45) -
Field (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 552 -
Complexity N/A Very High (37) -
Switch (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 473 -
Complexity N/A Very High (44) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.9KB 1.3KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

github-actions Bot added a commit that referenced this pull request Jul 31, 2026

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks this seems much better! I'm thinking we may not need an extra prop and just always have the description area to forward clicks to focus the input. You should be able to use useInputContainer to achieve this and it will also skip activating if there's nested interactive content in the description or label like a link that gets clicked.

Comment thread packages/core/src/Field/FieldLabel.tsx Outdated
* single control.
* @default false
*/
hasClickableDescription?: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's skip adding this. It should be fine to allow descriptions to forward focus to the input.

Comment thread packages/core/src/Field/FieldLabel.tsx Outdated
// the label text keeps native `htmlFor` activation — so this adds no new tab
// stop and no a11y-tree change. Guarded so a click that lands on an
// interactive element inside a ReactNode description isn't hijacked.
const handleDescriptionClick = (e: React.MouseEvent<HTMLSpanElement>) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to leverage useInputContainer instead to forward the click to focus with should also skip potential nested interactive elements so won't interfere with links or other interactive content in the description. It also has a text selection guard.

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description-click forwarding via ownerDocument.getElementById is the right call — it resolves against the description's owning document (portal/iframe-safe) rather than global document, and the ref-shaped getter is just lazy resolution since FieldLabel points at the control by id. Sibling-span + aria-describedby avoids accessible-name pollution, and both click-to-toggle and no-name-pollution tests are present. Addresses my earlier feedback. Approving.

@cixzhang
cixzhang enabled auto-merge (squash) August 1, 2026 06:36
@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Aug 1, 2026
…ut name pollution

Clicking a CheckboxInput/Switch description did nothing — the description
renders as a sibling <span> after the </label>, so native label-click
activation never covered it.

This is the review-adjusted take on #4152. Rather than nesting the
description inside the <label> (which folds it into the control's
accessible NAME and, combined with the existing aria-describedby, makes
screen readers announce it twice), keep the description a sibling and
forward its click to the control by id. The whole label area becomes a
hit target with no accessible-name change.

- New `FieldLabel` prop `hasClickableDescription` (default false), set by
  CheckboxInput and Switch. Off for `Field`-backed text inputs (clicking a
  text label only focuses — no toggle benefit — so no reason to risk the
  name) and ignored for group labels.
- Forwarding is a mouse-only convenience: the control stays the keyboard
  path, the description isn't focusable, so no new tab stop / a11y-tree
  change. Guarded so clicks on an interactive node inside a ReactNode
  description aren't hijacked.

Tests: click-the-description toggles (CheckboxInput + Switch); the control's
accessible name excludes the description while accessible description
includes it (toHaveAccessibleName/Description — the assertion the original
PR was missing); FieldLabel forwards only when opted in, never for the
default text-input path or group labels, and keeps the description a
sibling of the label.
Remove the `hasClickableDescription` opt-in. The description always forwards
clicks to its control via the shared interactive-skip + focus-vs-click logic:
`.focus()` for text-like inputs (matching native label behavior) and
`.click()` for checkbox/switch. Clicks on interactive content inside a
description (links, buttons) are left alone. Group labels still forward
nothing. Reuses FOCUS_INPUT_TYPES and INTERACTIVE_SELECTORS from the hooks
layer so the definition is shared, not duplicated.
Use the existing useInputContainer hook to forward description clicks to
the control instead of hand-rolling the click logic. The hook already
skips nested interactive content and guards against text selection, so
the bespoke handler and the FOCUS_INPUT_TYPES export are dropped.
…s active

Attaching the container ref unconditionally would let useClickableContainer
stamp data-pressable-container on non-forwarding descriptions (group labels,
no inputID), which is in the interactive-selector set. Gate the ref on
forwardsDescriptionClick so those cases keep a plain span.
… focus

useInputContainer classified controls by input.type, so a combobox rendered
as <input type="text"> (DateInput's field, role=combobox aria-haspopup=dialog)
got .focus() — which focuses it but never opens the picker (it opens on click,
with no onFocus opener). Detect popup triggers (role=combobox or aria-haspopup)
and .click() them before the type check. Plain text/number/textarea inputs are
unaffected and still .focus(). Fixes clicking a Field description forwarding to
a DateInput without opening the calendar.
@cixzhang
cixzhang merged commit 921d3db into main Aug 1, 2026
19 checks passed
github-actions Bot added a commit that referenced this pull request Aug 1, 2026
@github-actions
github-actions Bot deleted the fix/field-label-clickable-description branch August 2, 2026 06:57

This branch was successfully deployed

1 active deployment
Preview — cb0ca88b Deployed Aug 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants