Skip to content

fix(react-charts): handle stale legend overflow counts - #36691

Open
Jake Salvesen (jakesalvesen) wants to merge 2 commits into
microsoft:masterfrom
jakesalvesen:fix/36641-legends-overflow-shrink
Open

fix(react-charts): handle stale legend overflow counts#36691
Jake Salvesen (jakesalvesen) wants to merge 2 commits into
microsoft:masterfrom
jakesalvesen:fix/36641-legends-overflow-shrink

Conversation

@jakesalvesen

@jakesalvesen Jake Salvesen (jakesalvesen) commented Sep 3, 2026

Copy link
Copy Markdown

Previous Behavior

Legends could crash while reconciling a dynamic item-count change. When an overflowed list shrank, useOverflowMenu() could briefly return the previous overflowCount. Subtracting that stale count from the current item count could produce a negative index and dereference items[index].props for a missing item.

The initial guard in this PR avoided the crash but could unmount the overflow trigger while isOverflowing stayed true. The registration effect did not rerun, leaving the overflow manager measuring a detached trigger.

New Behavior

  • Selects overflow menu entries from current item IDs using the public useOverflowVisibility() snapshot instead of a count-derived array offset.
  • Keeps the trigger mounted whenever isOverflowing is true, preserving its DOM identity and overflow registration during a still-overflowing shrink.
  • Derives the trigger label from the entries actually displayed in the menu.
  • Removes the redundant missing-item guard: itemIds and items are built synchronously from the same legend data; the overflow snapshot is the stale value.
  • Preserves selection, event forwarding, and full-list ARIA position metadata. No public API or styling changes.
  • Retains the patch change file for @fluentui/react-charts.

Regression Coverage

  • Retains the original 17-to-3 shrink case.
  • Adds 17-to-10 shrink coverage asserting the same connected, registered trigger and the correct count.
  • Opens the menu after 17-to-10 shrink and checks the exact remaining overflow entries, absence of removed legends, selection, and ARIA metadata.
  • Resizes after 17-to-10 shrink (250 -> 200 -> 300px), checking visible/overflow counts and persistent trigger identity.
  • Checks 17-to-0-to-17 recovery.

The layout mock reports zero width for detached elements. Reintroducing the old stale-count early-return condition makes the trigger-identity and post-shrink resize regressions fail; restoring the fix passes both.

Validation

Local validation with Node 24.20.0 and Yarn 4.18.0:

  • Focused Legends.test.tsx: 17 passed, 4 skipped, 5 snapshots passed.
  • Full react-charts:test with TZ=UTC: 917 passed, 86 skipped, 321 snapshots passed. No snapshot updates; the previously reported HeatMap date snapshot failures pass with this timezone.
  • react-charts:lint: 0 errors, 438 package warnings.
  • Scoped Prettier check through react-charts:format:check: passed for both changed files.
  • react-charts:type-check: blocked before reaching the chart type-check. The unchanged react-provider and react-button dependency builds fail during the Windows Griffel/Babel style transform because a generated module path loses part of the checkout path. No build tooling was changed. Upstream CI is still needed for this gate.

Related Issue(s)

@jakesalvesen

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@jakesalvesen
Jake Salvesen (jakesalvesen) marked this pull request as ready for review September 3, 2026 23:46
@jakesalvesen

Copy link
Copy Markdown
Author

@microsoft/charting-team this is ready for review. The focused Legends regression and package lint pass locally; the PR includes the required @fluentui/react-charts patch change file.

Comment thread packages/charts/react-charts/library/src/components/Legends/OverflowMenu.tsx Outdated
Comment thread packages/charts/react-charts/library/src/components/Legends/OverflowMenu.tsx Outdated
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.

[Bug]: Legends OverflowMenu crashes after the legend count changes (items[i] is undefined)

2 participants