Skip to content

Preserve first responder position when children are replaced#52

Open
arcticleo wants to merge 1 commit intorensbreur:mainfrom
arcticleo:preserve-first-responder
Open

Preserve first responder position when children are replaced#52
arcticleo wants to merge 1 commit intorensbreur:mainfrom
arcticleo:preserve-first-responder

Conversation

@arcticleo
Copy link

If a Control's children need to be rebuilt (for example, during ForEach updates when the underlying data changes), the first responder would always reset to the first selectable element. This caused jarring UX in lists where the selection would jump to the top after any update.

With this proposed change, Control tracks which child index contained the first responder before removal. When adding new children with firstResponder == nil, it attempts to restore the first responder at that same index. If the index is out of bounds (e.g., the list got shorter after a deletion), it decrements until finding a valid selectable element.

This provides intuitive behavior for:

  • Lists where items are deleted (selection stays near same position)
  • Dynamic content that refreshes (selection doesn't jump to top)
  • Any view rebuild that replaces children

This change should be backwards compatible because it only changes behavior in cases where firstResponder is nil, which is when the selection would have been reset anyway.

If a Control's children need to be rebuilt (for example, during ForEach updates when the underlying data changes), the first responder would always reset to the first selectable element. This caused jarring UX in dynamic lists where the selection would jump to the top after any update.

With this proposed change, Control tracks which child index contained the first responder before removal. When adding new children with firstResponder == nil, it attempts to restore the first responder at that same index. If the index is out of bounds (e.g., the list got shorter after a deletion), it decrements until finding a valid selectable element.

This provides intuitive behavior for:

- Lists where items are deleted (selection stays near same position)
- Dynamic content that refreshes (selection doesn't jump to top)
- Any view rebuild that replaces children

This change should be backwards compatible because it only changes behavior in cases where firstResponder is nil, which is when the selection would have been reset anyway.
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.

1 participant