Repository navigation
fix(field-label): forward description clicks to checkbox/switch without name pollution - #4574
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR Modified ComponentsCheckboxInput (@astryxdesign/core) · View in Storybook
Field (@astryxdesign/core) · View in Storybook
Switch (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: No accessibility violations detected. Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
cixzhang
left a comment
There was a problem hiding this comment.
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.
| * single control. | ||
| * @default false | ||
| */ | ||
| hasClickableDescription?: boolean; |
There was a problem hiding this comment.
Let's skip adding this. It should be fine to allow descriptions to forward focus to the input.
| // 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>) => { |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
…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.
b459611 to
cb0ca88
Compare
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 viaaria-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.FieldLabelprophasClickableDescription(defaultfalse), set byCheckboxInputandSwitch.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.e.target === e.currentTarget,defaultPrevented) so a click on an interactive element inside aReactNodedescription isn't hijacked.Tests (both types cixzhang asked for)
CheckboxInput+Switch.toHaveAccessibleNameexcludes the description whiletoHaveAccessibleDescriptionincludes it —CheckboxInput+Switch.FieldLabelforwards 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
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.