Skip to content

fix: focus restoring in collections#9545

Open
snowystinger wants to merge 5 commits intomainfrom
fix-focus-restore-in-collections
Open

fix: focus restoring in collections#9545
snowystinger wants to merge 5 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.

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.

3 participants