Skip to content

fix: prevent unbounded itemRefs growth and ArrowUp crash in Accordion - #5406

Merged
DianaSuvorova merged 2 commits into
uber:mainfrom
zigzagdev:fix/accordion-item-refs-map
Sep 4, 2026
Merged

DianaSuvorova merged 2 commits into
uber:mainfrom
zigzagdev:fix/accordion-item-refs-map

Conversation

@zigzagdev

Copy link
Copy Markdown
Contributor

Fixes #1, Fixes #2

Description

Accordion's itemRefs was a plain array that getItems() (called on every render) only ever pushed to, without ever resetting it.
Because of that, every re-render left the previous render's refs which React had already nulled out (.current = null) once it swapped in new ref objects for the same keyed panels sitting in the array forever, growing it without bound.
As a result, once any re-render happened (e.g. expanding a panel), pressing Home would silently stop moving focus because it kept reading a stale, detached ref.
Also fixed a related crash: pressing ArrowUp while focus was already on the first panel header indexed into itemRefs[-1], throwing a TypeError.

  • Replaced itemRefs (array) with a Map keyed by each panel's key, so it's rebuilt in sync with the current children on every render and reuses the existing ref as long as the key hasn't changed.
  • Added an activeItemIdx > 0 guard on ArrowUp so it no longer crashes when focus is on the first panel.
  • Added e2e regression tests in accordion.e2e.ts covering both fixes (@playwright/test wasn't installed in this environment, so I couldn't run them locally — please confirm they pass in CI)

Scope

  • Patch: Bug Fix

itemRefs was a plain array that getItems() only ever pushed to on every
render, so refs from earlier renders (nulled out by React once the key's
ref object changes) piled up forever and Home/keyboard nav could target
stale, detached refs after a re-render. Switch to a Map keyed by panel
key so refs are reused/dropped in sync with the current children, and
guard ArrowUp so it no longer indexes itemRefs[-1] when focus is already
on the first panel header.
…us bugs

Covers pressing ArrowUp while focus is already on the first panel header
(previously threw), and keyboard navigation (Home/End) after a re-render
has occurred (previously targeted stale, detached refs).
@CLAassistant

CLAassistant commented Sep 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@zigzagdev zigzagdev changed the title Fix/accordion item refs map fix: prevent unbounded itemRefs growth and ArrowUp crash in Accordion Sep 2, 2026
@DianaSuvorova

Copy link
Copy Markdown
Collaborator

Nice, thanks!

@DianaSuvorova
DianaSuvorova merged commit 59417ce into uber:main Sep 4, 2026
3 checks passed
@zigzagdev
zigzagdev deleted the fix/accordion-item-refs-map branch September 5, 2026 02:35
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.

3 participants