Repository navigation
[Tabs] Fix active tab flipping after programmatic selection - #5470
NoobMaster (oONoobMasterOo) wants to merge 4 commits into
Conversation
Since 5.0.1 a tab change event raised by the browser before it applied the active tab set by the server was written back to the activeid attribute, which made both values alternate indefinitely (e.g. nested FluentTabs). Tab change events are now ignored until the browser confirms the tab selected from code. Fixes microsoft#5468
There was a problem hiding this comment.
🟡 Changes recommended
Pending-selection handling can block user selections, leave component state stale, and miss dynamically added or shown tabs.
3 open issues
What changed in this PR
Addresses #5468 by tracking pending programmatic tab selections to prevent stale browser events from causing repeated tab switching.
Changes:
- Adds pending-selection handling to
FluentTabs. - Adds stale-event regression tests and updates confirmation-event coverage.
| File | Description |
|---|---|
| tests/Core/Components/Tabs/FluentTabsTests.razor | Tests stale events and selection after browser confirmation. |
| src/Core/Components/Tabs/FluentTabs.razor.cs | Tracks pending selections and filters incoming tab-change events. |
Review effort: Balanced
| var tab = Tabs.FirstOrDefault(t => t.Visible && !t.Disabled && string.Equals(t.Id, tabId, StringComparison.Ordinal)); | ||
| if (await SetActiveTabAsync(tab)) | ||
| { | ||
| _pendingActiveTabId = tab?.Id; |
There was a problem hiding this comment.
Fixed in fb42ed5. The pending tab is now set inside SetActiveTabAsync before the selection callbacks are invoked, and only when the activeid attribute actually changes. Added FluentTabs_SelectTabAsync_AcceptsUserSelectionAfterEchoDuringCallback, which delivers the echo while the callback is suspended.
| _pendingActiveTabId = Tabs.Any(t => t.Visible && !t.Disabled && string.Equals(t.Id, activeTabId, StringComparison.Ordinal)) | ||
| ? activeTabId | ||
| : null; |
There was a problem hiding this comment.
Fixed in fb42ed5. The pending tab is now validated when a tab change is received, once the child tabs are updated; if it can't be selected, it's cleared so the event is handled normally. Added FluentTabs_ParameterSelection_IgnoresStaleTabChangeForShownTab.
| if (string.Equals(args.ActiveId, _pendingActiveTabId, StringComparison.Ordinal)) | ||
| { | ||
| _pendingActiveTabId = null; | ||
| } | ||
|
|
||
| return; |
There was a problem hiding this comment.
Fixed in fb42ed5. On a matching echo, the pending tab is cleared and the event goes through SetActiveTabAsync, so ActiveTab and ActiveTabChanged are synchronized. Added FluentTabs_ParameterSelection_EchoUpdatesActiveTab.
|
@microsoft-github-policy-service agree |
Validate the pending tab when a tab change is received instead of when the parameters are set, so a tab shown or added in the same render is covered. Set the pending tab before invoking the selection callbacks, and synchronize the active tab when the browser confirms the pending selection.
|
Could you please provide a short video showing the issue before the code change, and then the expected behavior after the fix? This will make it easier to clearly see and validate the improvement. |
| return; | ||
| } | ||
|
|
||
| if (_pendingActiveTabId is not null) |
There was a problem hiding this comment.
High: a stale event can acknowledge the wrong request
Confidence: high — browser reproduction and two failing bUnit tests.
Primary location: FluentTabs.razor.cs, line 295, the block spanning lines 295 through 303.
The code treats args.ActiveId == _pendingActiveTabId as sufficient acknowledgement and clears pending state. An older user event can already carry that ID. Equality of values does not prove that the browser has processed the latest server selection.
Confirmed sequence:
- Server and browser initially select A.
- The browser selects B, then A; their events are delayed in transit.
- Before they reach the server, an asynchronous server operation requests B, making B pending.
- The old B event arrives and clears pending state, even though it predates the server request.
- The old A event is then accepted and writes A back.
- The actual B echo arrives afterwards. A/B write-backs create further events and sustain the oscillation.
Impact: the defect this PR aims to fix remains reproducible when a server selection overlaps user changes in flight. Panels keep alternating and the circuit continues exchanging events without further interaction. Both ActiveTabId parameter selection and SelectTabAsync are affected.
Recommended correction: do not use the tab ID alone as proof that the current request has been processed. Establish explicit correlation between the server request and browser acknowledgement, or an equivalent mechanism that distinguishes events predating that request without blocking later user selections. This may require an interop contract adjustment and changes to TypeScript sources, not generated JavaScript. Add coverage for “old B, old A, actual B echo” through both entry points, plus browser validation under latency.
Both temporary review tests fail after delivering old B followed by old A, before delivering the actual echo:
TabChangeHandlerAsync_StaleMatchingEventBeforeRealEcho_KeepsProgrammaticSelection
Expected: "b"
Actual: "a"
SelectTabAsync_StaleMatchingEventBeforeRealEcho_KeepsProgrammaticSelection
Expected: "b"
Actual: "a"
total: 2; failed: 2; succeeded: 0; exit code: 2
The two selection entry points are consolidated into one finding because they share one root cause.
There was a problem hiding this comment.
You were right: the matching ID doesn't prove the request was processed. Reworked in e7814e2 with an explicit correlation: the tab list renders an activeid-version (incremented when the server changes the active tab, rendered before activeid), and the tabchange event now sends the version rendered when it was raised (TypeScript in FluentUICustomEvents.ts). Events raised with an older version are ignored, so the pending state is gone and later user selections are never blocked.
Added FluentTabs_ServerSelection_IgnoresMatchingTabChangeRaisedBeforeIt (old B, old A, then the actual B echo) for both the ActiveTabId parameter and SelectTabAsync: both cases fail with the previous implementation and pass now.
Browser validation under latency: with 600 ms on the outgoing messages, "select B in 500 ms" and then clicking B and A before the B render arrives: both clicks are sent with the old version and ignored, the B echo (new version) is accepted, and the tabs stay on B with no further events. The #5468 sample with 300 ms latency settles on Results after 3 events.
A matching tab ID was used as proof that the browser had applied the active tab set by the server, but an older user event can carry the same ID. The tab list now renders an activeid-version, incremented when the server changes the active tab, and the tabchange event sends the version rendered when it was raised, so older events are ignored.
|
Here is the requested video, using the sample from #5468 with 300 ms latency added to the outgoing messages ( Before (dev): after "Show results", the inner tabs keep alternating between Setup and Results. 5470-before.mp4After (this PR): the inner tabs switch to Results and stay there, and clicking Setup / Results still works. 5470-after.mp4 |
|
We've added a new requirement regarding PRs
|
updated and checked. |
|
Thank you for your contribution and your interest in improving the library. I’m closing this PR without merging it. In its current form, the review and validation effort required is beyond what we can reasonably take on as maintainers. Our AI contribution policy welcomes AI-assisted contributions, but requires meaningful human review, understanding, testing, and ownership of every change. AI-generated code is not automatically disqualified, but contributors remain responsible for explaining the implementation, validating its behavior, and considering potential regressions. That responsibility cannot be transferred to maintainers. This decision does not mean the underlying issue is invalid. If you would like to revisit it, please consider submitting a smaller, focused PR with a clear explanation of the root cause, the proposed solution, and the validation performed. Thank you for your understanding. |


