@@ -41,6 +41,8 @@ import chatStyle from "./chat.module.scss";
41
41
42
42
import { Input , Modal , showModal , showToast } from "./ui-lib" ;
43
43
44
+ import calcTextareaHeight from "../calcTextareaHeight" ;
45
+
44
46
const Markdown = dynamic (
45
47
async ( ) => memo ( ( await import ( "./markdown" ) ) . Markdown ) ,
46
48
{
@@ -331,6 +333,10 @@ function useScrollToBottom() {
331
333
export function Chat ( props : {
332
334
showSideBar ?: ( ) => void ;
333
335
sideBarShowing ?: boolean ;
336
+ autoSize : {
337
+ minRows : number ;
338
+ maxRows ?: number ;
339
+ } ;
334
340
} ) {
335
341
type RenderMessage = Message & { preview ?: boolean } ;
336
342
@@ -347,6 +353,7 @@ export function Chat(props: {
347
353
const { submitKey, shouldSubmit } = useSubmitHandler ( ) ;
348
354
const { scrollRef, setAutoScroll } = useScrollToBottom ( ) ;
349
355
const [ hitBottom , setHitBottom ] = useState ( false ) ;
356
+ const [ textareaStyle , setTextareaStyle ] = useState ( { } ) ;
350
357
351
358
const onChatBodyScroll = ( e : HTMLElement ) => {
352
359
const isTouchBottom = e . scrollTop + e . clientHeight >= e . scrollHeight - 20 ;
@@ -380,6 +387,16 @@ export function Chat(props: {
380
387
dom . scrollTop = dom . scrollHeight - dom . offsetHeight + paddingBottomNum ;
381
388
} ;
382
389
390
+ // textarea has an adaptive height
391
+ const resizeTextarea = ( ) => {
392
+ const dom = inputRef . current ;
393
+ if ( ! dom ) return ;
394
+ const { minRows, maxRows } = props . autoSize ;
395
+ setTimeout ( ( ) => {
396
+ setTextareaStyle ( calcTextareaHeight ( dom , minRows , maxRows ) ) ;
397
+ } , 50 ) ;
398
+ } ;
399
+
383
400
// only search prompts when user input is short
384
401
const SEARCH_TEXT_LIMIT = 30 ;
385
402
const onInput = ( text : string ) => {
@@ -504,6 +521,11 @@ export function Chat(props: {
504
521
// eslint-disable-next-line react-hooks/exhaustive-deps
505
522
} , [ ] ) ;
506
523
524
+ // Textarea Adaptive height
525
+ useEffect ( ( ) => {
526
+ resizeTextarea ( ) ;
527
+ } ) ;
528
+
507
529
return (
508
530
< div className = { styles . chat } key = { session . id } >
509
531
< div className = { styles [ "window-header" ] } >
@@ -659,8 +681,8 @@ export function Chat(props: {
659
681
< textarea
660
682
ref = { inputRef }
661
683
className = { styles [ "chat-input" ] }
684
+ style = { textareaStyle }
662
685
placeholder = { Locale . Chat . Input ( submitKey ) }
663
- rows = { 2 }
664
686
onInput = { ( e ) => onInput ( e . currentTarget . value ) }
665
687
value = { userInput }
666
688
onKeyDown = { onInputKeyDown }
0 commit comments