aiellie

Search

Find a component, template or page

GitHub
Components

Composer

The box a message is written in: a field that grows as you type, and a row beneath it for controls, like an add menu or a model selector, that ends in a send button that turns into stop while a reply streams.

Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/composer"

shadcn needs the @aiellie registry in components.json first. npx aiellie add composer adds it on its first run, then installs the same way.

Composer is the box a message is written in. It's a form that holds the text, sends it on Enter and keeps Shift+Enter for a new line, turns its send button into stop while a reply comes in, and catches files dropped or pasted onto it. Its parts lay out the box, so whatever else a chat puts in it, a model selector or an add menu, goes in the footer beside the send button.

Getting started

  1. Lay out the box

    Composer holds the text and hands it to onSubmit, trimmed, then empties itself. Leave out value and it keeps the text on its own.

    components/chat-input.tsx
    "use client"
    
    import {
      Composer,
      ComposerFooter,
      ComposerInput,
      ComposerSubmit,
    } from "@/components/aiellie/composer"
    
    export function ChatInput({ onSend }: { onSend: (text: string) => void }) {
      return (
        <Composer onSubmit={onSend}>
          <ComposerInput />
          <ComposerFooter>
            <ComposerSubmit />
          </ComposerFooter>
        </Composer>
      )
    }
  2. Hook it up to the AI SDK

    status takes the AI SDK's useChat status as it is. While it's submitted or streaming, Enter does nothing and the send button becomes stop, which calls onStop.

    components/chat-input.tsx
    "use client"
    
    import { useChat } from "@ai-sdk/react"
    
    import {
      Composer,
      ComposerFooter,
      ComposerInput,
      ComposerSubmit,
    } from "@/components/aiellie/composer"
    
    export function ChatInput() {
      const { sendMessage, status, stop } = useChat()
    
      return (
        <Composer
          status={status}
          onStop={stop}
          onSubmit={(text) => sendMessage({ text })}
        >
          <ComposerInput />
          <ComposerFooter>
            <ComposerSubmit />
          </ComposerFooter>
        </Composer>
      )
    }

Anatomy

<div data-slot="composer-header" data-open /> {/* optional tray, just before the box */}
<form data-slot="composer" data-status="ready" data-dragging>
  <textarea data-slot="composer-input" />
  <div data-slot="composer-footer">
    {/* anything else: a model selector, an add menu, dictation */}
    <button data-slot="composer-submit" />
  </div>
</form>

data-status repeats status, and data-dragging is set while files are dragged over a box that has onFilesAdd, which dashes its border. The box takes the ring color while the input has focus, rather than drawing a ring of its own.

ComposerHeader sits on top of the box rather than inside it, so it goes just before Composer in the same wrapper. It stays mounted while closed, so it folds away instead of vanishing, and it's inert while folded.

Examples

Stop a reply

Hand onStop in and the button stops the reply while status is busy. Without it the button stays disabled until the reply is done.

Send something, then stop the reply.

Drop and paste files

onFilesAdd gets the files dropped or pasted onto the box. Show them in a ComposerHeader with Attachments, and set hasAttachments so a message of files alone can be sent. A dropped folder is left out, because the browser hands it over as an empty file. A paste that carries text as well as a picture of it pastes the text.

Hold the text yourself

Pass value and onValueChange when something else needs the text, like a suggestion that fills it in or a draft kept between chats.

const [draft, setDraft] = React.useState("")

<Composer value={draft} onValueChange={setDraft} onSubmit={send}>
  <ComposerInput />
  <ComposerFooter>
    <ComposerSubmit />
  </ComposerFooter>
</Composer>

API reference

Composer

PropTypeDefaultDescription
onSubmit(value: string) => voidrequiredCalled with the trimmed text, which is empty only when hasAttachments is set. The box empties itself afterwards.
valuestringThe text, when you hold it yourself.
defaultValuestring""The text it starts with when it holds the text itself.
onValueChange(value: string) => voidCalled as the text changes.
status"ready" | "submitted" | "streaming" | "error""ready"The AI SDK's useChat status. submitted and streaming hold sending and turn the button into stop.
onStop() => voidCalled by the stop button. Without it, stop stays disabled.
disabledbooleanfalseTurns off the input and sending.
hasAttachmentsbooleanfalseLets a message with no text be sent, because files go with it.
onFilesAdd(files: File[]) => voidCalled with files dropped or pasted onto the box. Without it, neither is caught.
classNamestringMerged onto the form.

All other form props are forwarded.

ComposerInput

PropTypeDefaultDescription
placeholderstring"Ask anything"Also its accessible name, unless you pass aria-label.
onKeyDownKeyboardEventHandlerRuns before Enter sends. Call preventDefault() to keep Enter for yourself, like a mention list does.
classNamestringMerged onto the textarea.

Takes every Textarea prop but value, defaultValue and onChange, which the composer owns. It grows with its text up to 12rem, then scrolls.

ComposerHeader

PropTypeDefaultDescription
openbooleantrueWhether the tray shows. Closed, it folds away and is inert.
classNamestringMerged onto the tray's inner row.

ComposerFooter

PropTypeDefaultDescription
classNamestringMerged onto the row. It packs its children at the end; put me-auto on one to push it to the start.

ComposerSubmit

PropTypeDefaultDescription
classNamestringMerged onto the button. It's send while ready and stop while busy.

Takes every Button prop but type and children. It's disabled while there's nothing to send.