Skip to content

Reset chat input bar height when text is cleared after sending#73

Merged
mcintyre94 merged 2 commits intomainfrom
resize-chat-box-on-clear-dd2e6063
Mar 14, 2026
Merged

Reset chat input bar height when text is cleared after sending#73
mcintyre94 merged 2 commits intomainfrom
resize-chat-box-on-clear-dd2e6063

Conversation

@mcintyre94
Copy link
Copy Markdown
Owner

Summary

  • Chat input TextField grows 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
  • Added .id(text.isEmpty) to the TextField — when text transitions to empty, SwiftUI recreates the view and resets its height to the minimum

Test plan

  • Type a multi-line message in the chat input bar
  • Send the message — confirm the input bar collapses back to single-line height
  • Verify keyboard dismisses correctly and no focus issues occur

🤖 Generated with Claude Code

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>
@mcintyre94 mcintyre94 force-pushed the resize-chat-box-on-clear-dd2e6063 branch from 4395957 to ab566b1 Compare March 14, 2026 18:19
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>
@claude
Copy link
Copy Markdown

claude bot commented Mar 14, 2026

Code review

Bug 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:

.padding(.vertical, 8)
.glassEffect(in: .rect(cornerRadius: 20))
.id(text.isEmpty)
if isStreaming {

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.

@mcintyre94 mcintyre94 merged commit 7454523 into main Mar 14, 2026
2 checks passed
@mcintyre94 mcintyre94 deleted the resize-chat-box-on-clear-dd2e6063 branch March 14, 2026 18:40
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