Skip to content

Commit 192baf7

Browse files
committed
fix: focus message composer textarea on quoted message value change
1 parent adeb0e7 commit 192baf7

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

src/components/MessageInput/QuotedMessagePreview.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useEffect, useMemo } from 'react';
22

33
import { CloseIcon } from './icons';
44
import { Attachment as DefaultAttachment } from '../Attachment';
@@ -12,6 +12,7 @@ import { useTranslationContext } from '../../context/TranslationContext';
1212
import { useStateStore } from '../../store';
1313
import { useMessageComposer } from './hooks';
1414
import { renderText as defaultRenderText } from '../Message/renderText';
15+
import { useMessageInputContext } from '../../context';
1516
import type { MessageComposerState, TranslationLanguages } from 'stream-chat';
1617
import type { MessageContextValue } from '../../context';
1718

@@ -56,6 +57,7 @@ export const QuotedMessagePreview = ({
5657
const { Attachment = DefaultAttachment, Avatar = DefaultAvatar } =
5758
useComponentContext('QuotedMessagePreview');
5859
const { userLanguage } = useTranslationContext('QuotedMessagePreview');
60+
const { focus, textareaRef } = useMessageInputContext();
5961
const messageComposer = useMessageComposer();
6062
const { quotedMessage } = useStateStore(
6163
messageComposer.state,
@@ -82,6 +84,12 @@ export const QuotedMessagePreview = ({
8284

8385
const poll = quotedMessage?.poll_id && client.polls.fromState(quotedMessage.poll_id);
8486

87+
useEffect(() => {
88+
const textareaIsFocused = textareaRef.current?.matches(':focus');
89+
if (!textareaRef.current || textareaIsFocused || !focus) return;
90+
textareaRef.current.focus();
91+
}, [focus, quotedMessage, textareaRef]);
92+
8593
if (!quotedMessageText && !quotedMessageAttachments.length && !poll) return null;
8694

8795
return (

0 commit comments

Comments
 (0)