Components / AI Chat Interfaces

AI Chat Input Bar

A modern chat input bar with send button, attachment support, and auto-resize textarea for AI conversation interfaces.

Live Preview — ai-chat-input-01

Installation

Dependencies

Install the core libraries required for this component.

npm
npm install react

Shadcn UI Setup

Add the necessary Shadcn UI primitives.

npm
npx shadcn-ui@latest add button textarea select

Source Code

ai-chat-input-01.tsx
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { useState, useRef, useEffect } from "react";

export default function AIChatInput01() {
    const [message, setMessage] = useState("");
    const [selectedModel, setSelectedModel] = useState("chatgpt");
    const [searchEnabled, setSearchEnabled] = useState(false);
    const textareaRef = useRef<HTMLTextAreaElement>(null);

    // Auto-resize textarea
    useEffect(() => {
        if (textareaRef.current) {
            textareaRef.current.style.height = "0px";
            textareaRef.current.style.height = textareaRef.current.scrollHeight + "px";
        }
    }, [message]);

    const handleSend = () => {
        if (!message.trim()) return;
        console.log("Send message:", message, "Model:", selectedModel, "Search:", searchEnabled);
        setMessage("");
    };

    const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
        if (e.key === "Enter" && !e.shiftKey) {
            e.preventDefault();
            handleSend();
        }
    };

    return (
        <div className="flex flex-col gap-2">
            {/* Top controls: model selection + search toggle */}
            <div className="flex items-center gap-2">
                <Select onValueChange={(val) => setSelectedModel(val)} defaultValue={selectedModel}>
                    <SelectTrigger className="w-[150px]">
                        <SelectValue placeholder="Select model" />
                    </SelectTrigger>
                    <SelectContent>
                        <SelectItem value="chatgpt">ChatGPT</SelectItem>
                        <SelectItem value="gpt4">GPT-4</SelectItem>
                        <SelectItem value="custom-llm">Custom LLM</SelectItem>
                    </SelectContent>
                </Select>

                <Button
                    variant={searchEnabled ? "default" : "outline"}
                    onClick={() => setSearchEnabled((prev) => !prev)}
                >
                    {searchEnabled ? "Search Enabled" : "Search"}
                </Button>
            </div>

            {/* Text input + send */}
            <div className="flex items-end gap-2">
                <Textarea
                    ref={textareaRef}
                    value={message}
                    onChange={(e) => setMessage(e.target.value)}
                    onKeyDown={handleKeyDown}
                    placeholder="Type your message..."
                    className="resize-none grow overflow-hidden p-2"
                    rows={1}
                />
                <Button onClick={handleSend} className="flex-shrink-0">
                    Send
                </Button>
            </div>
        </div>
    );
}

Shadcn Components

Primitives required in your @/components/ui directory.

Component Path
button @/components/ui/button
textarea @/components/ui/textarea
select @/components/ui/select

Functional Logic

Internal functions used to handle component logic.

Function Parameters
handleSend() None
handleKeyDown() e: React.KeyboardEvent<HTMLTextAreaElement>
AIChatInput01() None

State Management

React state variables managed within this component.

Variable Initial Value
message ""
selectedModel "chatgpt"
searchEnabled false

Variables

All variable declarations found in this component.

Name Kind Value
textareaRef const useRef<HTMLTextAreaElement>(null)

JSX Tags Usage

Every JSX/HTML tag used in this component, with usage count and source.

Tag Count Type Source
<HTMLTextAreaElement> Library Local / Inline
<SelectContent> Library @/components/ui/select
<SelectItem> Library @/components/ui/select
<SelectTrigger> Library @/components/ui/select
<SelectValue> Library @/components/ui/select
<Button> Native Native HTML
<div> Native Native HTML
<Select> Native Native HTML
<Textarea> Native Native HTML

Related Components

  • AI Chat Bubble — A conversational chat bubble component with typing indicator, avatar support, and message timestamps for AI chat interfaces.
  • AI Chat Window — A full-featured AI chat window with message history, streaming response animation, and conversation management.
  • AI Streaming Text — A typewriter-style streaming text component that simulates token-by-token AI response generation with cursor animation.
  • AI Chat Sidebar — A conversation history sidebar listing previous AI chat sessions with search, date grouping, and active state indicators.
  • AI Model Selector — A dropdown model selector for choosing between AI models (GPT-4, Claude, Gemini) with model info badges and capability indicators.
  • AI Response Actions — An action bar for AI responses with copy, regenerate, thumbs up/down, and share buttons with tooltip labels.