Skip to content

perf(Checkbox): skip label querySelector when aria-label is provided - #2715

Merged
zernonia merged 1 commit into
v2from
perf/checkbox-skip-label-query
Jun 16, 2026
Merged

zernonia merged 1 commit into
v2from
perf/checkbox-skip-label-query

Conversation

@zernonia

@zernonia zernonia commented Jun 16, 2026 •

Copy link
Copy Markdown
Member

Closes #2318.

Summary

CheckboxRoot looks up a fallback aria-label by running document.querySelector('[for="<id>"]'). This runs even when an explicit aria-label is already provided, which gets expensive when rendering many checkboxes (e.g. hundreds in a data table).

Changes

  • Guard the lookup inside the ariaLabel computed via useAttrs(): when aria-label is present, return early and skip the query entirely. The explicit label always wins anyway, so behavior is unchanged.
  • Tests: assert the [for=...] query is not called when aria-label is 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

    • Enhanced test coverage for Checkbox component accessibility behavior with aria-label support.
  • Bug Fixes

    • Improved Checkbox accessibility by properly handling explicit aria-label attributes while maintaining support for labels linked via the for attribute.

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>
@coderabbitai

coderabbitai Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

CheckboxRoot.vue now imports useAttrs and short-circuits the ariaLabel computed property when attrs['aria-label'] is present, skipping the document.querySelector DOM lookup. New tests in Checkbox.test.ts spy on document.querySelector to verify the query is suppressed when aria-label is provided and executed when it is not.

Changes

Conditional aria-label DOM lookup in CheckboxRoot

Layer / File(s) Summary
Conditional ariaLabel computation and tests
packages/core/src/Checkbox/CheckboxRoot.vue, packages/core/src/Checkbox/Checkbox.test.ts
CheckboxRoot.vue adds useAttrs import and rewrites ariaLabel to return undefined immediately when attrs['aria-label'] is set, bypassing the document.querySelector call. Tests expand Vitest imports to include afterEach, vi, and nextTick, and add a describe block that spies on document.querySelector to assert it is skipped when aria-label is provided and called when it is absent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 A label was sought with a query each time,
No matter if one was already defined!
Now attrs are checked with a quick little glance,
The DOM gets a rest — no unnecessary dance.
Fewer selectors, faster the hops,
This rabbit approves all the QuerySelector stops! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and concisely describes the main performance optimization: skipping label querySelector when aria-label is provided.
Linked Issues check ✅ Passed The changes directly address issue #2318 by adding a conditional check to skip querySelector when aria-label is explicitly provided, with corresponding tests to verify the optimization.
Out of Scope Changes check ✅ Passed All changes are scoped to the Checkbox component's performance optimization objective; no unrelated modifications are present in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/checkbox-skip-label-query

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new

pkg-pr-new Bot commented Jun 16, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/reka-ui@2715

commit: d4db6fd

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (2)
packages/core/src/Checkbox/CheckboxRoot.vue (2)

126-134: Consider applying the same optimization to SwitchRoot and Radio.

The ariaLabel short-circuit optimization (skipping querySelector when attrs['aria-label'] is present) is currently unique to CheckboxRoot. Based on the relevant code snippets, SwitchRoot.vue (lines 82–106) and Radio.vue (lines 47–83) use the same document.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 value

Optional: clarify comment precision.

The comment states "An explicit aria-label always wins," but the guard if (attrs['aria-label']) only short-circuits for truthy values. If a user passes aria-label="" (empty string), it's falsy, so the fallback querySelector still runs. This is reasonable behavior (an empty label isn't useful), but the comment could be more precise: "A truthy explicit aria-label skips 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

📥 Commits

Reviewing files that changed from the base of the PR and between fa5cf0e and d4db6fd.

📒 Files selected for processing (2)
  • packages/core/src/Checkbox/Checkbox.test.ts
  • packages/core/src/Checkbox/CheckboxRoot.vue

@zernonia
zernonia merged commit 39c52df into v2 Jun 16, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Perf]: CheckboxRoot calls document.querySelector even if aria label is passed

1 participant