Reset chat input bar height when text is cleared after sending#73
Reset chat input bar height when text is cleared after sending#73mcintyre94 merged 2 commits intomainfrom
Conversation
Add `.id(text.isEmpty)` to the TextField so SwiftUI recreates it when the text transitions to empty, collapsing it back to its single-line minimum height. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
4395957 to
ab566b1
Compare
PasteInterceptingTextInput tracks height via a dynamicHeight binding rather than SwiftUI's intrinsic sizing, so the .id() trick doesn't help — the textInputHeight state survives view recreation. Instead, watch `text` with onChange and reset textInputHeight to 36 whenever it becomes empty. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Code reviewBug found: .id(text.isEmpty) causes keyboard focus loss on first keystroke The fix uses a Bool as the view identity, which toggles in both directions. This means SwiftUI destroys and recreates the view not just after sending (intended), but also when the user types their first character (unintended). Root cause: When text goes from empty to non-empty on the first keystroke, .id() changes from true to false. SwiftUI treats this as a completely new view and calls makeUIView on PasteInterceptingTextInput, creating a fresh UITextView instance. The old UITextView that held first-responder status is deallocated, dismissing the keyboard mid-keystroke. Affected line: wisp/Wisp/Views/SpriteDetail/Chat/ChatInputBar.swift Lines 73 to 77 in ab566b1 Fix: Use a monotonically-increasing counter that only increments when a message is sent, not on every empty/non-empty transition. Add @State private var resetID = 0 to ChatInputBar, increment it in the send action alongside clearing text, and replace .id(text.isEmpty) with .id(resetID). This way the view is only recreated after send, leaving the UITextView and its keyboard focus untouched while the user is typing. |
Summary
TextFieldgrows as the user types (up to 5 lines) but wasn't collapsing back to its single-line height after a message was sent and the text was cleared.id(text.isEmpty)to theTextField— when text transitions to empty, SwiftUI recreates the view and resets its height to the minimumTest plan
🤖 Generated with Claude Code