simple-aisimple-ai
GitHub

Chat Input

PreviousNext

A chat input component with mention support and automatic height adjustment.

Installation

pnpm dlx shadcn@latest add @simple-ai/chat-input

About

A powerful chat input component built on TipTap editor that features automatic height adjustment, smart keyboard handling, and optional mention support. Press Enter to submit or Shift+Enter for new lines. The component can be used as a simple text input or extended with mentions for @users, /files, and custom triggers.

This component is built on top of TipTap, a headless editor framework for building rich text editors. It provides a rich text editor with mention support, automatic height adjustment, and proper keyboard handling.

Usage

import {
  ChatInput,
  ChatInputEditor,
  ChatInputMention,
  ChatInputSubmitButton,
  createMentionConfig,
  useChatInput,
} from "@/components/ui/chat-input"
 
type Member = { id: string; name: string; avatar?: string };
const members: Member[] = [
  { id: "1", name: "Alice", avatar: "/alice.jpg" },
  { id: "2", name: "Bob" },
];
 
export function ChatWithMentions() {
  const { value, onChange, parsed, handleSubmit, mentionConfigs } =
    useChatInput({
      mentions: {
        member: createMentionConfig<Member>({
          type: "member",
          trigger: "@",
          items: members,
        }),
      },
      onSubmit: (parsed) => {
        console.log("Content:", parsed.content);
        console.log("Mentioned members:", parsed.member);
      },
    });
 
  return (
    <ChatInput onSubmit={handleSubmit} value={value} onChange={onChange}>
      <ChatInputMention
        type={mentionConfigs.member.type}
        trigger={mentionConfigs.member.trigger}
        items={mentionConfigs.member.items}
      >
        {(item) => <span>{item.name}</span>}
      </ChatInputMention>
      <ChatInputEditor placeholder="Type @ to mention..." />
      <ChatInputGroupAddon align="block-end">
        <ChatInputSubmitButton className="ml-auto" />
      </ChatInputGroupAddon>
    </ChatInput>
  );
}

Examples

Without mentions

With Custom Layout

Custom Mention Styling

Reference

ChatInput

Root container component that manages the input state and context.

PropTypeDefaultDescription
onSubmit() => void-Callback when message is submitted
isStreamingbooleanfalseWhether AI is currently streaming a response
onStop() => void-Callback to stop streaming (shows stop button)
disabledbooleanfalseDisables the input
valueJSONContent-Controlled TipTap JSON value
onChange(value: JSONContent) => void-Value change handler

ChatInputEditor

The TipTap editor instance for text input.

PropTypeDefaultDescription
placeholderstring"Type a message..."Placeholder text
disabledboolean-Disables editing
onEnter() => void-Custom Enter key handler
valueJSONContent-Optional controlled value
onChange(value: JSONContent) => void-Optional change handler

ChatInputMention

Registers a mention type (render prop pattern).

PropTypeDescription
typestringUnique mention type identifier
triggerstringTrigger character (e.g., "@" or "/")
itemsT[]Array of mention items
children(item: T, isSelected: boolean) => ReactNodeRender function for dropdown items
editorMentionClassstringCustom CSS class for mentions in editor

ChatInputSubmitButton

Submit button that adapts to loading/streaming states.

PropTypeDescription
isStreamingbooleanOverride streaming state
onStop() => voidOverride stop handler
disabledbooleanDisable the button

useChatInput

Manages chat input state with optional mentions support.

Parameters

PropTypeDescription
mentionsRecord<string, MentionConfig<any>>Mention configurations
initialValueJSONContentInitial value
onSubmit(parsed: ParsedFromObject<Mentions>) => voidCallback when message is submitted

Returns

PropTypeDescription
valueJSONContentCurrent value
onChange(value: JSONContent) => voidValue change handler
parsedParsedFromObject<Mentions>Parsed output with content string and mention arrays
clear() => voidFunction to clear the input
handleSubmit() => voidSubmit handler
mentionConfigsRecord<string, MentionConfig<any>>Mention configs (when using mentions)