Skip to content

fix(FocusScope): respect tabindex order on autofocus - #2765

Merged
zernonia merged 1 commit into
unovue:v2from
yan-ad:fix/focus-scope-tabindex-order
Sep 28, 2026
Merged

zernonia merged 1 commit into
unovue:v2from
yan-ad:fix/focus-scope-tabindex-order

Conversation

@yan-ad

@yan-ad yan-ad commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2609

Description

  • Sort tabbable candidates so positive tabindex values are focused before default tabindex=0 elements
  • Preserve DOM order for candidates with equal tabindex
  • Add Dialog coverage for autofocus with tabindex ordering

Verification

  • pnpm --filter reka-ui exec vitest run src/Dialog/Dialog.test.ts src/FocusScope/FocusScope.test.ts
  • pnpm --filter reka-ui type-check

Note: pnpm lint currently fails with an internal ESLint plugin error: TypeError: sourceCode.isSpaceBetweenTokens is not a function.

Summary by CodeRabbit

  • Bug Fixes

    • Improved keyboard focus behavior in dialogs so the initial focus now lands on the most appropriate focusable element, respecting positive tabindex order.
    • This makes dialog navigation more predictable for keyboard and assistive technology users.
  • Tests

    • Added coverage for dialog focus order to verify the correct element receives focus when a dialog opens.

@coderabbitai

coderabbitai Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 88f23f95-9c8e-45ad-b305-d9f2f91907a7

📥 Commits

Reviewing files that changed from the base of the PR and between 47c433a and 0aa68eb.

📒 Files selected for processing (2)
  • packages/core/src/Dialog/Dialog.test.ts
  • packages/core/src/FocusScope/utils.ts

📝 Walkthrough

Walkthrough

FocusScope now sorts tabbable candidates by tabindex, placing positive values ahead of zero-tabindex elements. The dialog test adds mixed-tabindex focus targets and asserts the dialog opens with focus on the lowest positive tabindex element.

Changes

Tabindex focus ordering

Layer / File(s) Summary
Tabbable candidate sorting
packages/core/src/FocusScope/utils.ts
getTabbableCandidates now returns a tabIndex-sorted list, ordering non-zero values ascending and placing tabIndex === 0 candidates after them.
Dialog tabindex test
packages/core/src/Dialog/Dialog.test.ts
A dialog fixture adds focusable elements with different tabindex values, and a test asserts that opening the dialog focuses the lowest positive tabindex element.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • unovue/reka-ui#2307: Adjusts FocusScope initial focus behavior, which is directly adjacent to the tabindex-based candidate ordering changed here.

Poem

Hop hop, I sorted the tabbable line,
With tiny tabindex footsteps in a neat little sign.
The dialog opened, and what did I see?
The lowest bright focus hopped first to me.
🐇✨

🚥 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 clearly summarizes the autofocus tabindex-order fix in FocusScope.
Linked Issues check ✅ Passed The change makes autofocus respect tabindex order and adds dialog coverage for the lowest non-negative tabindex.
Out of Scope Changes check ✅ Passed All changes are directly related to FocusScope autofocus ordering and its dialog tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed: private package registry requires authentication. Disable ESLint in CodeRabbit settings or use public packages.


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.

@pkg-pr-new

pkg-pr-new Bot commented Jun 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 0aa68eb

@zernonia
zernonia merged commit b755e67 into unovue:v2 Sep 28, 2026
7 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.

[Feature]: Respect tabindex when focussing first tabbable element in FocusTrap

2 participants