Skip to content
AI Interfaces block

Prompt Attachment Tray

Files staged against a prompt with a running context-token budget, unreadable files flagged before send, removal allowed mid-upload, and a send button that names its one blocker.

269 lineslucide-reactAdded 31 Aug 2026Updated 10 Sept 2026
  • ai
  • chat
  • attachments
  • upload
  • context window

What's included

  • components/chat-attachment-tray.tsx
  • Needs lucide-react

Works with

  • React
  • Next.js
  • Tailwind CSS
  • TypeScript

npx hoverlab add chat-attachment-tray

Or over MCP, from your editor's agent — no account needed.

License

Free to read, copy and install, for personal and non-commercial projects. Shipping it in client work or a paid product needs Pro ($79 once). The source lands in your repo and stops being ours — no attribution, nothing to upgrade.

Was this useful?

Start a page with this section — add more, order them, and leave with the page source.

Preview

  • q3-renewals.pdf

    820 KB · about 14,200 tokens

  • dashboard-screenshot.png

    1.4 MB · uploading 62%

  • walkthrough.mov

    41 MB

    Video is not readable by this model. Attach a screenshot or a transcript.

37,200 / 128,000 tokens · 15,800 from attachments29%

Rendered live in your current theme — this is the same component whose source is below, not a screenshot of it.

Source

components/chat-attachment-tray.tsx
'use client'

/**
 * <ChatAttachmentTray> — files staged against a prompt, before it is sent.
 *
 * The catalog's upload blocks cover uploading as the task: a dropzone, a
 * progress list, a resumable transfer. Attaching to a message is a
 * different problem. The upload is incidental; what matters is what the
 * model will actually receive, and whether the send button should be
 * available yet.
 *
 * THE THREE THINGS THIS SHOWS THAT AN UPLOAD LIST DOES NOT
 *
 *   - Whether each file can be read at all. A 40MB video attached to a text
 *     model is not a slow upload, it is a file that will be ignored, and
 *     saying so before send is the whole job.
 *   - What it costs. Attachments are the largest single driver of context
 *     spend and the one users never see coming; the running token estimate
 *     is beside the send button, not in a settings page.
 *   - Whether the context window can hold it. A tray that lets you attach
 *     past the limit and then fails on send has wasted the upload and the
 *     wait.
 *
 * SEND IS BLOCKED FOR ONE REASON AT A TIME. When it is disabled the button
 * says why in the same line, rather than being greyed with the explanation
 * somewhere above it.
 *
 * UPLOADING IS NOT A BLOCKER FOR REMOVAL. A file can be removed while it is
 * still transferring, which is the state most trays get wrong — the × is
 * hidden until the upload finishes, so the user waits for a file they
 * already decided against.
 *
 * ACCESSIBILITY: the tray is a `<ul>` with each file's status in text, not
 * only in colour; progress uses `role="progressbar"` with a real value; and
 * the token budget lives in an `aria-live="polite"` region so it is
 * announced when attaching changes it.
 */

import * as React from 'react'
import { AlertTriangle, FileText, Image as ImageIcon, Paperclip, Send, X } from 'lucide-react'

export type AttachmentState = 'uploading' | 'ready' | 'unsupported'

export interface Attachment {
  id: string
  name: string
  /** Human-readable, already formatted. */
  size: string
  kind: 'image' | 'document'
  state: AttachmentState
  /** 0–100, only meaningful while uploading. */
  progress?: number
  /** Estimated context cost once read. */
  tokens?: number
  /** Why it cannot be used, when state is `unsupported`. */
  reason?: string
}

export interface ChatAttachmentTrayProps {
  attachments?: Attachment[]
  /** Context window in tokens, for the budget line. */
  contextLimit?: number
  /** Tokens the conversation already occupies. */
  usedTokens?: number
  className?: string
}

