AI Elements

repository·main·Indexed 25 days ago

https://github.com/vercel/ai-elements

A React component library built on shadcn/ui for AI-native applications. It provides specialized components for common AI UI patterns, including conversations, messages, reasoning displays, code blocks, and prompt inputs. Components are installed via a dedicated CLI or the shadcn CLI and are fully customizable within the project's source code.

Tokens
118.3K
Snippets
156
Records
617
Agent score
80%

What's inside ai-elements

  1. Overview of Voice Selector features

    main

    The VoiceSelector is a composable dialog component designed for AI voice selection. Key capabilities include:

    • Composable Architecture: Granular control via sub-components like <VoiceSelectorTrigger />, <VoiceSelectorContent />, and <VoiceSelectorItem />.
    • Search & Filtering: Real-time filtering of voices via an integrated search input.
    • Metadata Display: Support for displaying voice attributes such as gender (with icons), accent (with flags), and age.
    • Voice Preview: Built-in support for play/pause/loading states for voice previews.
    • State Management: Uses React Context to allow nested components to access the selection state.
    • Navigation: Full keyboard navigation and support for both controlled and uncontrolled component patterns.
  2. Commit component features and file status mapping

    main

    The Commit component provides a comprehensive way to visualize git commits. Key features include:

    • Commit hash display with a built-in copy button.
    • Author avatars using initials.
    • Relative timestamp formatting.
    • A collapsible list of file changes.
    • Color-coded file status indicators.
    • Line addition and deletion counts.

    File Status Reference

    The component uses the following mapping for file statuses:

    StatusLabelColor
    addedAGreen
    modifiedMYellow
    deletedDRed
    renamedRBlue
  3. Features of PromptInput

    main

    The PromptInput component provides a comprehensive set of features for AI chat interfaces:

    • Text Handling: Auto-resizing textarea, customizable min/max height, and keyboard shortcuts (Enter to submit, Shift+Enter for new line).
    • File Support: File attachment support with drag-and-drop, image previews, and configurable constraints (max files, max size, accepted types).
    • Actions: Built-in screenshot capture, native speech recognition (Web Speech API), and a flexible toolbar for custom tools.
    • UI/UX: Automatic submit button icons based on status, built-in model selection dropdown, global document drop support (opt-in), and responsive design.
    • State Management: Form automatically resets on submit and provides an optional provider for lifted state management.
  4. Key features of the AI Elements IDE example

    main

    The AI Elements library provides several specialized components for building AI-driven interfaces. The IDE example utilizes the following components:

    • FileTree: Displays a hierarchical file structure with expandable folders and file selection.
    • CodeBlock: Renders syntax-highlighted code with line numbers and a copy button.
    • Terminal: Shows streaming build output with ANSI color support.
    • Plan: Displays the AI's implementation strategy using collapsible sections.
    • Queue: Organizes pending and completed tasks into separate sections.
    • Conversation & Message: Creates a streaming chat experience.
    • Checkpoint: Allows users to mark and restore specific conversation states.
    • Streaming Support: All components are designed to support real-time streaming for responsive user experiences.
  5. Use the Snippet component

    main

    The Snippet component is a lightweight inline code display designed for terminal commands and short code references. It is built on top of InputGroup and supports optional prefixes (like $ for terminal commands), built-in copy functionality, and a compact design suitable for chat or markdown interfaces.

    Common sub-components include:

    • <SnippetAddon />: For adding decorative or functional elements to the group.
    • <SnippetText />: For displaying static text within the group.
    • <SnippetInput />: For the actual code content (automatically handles value and readOnly).
    • <SnippetCopyButton />: For providing copy-to-clipboard functionality.
  6. What is the Sandbox component?

    main
    The Sandbox component is a collapsible container designed for chat interfaces to display AI-generated code and its execution output. It provides a structured UI with status badges (Pending, Running, Completed, Error), tabbed navigation between code and output views, and integrates well with CodeBlock and StackTrace components.
  7. Theme AI Elements using shadcn/ui

    main

    AI Elements is built on top of shadcn/ui, meaning it inherits its accessibility and theming capabilities.

    • Theming: It uses CSS variables, so your existing shadcn/ui theme will apply to AI Elements components automatically.
    • Accessibility: Components follow a WCAG 2.1 AA accessibility baseline and use semantic HTML.
    • Dark Mode: Dark mode support is built-in.
  8. Compose chat interfaces with Message components

    main

    AI Elements uses a composable architecture where components act as building blocks. Instead of rigid layouts, you can nest components like Message, MessageContent, and MessageResponse to build custom chat UIs. This allows you to control exactly how messages from different roles (e.g., 'assistant' or 'user') are rendered.

    <Message from="assistant">
      <MessageContent>
        <MessageResponse>{text}</MessageResponse>
      </MessageContent>
    </Message>
  9. Use Edge.Temporary and Edge.Animated edge types

    main

    The Edge component provides two distinct pre-styled edge types designed for React Flow canvases:

    • Edge.Temporary: A dashed edge style intended for temporary or preview connections. It uses a simple Bezier path with a dashed stroke pattern.
    • Edge.Animated: A solid edge featuring an animated circle that moves along the path. The animation repeats indefinitely with a 2-second duration, providing visual feedback for active connections.
  10. Extend AI Elements components with primitive attributes

    main
    AI Elements components are designed to be highly extensible by accepting standard HTML attributes. For example, the Message component extends HTMLAttributes<HTMLDivElement>, allowing you to pass any valid div props (like className, id, or onClick) directly to the component.