Skip to content

Commit e02b6fb

Browse files
committed
feat: retain focus on input after sending message in chat
1 parent e73e822 commit e02b6fb

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/app/whatsapp/[slug]/chats/[chatId]/chat-input.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ export function ChatInput({ slug, chatId }: ChatInputProps) {
2424
const [selectedFile, setSelectedFile] = useState<File | null>(null);
2525
const [isPending, startTransition] = useTransition();
2626
const fileInputRef = useRef<HTMLInputElement>(null);
27+
const inputRef = useRef<HTMLInputElement>(null);
2728

2829
const handleSend = () => {
2930
if (!message.trim() && !selectedFile) return;
3031

32+
const hadFocus = document.activeElement === inputRef.current;
33+
3134
startTransition(async () => {
3235
try {
3336
if (selectedFile) {
@@ -42,6 +45,10 @@ export function ChatInput({ slug, chatId }: ChatInputProps) {
4245
await sendMessageAction(slug, chatId, message);
4346
}
4447
setMessage("");
48+
49+
if (hadFocus && (document.activeElement === document.body || document.activeElement === inputRef.current)) {
50+
inputRef.current?.focus();
51+
}
4552
} catch (error) {
4653
toast.error(error instanceof Error ? error.message : "Error al enviar mensaje");
4754
}
@@ -104,6 +111,7 @@ export function ChatInput({ slug, chatId }: ChatInputProps) {
104111
<Paperclip className="h-4 w-4" />
105112
</Button>
106113
<Input
114+
ref={inputRef}
107115
placeholder={selectedFile ? "Añade un pie de foto (opcional)..." : "Escribe un mensaje..."}
108116
value={message}
109117
onChange={(e) => setMessage(e.target.value)}

src/app/whatsapp/[slug]/connections/[connectionSlug]/chats/[chatId]/chat-input.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ export function ChatInput({ slug, connectionSlug, chatId }: ChatInputProps) {
2525
const [selectedFile, setSelectedFile] = useState<File | null>(null);
2626
const [isPending, startTransition] = useTransition();
2727
const fileInputRef = useRef<HTMLInputElement>(null);
28+
const inputRef = useRef<HTMLInputElement>(null);
2829

2930
const handleSend = () => {
3031
if (!message.trim() && !selectedFile) return;
3132

33+
const hadFocus = document.activeElement === inputRef.current;
34+
3235
startTransition(async () => {
3336
try {
3437
if (selectedFile) {
@@ -43,6 +46,10 @@ export function ChatInput({ slug, connectionSlug, chatId }: ChatInputProps) {
4346
await sendMessageAction(slug, connectionSlug, chatId, message);
4447
}
4548
setMessage("");
49+
50+
if (hadFocus && (document.activeElement === document.body || document.activeElement === inputRef.current)) {
51+
inputRef.current?.focus();
52+
}
4653
} catch (error) {
4754
toast.error(error instanceof Error ? error.message : "Failed to send message");
4855
}
@@ -105,6 +112,7 @@ export function ChatInput({ slug, connectionSlug, chatId }: ChatInputProps) {
105112
<Paperclip className="h-4 w-4" />
106113
</Button>
107114
<Input
115+
ref={inputRef}
108116
placeholder={selectedFile ? "Add a caption (optional)..." : "Type a message..."}
109117
value={message}
110118
onChange={(e) => setMessage(e.target.value)}

0 commit comments

Comments
 (0)