const DEFAULT_ATTACHMENTS: Attachment[] = [
  {
    id: '1',
    name: 'q3-renewals.pdf',
    size: '820 KB',
    kind: 'document',
    state: 'ready',
    tokens: 14_200,
  },
  {
    id: '2',
    name: 'dashboard-screenshot.png',
    size: '1.4 MB',
    kind: 'image',
    state: 'uploading',
    progress: 62,
    tokens: 1_600,
  },
  {
    id: '3',
    name: 'walkthrough.mov',
    size: '41 MB',
    kind: 'document',
    state: 'unsupported',
    reason: 'Video is not readable by this model. Attach a screenshot or a transcript.',
  },
]

export function ChatAttachmentTray({
  attachments = DEFAULT_ATTACHMENTS,
  contextLimit = 128_000,
  usedTokens = 21_400,
  className = '',
}: ChatAttachmentTrayProps) {
  /*
    Per-instance prefix for every id this block emits.

    The literals these replaced were a latent duplicate the moment the
    block appeared twice on one document, and `aria-labelledby` on a
    duplicated id resolves to the first match -- so the second copy was
    labelled by the first copy's heading. Client component, so `useId` is
    the right tool.
  */
  const uid = React.useId()

  const [files, setFiles] = React.useState(attachments)
  const [prompt, setPrompt] = React.useState('Summarise the renewal risks in these.')

  const remove = (id: string) => setFiles((current) => current.filter((file) => file.id !== id))

  const usable = files.filter((file) => file.state !== 'unsupported')
  const attachedTokens = usable.reduce((sum, file) => sum + (file.tokens ?? 0), 0)
  const projected = usedTokens + attachedTokens
  const overBudget = projected > contextLimit
  const stillUploading = files.some((file) => file.state === 'uploading')

  /*
   * One reason at a time, in the order the user can act on. Listing three
   * blockers at once makes none of them the next step.
   */
  const blockedBecause = overBudget
    ? 'Over the context window'
    : stillUploading
      ? 'Waiting for uploads'
      : prompt.trim().length === 0
        ? 'Write a message first'
        : null

  const percent = Math.min(100, Math.round((projected / contextLimit) * 100))

  return (
    <section
      className={`rounded-2xl border border-border bg-card p-4 text-card-foreground ${className}`}
    >
      {files.length > 0 ? (
        <ul className="mb-3 space-y-2">
          {files.map((file) => {
            const Icon = file.kind === 'image' ? ImageIcon : FileText
            const broken = file.state === 'unsupported'

            return (
              <li
                key={file.id}
                className={`flex items-start gap-3 rounded-xl border p-2.5 ${
                  broken ? 'border-destructive/40 bg-destructive/5' : 'border-border bg-muted/30'
                }`}
              >
                <span
                  aria-hidden
                  className={`mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg ${
                    broken ? 'bg-destructive/10' : 'bg-background'
                  }`}
                >
                  {broken ? (
                    <AlertTriangle className="h-4 w-4 text-destructive" />
                  ) : (
                    <Icon className="h-4 w-4 text-muted-foreground" />
                  )}
                </span>

                <div className="min-w-0 flex-1">
                  <p className="truncate text-sm font-medium">{file.name}</p>

                  <p className="mt-0.5 text-xs text-muted-foreground">
                    {file.size}
                    {file.state === 'ready' && file.tokens ? (
                      <> · about {file.tokens.toLocaleString('en-US')} tokens</>
                    ) : null}
                    {file.state === 'uploading' ? <> · uploading {file.progress}%</> : null}
                  </p>

                  {file.state === 'uploading' ? (
                    <div
                      role="progressbar"
                      aria-valuenow={file.progress ?? 0}
                      aria-valuemin={0}
                      aria-valuemax={100}
                      aria-label={`Uploading ${file.name}`}
                      className="mt-1.5 h-1 overflow-hidden rounded-full bg-muted"
                    >
                      <div
                        className="h-full rounded-full bg-primary transition-[width] duration-300 border border-transparent"
                        style={{ width: `${file.progress ?? 0}%` }}
                      />
                    </div>
                  ) : null}

                  {broken ? (
                    <p className="mt-1 text-xs text-destructive">{file.reason}</p>
                  ) : null}
                </div>

                {/* Removable at every stage, uploading included. */}
                <button
                  type="button"
                  onClick={() => remove(file.id)}
                  aria-label={`Remove ${file.name}`}
                  className="rounded-lg p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
                >
                  <X aria-hidden className="h-4 w-4" />
                </button>
              </li>
            )
          })}
        </ul>
      ) : null}

      <label className="sr-only" htmlFor={`${uid}-chat-prompt`}>
        Message
      </label>
      <textarea
        id={`${uid}-chat-prompt`}
        rows={2}
        value={prompt}
        onChange={(event) => setPrompt(event.target.value)}
        placeholder="Ask anything about the attached files…"
        className="w-full resize-none rounded-xl border border-border bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
      />

      {/* The budget, beside the send button rather than in a settings page. */}
      <div aria-live="polite" className="mt-3">
        <div className="flex items-center justify-between gap-3 text-xs">
          <span className={overBudget ? 'font-medium text-destructive' : 'text-muted-foreground'}>
            {projected.toLocaleString('en-US')} / {contextLimit.toLocaleString('en-US')} tokens
            {attachedTokens > 0 ? (
              <> · {attachedTokens.toLocaleString('en-US')} from attachments</>
            ) : null}
          </span>
          <span className="text-muted-foreground">{percent}%</span>
        </div>
        <div className="mt-1.5 h-1 overflow-hidden rounded-full bg-muted border border-transparent">
          <div
            className={`h-full rounded-full transition-[width] duration-300 ${
              overBudget ? 'bg-destructive' : 'bg-primary'
            }`}
            style={{ width: `${percent}%` }}
          />
        </div>
      </div>

      <div className="mt-3 flex items-center justify-between gap-3">
        <button
          type="button"
          className="inline-flex items-center gap-1.5 rounded-lg border border-border px-3 py-2 text-sm font-medium transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
        >
          <Paperclip aria-hidden className="h-4 w-4" />
          Attach
        </button>

        <button
          type="button"
          disabled={Boolean(blockedBecause)}
          className="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-2 text-sm font-semibold text-primary-foreground transition-opacity hover:opacity-90 disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
        >
          <Send aria-hidden className="h-4 w-4 rtl:rotate-180" />
          {blockedBecause ?? 'Send'}
        </button>
      </div>
    </section>
  )
}

