Skip to content

Commit e6d5a7f

Browse files
authored
fix: keep focused textarea when message composer state changes (#2759)
1 parent a590629 commit e6d5a7f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/components/MessageInput/EditMessageForm.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const EditMessageModal = ({
8686
>
8787
<MessageInput
8888
clearEditingState={clearEditingState}
89+
focus
8990
hideSendButton
9091
Input={EditMessageInput}
9192
{...additionalMessageInputProps}

src/components/TextareaComposer/TextareaComposer.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';
99
import Textarea from 'react-textarea-autosize';
1010
import { useMessageComposer } from '../MessageInput';
1111
import type {
12+
AttachmentManagerState,
1213
MessageComposerConfig,
14+
MessageComposerState,
1315
SearchSourceState,
1416
TextComposerState,
1517
} from 'stream-chat';
@@ -36,6 +38,14 @@ const configStateSelector = (state: MessageComposerConfig) => ({
3638
enabled: state.text.enabled,
3739
});
3840

41+
const messageComposerStateSelector = (state: MessageComposerState) => ({
42+
quotedMessage: state.quotedMessage,
43+
});
44+
45+
const attachmentManagerStateSelector = (state: AttachmentManagerState) => ({
46+
attachments: state.attachments,
47+
});
48+
3949
/**
4050
* isComposing prevents double submissions in Korean and other languages.
4151
* starting point for a read:
@@ -78,14 +88,14 @@ export const TextareaComposer = ({
7888
const {
7989
additionalTextareaProps,
8090
cooldownRemaining,
91+
focus,
8192
handleSubmit,
8293
maxRows: maxRowsContext,
8394
minRows: minRowsContext,
8495
onPaste,
8596
shouldSubmit: shouldSubmitContext,
8697
textareaRef,
8798
} = useMessageInputContext();
88-
8999
const maxRows = maxRowsProp ?? maxRowsContext ?? 1;
90100
const minRows = minRowsProp ?? minRowsContext;
91101
const placeholder = placeholderProp ?? additionalTextareaProps?.placeholder;
@@ -99,6 +109,14 @@ export const TextareaComposer = ({
99109
);
100110

101111
const { enabled } = useStateStore(messageComposer.configState, configStateSelector);
112+
const { quotedMessage } = useStateStore(
113+
messageComposer.state,
114+
messageComposerStateSelector,
115+
);
116+
const { attachments } = useStateStore(
117+
messageComposer.attachmentManager.state,
118+
attachmentManagerStateSelector,
119+
);
102120

103121
const { isLoadingItems } =
104122
useStateStore(suggestions?.searchSource.state, searchSourceStateSelector) ?? {};
@@ -235,6 +253,12 @@ export const TextareaComposer = ({
235253
}
236254
}, [textComposer.suggestions]);
237255

256+
useEffect(() => {
257+
const textareaIsFocused = textareaRef.current?.matches(':focus');
258+
if (!textareaRef.current || textareaIsFocused || !focus) return;
259+
textareaRef.current.focus();
260+
}, [attachments, focus, quotedMessage, textareaRef]);
261+
238262
return (
239263
<div
240264
className={clsx(

0 commit comments

Comments
 (0)