Repository navigation
perf(Checkbox): skip label querySelector when aria-label is provided - #2715
Conversation
Closes #2318. `CheckboxRoot` resolved a fallback aria-label by running `document.querySelector([for="<id>"])`. The query is now guarded inside the computed so it never runs when an explicit `aria-label` is passed — avoiding hundreds of DOM lookups when rendering many checkboxes (e.g. a data table). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthrough
ChangesConditional aria-label DOM lookup in CheckboxRoot
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/core/src/Checkbox/CheckboxRoot.vue (2)
126-134: Consider applying the same optimization toSwitchRootandRadio.The
ariaLabelshort-circuit optimization (skippingquerySelectorwhenattrs['aria-label']is present) is currently unique toCheckboxRoot. Based on the relevant code snippets,SwitchRoot.vue(lines 82–106) andRadio.vue(lines 47–83) use the samedocument.querySelector([for="..."])pattern without this guard. If those components are also rendered at scale (e.g., large forms or lists), they would benefit from the same performance improvement.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/Checkbox/CheckboxRoot.vue` around lines 126 - 134, Apply the same ariaLabel optimization from CheckboxRoot to both SwitchRoot and Radio components. In each component, add a short-circuit check that returns undefined immediately if attrs['aria-label'] is explicitly set, before executing the potentially expensive document.querySelector([for="..."]) lookup. This prevents unnecessary DOM queries when an explicit aria-label is already provided and improves performance when rendering these components at scale.
127-128: 💤 Low valueOptional: clarify comment precision.
The comment states "An explicit
aria-labelalways wins," but the guardif (attrs['aria-label'])only short-circuits for truthy values. If a user passesaria-label=""(empty string), it's falsy, so the fallbackquerySelectorstill runs. This is reasonable behavior (an empty label isn't useful), but the comment could be more precise: "A truthy explicitaria-labelskips the query."🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/core/src/Checkbox/CheckboxRoot.vue` around lines 127 - 128, The comment in CheckboxRoot.vue at the aria-label guard is imprecise about the actual behavior. Update the comment to clarify that only a truthy explicit aria-label value skips the querySelector, not just any explicit aria-label. Change the comment text from "An explicit `aria-label` always wins" to more accurately reflect that "A truthy explicit `aria-label` skips the query" or similar wording that captures the truthiness check in the guard condition.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/core/src/Checkbox/CheckboxRoot.vue`:
- Around line 126-134: Apply the same ariaLabel optimization from CheckboxRoot
to both SwitchRoot and Radio components. In each component, add a short-circuit
check that returns undefined immediately if attrs['aria-label'] is explicitly
set, before executing the potentially expensive
document.querySelector([for="..."]) lookup. This prevents unnecessary DOM
queries when an explicit aria-label is already provided and improves performance
when rendering these components at scale.
- Around line 127-128: The comment in CheckboxRoot.vue at the aria-label guard
is imprecise about the actual behavior. Update the comment to clarify that only
a truthy explicit aria-label value skips the querySelector, not just any
explicit aria-label. Change the comment text from "An explicit `aria-label`
always wins" to more accurately reflect that "A truthy explicit `aria-label`
skips the query" or similar wording that captures the truthiness check in the
guard condition.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: abfe57f7-edeb-4dbb-a186-f61c48f9b96e
📒 Files selected for processing (2)
packages/core/src/Checkbox/Checkbox.test.tspackages/core/src/Checkbox/CheckboxRoot.vue
Closes #2318.
Summary
CheckboxRootlooks up a fallback aria-label by runningdocument.querySelector('[for="<id>"]'). This runs even when an explicitaria-labelis already provided, which gets expensive when rendering many checkboxes (e.g. hundreds in a data table).Changes
ariaLabelcomputed viauseAttrs(): whenaria-labelis present, return early and skip the query entirely. The explicit label always wins anyway, so behavior is unchanged.[for=...]query is not called whenaria-labelis set, and is attempted as the fallback when it isn't.Notes
No API or behavior change — purely avoids unnecessary DOM work.
🤖 Generated with Claude Code
Summary by CodeRabbit
Tests
Bug Fixes
forattribute.