Before you paste

  • Styling is Tailwind utility classes on semantic tokens (bg-card, text-muted-foreground) — it inherits your theme instead of overriding it.
  • Install: npm i lucide-react
  • Every prop has a default, so it renders standalone before you wire it up.

Where it goes

Drop it at components/chat-attachment-tray.tsx and import it where you need the section:

import { ChatAttachmentTray } from '@/components/chat-attachment-tray'

Props

Read out of the component’s own type and signature, so this cannot drift from the source below. Every prop has a default — the component renders standalone before you pass it anything.

PropTypeDefault
attachmentsAttachment[]DEFAULT_ATTACHMENTS
contextLimitContext window in tokens, for the budget line.number128_000
usedTokensTokens the conversation already occupies.number21_400
classNamestring''

Not using React?

The same block rendered once to markup, wrapped as a file your framework compiles. Tailwind classes are framework-agnostic, so the design transfers intact — the behaviour does not.

This block is interactive. The markup below is its initial state with the event handlers stripped — you will need to re-wire the behaviour in your framework.

chat-attachment-tray.html
<!--
  Prompt Attachment Tray — markup from the Hoverlab catalog.

  This is the block rendered once to HTML and wrapped as a component
  file. It is not a port of the React source: the Tailwind classes carry
  the design, which is the part that took the work, and they are the same
  in every framework.

  This block is interactive in React and the handlers are NOT here.
  Buttons, toggles and menus render in their initial state and do
  nothing until you wire them up.
