Skip to content

fix: focus restoring in collections#9545

Merged
devongovett merged 7 commits intomainfrom
fix-focus-restore-in-collections
Mar 18, 2026
Merged

fix: focus restoring in collections#9545
devongovett merged 7 commits intomainfrom
fix-focus-restore-in-collections

Conversation

@snowystinger
Copy link
Member

Closes

As an alternative to #9539

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

🧢 Your Project:

@rspbot
Copy link

rspbot commented Jan 28, 2026

@rspbot
Copy link

rspbot commented Mar 16, 2026

@rspbot
Copy link

rspbot commented Mar 17, 2026

@rspbot
Copy link

rspbot commented Mar 17, 2026

Copy link
Member

@devongovett devongovett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little worried about the performance of traversing two collections. Maybe there's a simpler algorithm we can use where we walk the old collection starting from the focused key until we find an item that exists in the new collection. Though if the items were also re-ordered at the same time as items were removed the results could be unexpected. How much do we care about that case?

// Count how many items were removed before the focused item's original index
const itemNodesKeys = new Set(itemNodes.map(node => node.key));
const removedBeforeCount = cachedItemNodes.filter((node, idx) =>
idx < (startItem?.index ?? 0) && !itemNodesKeys.has(node.key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with new collections startItem.index will be relative to the section, not the entire list so this comparison may not work. It would also not account for non-item siblings which would affect its index.

@devongovett
Copy link
Member

Pushed a change to use this simpler algorithm. We'll see if there are any real-world downsides.

@rspbot
Copy link

rspbot commented Mar 18, 2026

@devongovett devongovett added this pull request to the merge queue Mar 18, 2026
Merged via the queue into main with commit 31c8dbd Mar 18, 2026
29 checks passed
@devongovett devongovett deleted the fix-focus-restore-in-collections branch March 18, 2026 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants