Skip to content

Commit

Permalink
ChatInput: remove querySelector avoid useRef() & forwardRef()
Browse files Browse the repository at this point in the history
  • Loading branch information
cztomsik committed Mar 28, 2024
1 parent ee50043 commit dee0ee1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/app/chat/ChatInput.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { useEffect } from "preact/hooks"
import { useEffect, useId } from "preact/hooks"
import { SendHorizontal } from "lucide"
import { AutoGrowTextarea, Form, IconButton } from "../_components"

export const ChatInput = ({ value, onChange, onSend }) => {
const id = useId()

// Steal focus if no input is focused and a key is pressed
useEffect(() => {
const listener = e => {
if (!("value" in e.target) && !e.altKey && !e.metaKey && !e.ctrlKey && e.key.length === 1) {
// TODO: useRef()
// TODO: or maybe just use one global listener and always find closest textarea/input?
const input = document.querySelector("footer textarea")!
const input = document.getElementById(id)!
e.preventDefault()
input.focus()
input.value += e.key
Expand All @@ -30,6 +30,7 @@ export const ChatInput = ({ value, onChange, onSend }) => {
return (
<Form class="relative max-w-2xl mx-auto" onSubmit={onSend}>
<AutoGrowTextarea
id={id}
class="text-lg px-3 py-3 pr-16 bg-transparent"
rows={1}
placeholder="Ask anything..."
Expand Down

0 comments on commit dee0ee1

Please sign in to comment.