-->
<section class="rounded-2xl border border-border bg-card p-4 text-card-foreground ">
  <ul class="mb-3 space-y-2">
    <li class="flex items-start gap-3 rounded-xl border p-2.5 border-border bg-muted/30">
      <span aria-hidden="true" class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-background">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-file-text h-4 w-4 text-muted-foreground" aria-hidden="true">
          <path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"></path>
          <path d="M14 2v4a2 2 0 0 0 2 2h4"></path>
          <path d="M10 9H8"></path>
          <path d="M16 13H8"></path>
          <path d="M16 17H8"></path>
        </svg>
      </span>
      <div class="min-w-0 flex-1">
        <p class="truncate text-sm font-medium">q3-renewals.pdf</p>
        <p class="mt-0.5 text-xs text-muted-foreground">820 KB · about 14,200 tokens</p>
      </div>
      <button type="button" aria-label="Remove q3-renewals.pdf" class="rounded-lg p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-x h-4 w-4" aria-hidden="true">
          <path d="M18 6 6 18"></path>
          <path d="m6 6 12 12"></path>
        </svg>
      </button>
    </li>
    <li class="flex items-start gap-3 rounded-xl border p-2.5 border-border bg-muted/30">
      <span aria-hidden="true" class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-background">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-image h-4 w-4 text-muted-foreground" aria-hidden="true">
          <rect width="18" height="18" x="3" y="3" rx="2" ry="2"></rect>
          <circle cx="9" cy="9" r="2"></circle>
          <path d="m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21"></path>
        </svg>
      </span>
      <div class="min-w-0 flex-1">
        <p class="truncate text-sm font-medium">dashboard-screenshot.png</p>
        <p class="mt-0.5 text-xs text-muted-foreground">1.4 MB · uploading 62%</p>
        <div role="progressbar" aria-valuenow="62" aria-valuemin="0" aria-valuemax="100" aria-label="Uploading dashboard-screenshot.png" class="mt-1.5 h-1 overflow-hidden rounded-full bg-muted">
          <div class="h-full rounded-full bg-primary transition-[width] duration-300 border border-transparent" style="width:62%"></div>
        </div>
      </div>
      <button type="button" aria-label="Remove dashboard-screenshot.png" class="rounded-lg p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-x h-4 w-4" aria-hidden="true">
          <path d="M18 6 6 18"></path>
          <path d="m6 6 12 12"></path>
        </svg>
      </button>
    </li>
    <li class="flex items-start gap-3 rounded-xl border p-2.5 border-destructive/40 bg-destructive/5">
      <span aria-hidden="true" class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg bg-destructive/10">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-triangle-alert h-4 w-4 text-destructive" aria-hidden="true">
          <path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"></path>
          <path d="M12 9v4"></path>
          <path d="M12 17h.01"></path>
        </svg>
      </span>
      <div class="min-w-0 flex-1">
        <p class="truncate text-sm font-medium">walkthrough.mov</p>
        <p class="mt-0.5 text-xs text-muted-foreground">41 MB</p>
        <p class="mt-1 text-xs text-destructive">Video is not readable by this model. Attach a screenshot or a transcript.</p>
      </div>
      <button type="button" aria-label="Remove walkthrough.mov" class="rounded-lg p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-x h-4 w-4" aria-hidden="true">
          <path d="M18 6 6 18"></path>
          <path d="m6 6 12 12"></path>
        </svg>
      </button>
    </li>
  </ul>
  <label class="sr-only" for="_R_0_-chat-prompt">Message</label>
  <textarea id="_R_0_-chat-prompt" rows="2" placeholder="Ask anything about the attached files…" class="w-full resize-none rounded-xl border border-border bg-background px-3 py-2 text-sm focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">Summarise the renewal risks in these.</textarea>
  <div aria-live="polite" class="mt-3">
    <div class="flex items-center justify-between gap-3 text-xs">
      <span class="text-muted-foreground">37,200 / 128,000 tokens · 15,800 from attachments</span>
      <span class="text-muted-foreground">29%</span>
    </div>
    <div class="mt-1.5 h-1 overflow-hidden rounded-full bg-muted border border-transparent">
      <div class="h-full rounded-full transition-[width] duration-300 bg-primary" style="width:29%"></div>
    </div>
  </div>
  <div class="mt-3 flex items-center justify-between gap-3">
    <button type="button" class="inline-flex items-center gap-1.5 rounded-lg border border-border px-3 py-2 text-sm font-medium transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-paperclip h-4 w-4" aria-hidden="true"><path d="m16 6-8.414 8.586a2 2 0 0 0 2.829 2.829l8.414-8.586a4 4 0 1 0-5.657-5.657l-8.379 8.551a6 6 0 1 0 8.485 8.485l8.379-8.551"></path></svg>Attach</button>
    <button type="button" disabled="" class="inline-flex items-center gap-1.5 rounded-lg bg-primary px-3 py-2 text-sm font-semibold text-primary-foreground transition-opacity hover:opacity-90 disabled:pointer-events-none disabled:opacity-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-send h-4 w-4 rtl:rotate-180" aria-hidden="true"><path d="M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z"></path><path d="m21.854 2.147-10.94 10.939"></path></svg>Waiting for uploads</button>
  </div>