Pull Request
📖 Description
Cause
fluent-tablistraiseschangenot only on user selection, but also when the server writesactiveidand whenever its tab collection changes (setTabsre-announces the current tab).Since #5382, every
tabchangereceived by the server is written back toactiveid. When achangeraised by the browser is still in flight while the server sets another tab (e.g. a new tab becomes visible, thenActiveTabIdis set to it), the stale event is applied and written back, which raises a newchange, and so on. Both values then alternate indefinitely.Fix
The tab list renders an
activeid-versionattribute, incremented each time the server changes the active tab (ActiveTabId/ActiveTabparameters,SelectTabAsync, removal of the active tab). Thetabchangeevent sends the version rendered when it was raised (TabChangeEventArgs.ActiveIdVersion), andFluentTabsignores the events raised with an older version: they were raised before the browser applied the last server selection. All other events, including a user selection right after a server selection, are handled as before, so the behavior added in #5382 is kept.activeid-versionis rendered beforeactiveid, so thechangeraised synchronously whenactiveidis updated already sends the new version. An event without a version (ActiveIdVersion = null) is handled as before.🎫 Issues
👩💻 Reviewer Notes
The issue only reproduces with some latency between the browser and the server (see the Test Plan). The change is in
FluentTabs.razor.cs(version check in the tab change handler) andFluentUICustomEvents.ts(the version sent with the event).📑 Test Plan
Unit tests
Added:
FluentTabs_ParameterSelection_IgnoresTabChangeRaisedBeforeItFluentTabs_ServerSelection_IgnoresMatchingTabChangeRaisedBeforeIt(old B, old A, then the actual B echo, through theActiveTabIdparameter andSelectTabAsync)FluentTabs_ParameterSelection_AcceptsUserSelectionRaisedAfterItFluentTabs_ParameterSelection_IgnoresStaleTabChangeForShownTabFluentTabs_ParameterSelection_EchoUpdatesActiveTabThe snapshots are updated with
activeid-version.tests/Coresuite on this branch: 4909 tests, 0 failed (at175ea55b5).Manual test
The issue does not reproduce on localhost because the round-trip is too fast. To reproduce it, add a latency to the outgoing messages in the browser console:
changeevents after "Show results"Other checks
✅ Checklist
General
AI contribution declaration
AI assistance is allowed; fully AI-generated or autonomous pull requests without meaningful human review are not accepted. See the AI contribution policy.
Component-specific
⏭ Next Steps
None.