-
Notifications
You must be signed in to change notification settings - Fork 11.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Chat Input with Auto-Sizing Textarea #12785
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
98dc4fc
Update ChatScreen.tsx
characharm b1ebaf5
useAutosizeTextarea.ts
characharm fa9efef
Implement responsive auto-sizing chat textarea
characharm 0ebbb6a
-update compressed index.html.gz after npm run build
characharm fe2cb05
chore: normalize line endings to LF
characharm 3fb1941
refactor: Rename interface to PascalCase
characharm 0d41469
Merge branch 'master' into useAutosizeTextarea
ngxson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { useEffect, useRef, useState, useCallback } from 'react'; | ||
|
||
// Media Query for detecting "large" screens (matching Tailwind's lg: breakpoint) | ||
const LARGE_SCREEN_MQ = '(min-width: 1024px)'; | ||
|
||
// Calculates and sets the textarea height based on its scrollHeight | ||
const adjustTextareaHeight = (textarea: HTMLTextAreaElement | null) => { | ||
if (!textarea) return; | ||
|
||
// Only perform auto-sizing on large screens | ||
if (!window.matchMedia(LARGE_SCREEN_MQ).matches) { | ||
// On small screens, reset inline height and max-height styles. | ||
// This allows CSS (e.g., `rows` attribute or classes) to control the height, | ||
// and enables manual resizing if `resize-vertical` is set. | ||
textarea.style.height = ''; // Use 'auto' or '' to reset | ||
textarea.style.maxHeight = ''; | ||
return; // Do not adjust height programmatically on small screens | ||
} | ||
|
||
const computedStyle = window.getComputedStyle(textarea); | ||
// Get the max-height specified by CSS (e.g., from `lg:max-h-48`) | ||
const currentMaxHeight = computedStyle.maxHeight; | ||
|
||
// Temporarily remove max-height to allow scrollHeight to be calculated correctly | ||
textarea.style.maxHeight = 'none'; | ||
// Reset height to 'auto' to measure the actual scrollHeight needed | ||
textarea.style.height = 'auto'; | ||
// Set the height to the calculated scrollHeight | ||
textarea.style.height = `${textarea.scrollHeight}px`; | ||
// Re-apply the original max-height from CSS to enforce the limit | ||
textarea.style.maxHeight = currentMaxHeight; | ||
}; | ||
|
||
// Interface describing the API returned by the hook | ||
export interface ChatTextareaApi { | ||
value: () => string; | ||
setValue: (value: string) => void; | ||
focus: () => void; | ||
ref: React.RefObject<HTMLTextAreaElement>; | ||
onInput: (event: React.FormEvent<HTMLTextAreaElement>) => void; // Input handler | ||
} | ||
|
||
// This is a workaround to prevent the textarea from re-rendering when the inner content changes | ||
// See https://github.com/ggml-org/llama.cpp/pull/12299 | ||
// combined now with auto-sizing logic. | ||
export function useChatTextarea(initValue: string): ChatTextareaApi { | ||
const [savedInitValue, setSavedInitValue] = useState<string>(initValue); | ||
const textareaRef = useRef<HTMLTextAreaElement>(null); | ||
|
||
// Effect to set initial value and height on mount or when initValue changes | ||
useEffect(() => { | ||
const textarea = textareaRef.current; | ||
if (textarea) { | ||
if (typeof savedInitValue === 'string' && savedInitValue.length > 0) { | ||
textarea.value = savedInitValue; | ||
// Call adjustTextareaHeight - it will check screen size internally | ||
setTimeout(() => adjustTextareaHeight(textarea), 0); | ||
setSavedInitValue(''); // Reset after applying | ||
} else { | ||
// Adjust height even if there's no initial value (for initial render) | ||
setTimeout(() => adjustTextareaHeight(textarea), 0); | ||
} | ||
} | ||
}, [textareaRef, savedInitValue]); // Depend on ref and savedInitValue | ||
|
||
const handleInput = useCallback( | ||
(event: React.FormEvent<HTMLTextAreaElement>) => { | ||
// Call adjustTextareaHeight on every input - it will decide whether to act | ||
adjustTextareaHeight(event.currentTarget); | ||
}, | ||
[] | ||
); | ||
|
||
return { | ||
// Method to get the current value directly from the textarea | ||
value: () => { | ||
return textareaRef.current?.value ?? ''; | ||
}, | ||
// Method to programmatically set the value and trigger height adjustment | ||
setValue: (value: string) => { | ||
const textarea = textareaRef.current; | ||
if (textarea) { | ||
textarea.value = value; | ||
// Call adjustTextareaHeight - it will check screen size internally | ||
setTimeout(() => adjustTextareaHeight(textarea), 0); | ||
} | ||
}, | ||
focus: () => { | ||
if (textareaRef.current) { | ||
textareaRef.current.focus(); | ||
} | ||
}, | ||
ref: textareaRef, | ||
onInput: handleInput, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should preserve these comments (and you should also add comments to explain your code if necessary)