</section>
  • This is rendered HTML, not a translation of the React source. The Tailwind classes carry the design and work in any framework.
  • It is one frame: the component in its initial state, with no props applied beyond the defaults.
  • This block is interactive in React — toggles, menus or form state. None of that survives here; the markup is the closed/default state and the handlers are gone. Re-wire them in your own framework.
  • Requires Tailwind, and the design tokens the classes reference (bg-card, text-muted-foreground, and so on). The template ZIPs ship a globals.css that defines them.

What each framework gets across the whole catalog — effects convert properly; this rung is markup.

For AI

The component, its props, the design tokens it expects and the command that installs it — as one prompt. Paste it into Claude, Cursor, v0 or ChatGPT and what they build around it will match the rest of the catalog instead of inventing its own system.

See the prompt

Used in these pages

Want the whole screen instead of this one section? Open a page and copy it entire.

7 more blocks in Agent Chat

All of them free to read, copy and install — no account, no locked tiles, no watermarked preview. The whole catalog is open, and so are the API and the CLI.

Browse Agent Chat

Shipping one commercially

Copying the code is free. Putting it in client work or a paid product is what Pro is for — the licence, not the access.

  • A commercial licence for everything in the catalog
  • Unlimited bundle exports, in Vue, Svelte and Tailwind
  • One payment — no subscription, nothing to renew
Pro — $79 once

More Agent Chat blocks

View category
Open the full page for this block

Assistant Thread with Composer

A scrolling conversation with per-turn actions, announced as a polite log restricted to additions — so a streaming reply is not re-read on every token — and auto-scroll that only pins when the user is already at the bottom.

Agent Chat253 lines1 dep
Open the full page for this block

Composer with @ Sources and / Commands

The trigger menus done properly: @ and / open only at a token boundary, the query is derived from the caret so the menu closes itself, and focus never leaves the textarea — the combobox pattern, not a floating div.

Agent Chat434 lines1 dep
Open the full page for this block

Streaming Answer with Citations

Token-by-token text with linked citations, follow-up chips and a caret that stops the moment the stream does. The status is announced once; the prose is deliberately outside every live region.

Agent Chat296 lines1 dep
Open the full page for this block

Assistant Starter Screen

The blank thread, answered: grouped starter prompts as real lists of buttons, each group labelled, so the highest-leverage screen in an AI product is not a centred logo.

Agent Chat138 lines1 dep
Open the full page for this block

Message Version Switcher

The answers a regenerate throws away — siblings rather than replacements, an edited question that branches in place, and a plain sentence about what happens to the replies below.

Agent Chat272 lines1 dep
Open the full page for this block

Chat Beside a Canvas

The two-pane assistant layout, with the mismatch nobody else states: the canvas names the version it is showing and says so when that is not the one the conversation is about. Versions link back to the turn that made them.

Agent Chat321 lines1 dep
Open the full page for this block

Model Picker with Trade-offs

Model selection with the three numbers the choice actually turns on — capability, latency and cost per message — and locked models explained rather than greyed out.

Agent Chat267 lines1 dep