# Mentions and commands

> Type @ to pull in context or / for a command. The menu grows from the caret and your text becomes a chip.

Agents work better when people can point at what they mean. This prompt field opens a small menu right at the caret when you type a trigger: @ for files, people and pages, / for commands. It narrows as you type, its height follows the results, and a highlight slides between rows. Enter or Tab turns the typed "@q3-sa" into a chip in place. Searches can be async, and earlier results stay up while the next ones load; with nothing to show, "Searching" morphs into "No matches for …". Closing, the menu sinks back toward the caret rather than vanishing.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/mention-menu.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/mention-menu
- Registry item (source): https://hairlineui.com/r/mention-menu.json

## Usage

```tsx
import { MentionInput } from "@/components/mention-menu";

<MentionInput
  triggers={[
    { char: "@", label: "Context", items: files },
    { char: "/", label: "Commands", items: commands, startOnly: true, variant: "command" },
  ]}
  onSubmit={({ text, mentions }) => send(text, mentions)}
/>
```

## With the AI SDK

```tsx
"use client";
import { useRef } from "react";
import { useChat } from "@ai-sdk/react";
import { MentionInput, type MentionInputHandle } from "@/components/mention-menu";

export function Composer() {
  const { sendMessage } = useChat();
  const input = useRef<MentionInputHandle>(null);
  return (
    <MentionInput
      ref={input}
      triggers={[
        { char: "@", label: "Context", items: (q) => fetch(`/api/search?q=${encodeURIComponent(q)}`).then((r) => r.json()) },
        { char: "/", label: "Commands", items: COMMANDS, startOnly: true, variant: "command" },
      ]}
      onSubmit={({ text, mentions }) => {
        sendMessage(
          { text },
          {
            body: {
              context: mentions.filter((m) => m.trigger === "@").map((m) => m.id),
              command: mentions.find((m) => m.trigger === "/")?.id,
            },
          }
        );
        input.current?.clear();
      }}
    />
  );
}

// app/api/chat/route.ts: load what was mentioned into the prompt
import { convertToModelMessages, streamText } from "ai";
import { openai } from "@ai-sdk/openai";

export async function POST(req: Request) {
  const { messages, context = [], command } = await req.json();
  const docs = await Promise.all(context.map(loadDocument)); // your storage
  const result = streamText({
    model: openai("gpt-4.1"),
    system: [command && COMMAND_PROMPTS[command], ...docs.map((d) => `<doc name="${d.name}">\n${d.text}\n</doc>`)]
      .filter(Boolean)
      .join("\n\n"),
    messages: convertToModelMessages(messages),
  });
  return result.toUIMessageStreamResponse();
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `triggers` | `{ char; label; items; startOnly?; variant? }[]` | – | Each opening character, its menu name, and its items or a search (sync or async) that gets the query. |
| `onSubmit` | `(value: { text; mentions; segments }) => void` | – | Enter sends. text writes mentions as "@label"; segments keep the order for rendering. |
| `onChange` | `(value) => void` | – | Called on every edit, e.g. to enable a send button. |
| `defaultValue` | `MentionSegment[]` | – | Starting text and chips, e.g. a draft. |
| `placeholder` | `string` | `"Ask anything"` | Shown while the field is empty; also its label. |
| `side` | `"top" \| "bottom"` | `"top"` | Open above the caret (composers at the bottom of the screen) or below it. |
| `anchorRef` | `RefObject<HTMLElement>` | – | Open clear of this element, e.g. your composer, so the menu never covers it. |
| `ref` | `{ focus(); clear() }` | – | Focus the field, or clear it after sending. |

## States

- **closed**: A plain prompt field with a placeholder. Enter sends, Shift+Enter adds a line.
- **open**: A trigger after a space (or at a line’s start, for commands) grows the menu from the caret.
- **filtering**: Results narrow and rank as you type, matching text is bold, and the menu’s height animates to fit.
- **loading**: An async search shows "Searching" under a sweep of light until results arrive; on later keystrokes the old results stay and a small spinner fades in.
- **empty**: "Searching" morphs into "No matches for “xyz”" as its spinner folds away. Escape closes the menu (it sinks away in 160ms) and leaves your text alone.
- **chip**: The typed token becomes a tinted chip (or a mono command chip) where it was typed. Backspace removes it whole.

## What it handles

Agents work better when people can point at what they mean. This prompt field opens a menu at the caret when someone types @ or /, and turns the pick into a chip in the text.

- **A menu at the caret**: You define the triggers: @ for files, people and pages, / for commands. The menu grows from where the person is typing.
- **It narrows as you type**: Results rank by match, the matching text is bold and a highlight slides between rows.
- **Async search**: Earlier results stay up while the next ones load. With nothing to show, “Searching” morphs into “No matches for …”.
- **Text becomes a chip**: Enter or Tab turns the typed token into a chip in place, and Backspace removes the whole chip.
- **It doesn’t fire by accident**: @ opens only after a space or at the start, so email addresses don’t open it. IME composition never submits early.
- **Opens clear of the composer**: Open above or below the caret, or clear of your composer with anchorRef. onSubmit gives you the text and the mentions.

## Replaces

a rich text editor, caret position code, a mention popup, async search with loading, IME handling, chip rendering

## Where it fits

- **AI prompt boxes**: Type @ to attach files, pages or people as context for the model.
- **Slash commands**: Type / at the start of a line to pick a command.
- **Comments and team chat**: Mention teammates with @ and keep them as chips in the text.
- **Notes and task editors**: Link to other items from inside the text you are writing.

## Notes

- The field is a combobox: the open menu is a listbox and the active row is announced through aria-activedescendant.
- Arrow keys move, Enter or Tab picks, Escape closes; clicking a row keeps the caret in the field.
- Typing in an IME (Japanese, Chinese, Korean) never opens or submits mid-composition, and pasting always inserts plain text.
- @ only opens after a space or at the start, so email addresses don’t open the menu.

License: MIT (https://hairlineui.com/license#components)
