Skip to content

fix(core): stop emitting spurious blur for non-focused widgets on unregister#1665

Open
atul-upadhyay-7 wants to merge 1 commit into
Karanjot786:mainfrom
atul-upadhyay-7:fix/focusmanager-unregister-blur
Open

fix(core): stop emitting spurious blur for non-focused widgets on unregister#1665
atul-upadhyay-7 wants to merge 1 commit into
Karanjot786:mainfrom
atul-upadhyay-7:fix/focusmanager-unregister-blur

Conversation

@atul-upadhyay-7

Copy link
Copy Markdown

Summary

Fixes a bug where FocusManager.unregister() emitted a blur event for non-focused widgets, causing downstream handlers to crash when looking up the removed widget by ID.

Closes #1660

Root Cause

In the unregister() method, when a widget that precedes the focused widget was removed (idx < this._currentIndex), the code emitted both blur and focus events:

} else if (idx < this._currentIndex) {
    this._currentIndex--;
    this._events.emit('blur', { targetId: id, ... }); // BUG: id is already removed
    this._events.emit('focus', { targetId: this._focusables[this._currentIndex].id, ... });
}

The blur event targeted the removed widget (id) which was never focused. Handlers that looked up this widget by ID got undefined and crashed.

Fix

  • Non-focused widgets: When unregistering a widget that isn't currently focused, silently adjust _currentIndex if needed. No blur/focus events because the focused widget hasn't changed.
  • Focused widget: When unregistering the currently focused widget, emit blur for it (valid transition), then move focus to the next available widget.

Tests added

Test What it verifies
does not emit blur when unregistering a non-focused widget The original bug scenario
emits blur when unregistering the focused widget Correct blur on focused removal
moves focus to next widget when focused widget is unregistered Focus transitions correctly
sets currentId to null when last widget is unregistered Edge case: empty list
adjusts index correctly when non-focused widget before focused is removed Index bookkeeping
unregistering a non-focused widget after focused one does not affect focus No spurious focus change
does not emit any events when unregistering a non-focused widget Zero side effects

Verification

  • All 444 core tests pass (including 7 new tests)
  • Typecheck passes across all packages
  • No breaking changes to public API

…egister

When unregistering a widget that precedes the focused one, FocusManager
emitted a blur event for the removed widget even though it was never
focused. Downstream handlers looking up this widget by ID got undefined
and crashed.

Only emit blur when the actually focused widget is removed. For
non-focused widgets, silently adjust the index since the focused widget
hasn't changed.

Closes Karanjot786#1660
@coderabbitai

coderabbitai Bot commented Jun 19, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@atul-upadhyay-7, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 8 minutes and 46 seconds. Learn how PR review limits work.

To continue reviewing without waiting, enable usage-based billing in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, the refill rate gradually slows as usage increases. The highest same-day bursts are limited more strictly.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 0ee113c1-6043-467e-ac68-99df6ba873ed

📥 Commits

Reviewing files that changed from the base of the PR and between 4b874c0 and 8d7eec3.

📒 Files selected for processing (2)
  • packages/core/src/events/FocusManager.test.ts
  • packages/core/src/events/FocusManager.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@github-actions github-actions Bot added type:bug +10 pts. Bug fix. needs-star PR author has not starred the repo. area:core @termuijs/core type:testing +10 pts. Tests. and removed type:bug +10 pts. Bug fix. needs-star PR author has not starred the repo. labels Jun 19, 2026
@github-actions

Copy link
Copy Markdown

Hi @atul-upadhyay-7 👋

Star this repo before your PR merges.

Why? GSSoC 2026 contributors who star get priority review and points credit. After you star, push any commit (or re-run this check). The needs-star label lifts automatically.

Thanks for your contribution to TermUI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core @termuijs/core type:testing +10 pts. Tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] FocusManager.unregister emits blur for non-existent widget causing crashes

1 participant