Skip to content

Chat

Chat

tsx
"use client";

import { useState } from "react";
import {
  Avatar,
  ChatInput,
  ChatLauncher,
  ChatMessage,
  ChatMessageList,
  ChatPanel,
  ChatSource,
  ChatSources,
  ChatSuggestion,
  ChatSuggestions,
  ChatTyping,
  IconButton,
  Prose,
} from "@cherry-design-system/react";
import { Copy, RotateCcw, Sparkles } from "lucide-react";

type Message = {
  id: string;
  role: "user" | "assistant";
  text: string;
  sources?: { title: string; href: string }[];
};

async function askAssistant(question: string): Promise<Omit<Message, "id" | "role">> {
  const response = await fetch("/api/chat", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ question }),
  });
  if (!response.ok) throw new Error("The assistant is unavailable.");
  return response.json();
}

const suggestions = ["How do I add a brand color?", "Does cherry support dark mode?"];

export function Assistant() {
  const [open, setOpen] = useState(false);
  const [draft, setDraft] = useState("");
  const [messages, setMessages] = useState<Message[]>([]);
  const [pending, setPending] = useState(false);

  const send = async (text: string) => {
    setDraft("");
    setMessages((current) => [
      ...current,
      { id: crypto.randomUUID(), role: "user", text },
    ]);
    setPending(true);
    try {
      const reply = await askAssistant(text);
      setMessages((current) => [
        ...current,
        { id: crypto.randomUUID(), role: "assistant", ...reply },
      ]);
    } catch (error) {
      setMessages((current) => [
        ...current,
        {
          id: crypto.randomUUID(),
          role: "assistant",
          text: error instanceof Error ? error.message : "Something went wrong.",
        },
      ]);
    } finally {
      setPending(false);
    }
  };

  return (
    <>
      <ChatLauncher glow aria-expanded={open} onClick={() => setOpen(true)}>
        Ask AI
      </ChatLauncher>

      <ChatPanel
        variant="drawer"
        open={open}
        onOpenChange={setOpen}
        title="Assistant"
        actions={
          <IconButton
            aria-label="New conversation"
            icon={<RotateCcw />}
            size="sm"
            variant="ghost"
            onClick={() => setMessages([])}
          />
        }
      >
        <ChatMessageList streaming={pending}>
          {messages.length === 0 ? (
            <ChatSuggestions>
              {suggestions.map((text) => (
                <ChatSuggestion key={text} icon={<Sparkles />} onClick={() => send(text)}>
                  {text}
                </ChatSuggestion>
              ))}
            </ChatSuggestions>
          ) : null}

          {messages.map((message) =>
            message.role === "user" ? (
              <ChatMessage key={message.id} role="user">
                {message.text}
              </ChatMessage>
            ) : (
              <ChatMessage
                key={message.id}
                role="assistant"
                avatar={<Avatar size="sm" color="ai" icon={<Sparkles />} alt="" />}
                actions={
                  <IconButton
                    aria-label="Copy reply"
                    icon={<Copy />}
                    size="sm"
                    variant="ghost"
                    onClick={() => navigator.clipboard.writeText(message.text)}
                  />
                }
              >
                <Prose compact>
                  <p>{message.text}</p>
                </Prose>
                {message.sources?.length ? (
                  <ChatSources>
                    {message.sources.map((source) => (
                      <ChatSource key={source.href} href={source.href}>
                        {source.title}
                      </ChatSource>
                    ))}
                  </ChatSources>
                ) : null}
              </ChatMessage>
            ),
          )}

          <ChatTyping active={pending} />
        </ChatMessageList>

        <ChatInput
          value={draft}
          onValueChange={setDraft}
          onSend={send}
          loading={pending}
          footnote="Enter to send, Shift+Enter for a new line."
        />
      </ChatPanel>
    </>
  );
}

The chat kit is a set of parts you compose into an assistant UI: a ChatLauncher button opens a ChatPanel, which holds a scrolling ChatMessageList of ChatMessage turns, a ChatTyping indicator, ChatSources and ChatSuggestions, and a ChatInput composer. The parts hold no conversation state: you keep the messages and call your own backend.

Treat replies as untrusted text. Render them as plain text or through a markdown renderer that outputs React elements, never as raw HTML.

Examples

Inline panel

variant="inline" renders a plain panel in the page flow, for a dedicated chat page. It ignores open and has no close button.

tsx
import { ChatInput, ChatMessageList, ChatPanel } from "@cherry-design-system/react";

export function SupportPage({ children, onSend }: { children: React.ReactNode; onSend: (text: string) => void }) {
  return (
    <ChatPanel variant="inline" title="Support" style={{ height: 600 }}>
      <ChatMessageList>{children}</ChatMessageList>
      <ChatInput onSend={onSend} />
    </ChatPanel>
  );
}

Fullscreen and left drawer

A drawer slides in from the right by default and is modal below 992px, sitting beside the page above it. fullscreen covers the viewport and is always modal. modal overrides either default.

tsx
"use client";

import { useState } from "react";
import { ChatLauncher, ChatPanel } from "@cherry-design-system/react";

export function Panels() {
  const [open, setOpen] = useState<"left" | "full" | null>(null);

  return (
    <>
      <ChatLauncher onClick={() => setOpen("left")}>Left drawer</ChatLauncher>
      <ChatLauncher onClick={() => setOpen("full")}>Fullscreen</ChatLauncher>

      <ChatPanel
        variant="drawer"
        side="left"
        modal
        open={open === "left"}
        onOpenChange={(next) => !next && setOpen(null)}
      />
      <ChatPanel
        variant="fullscreen"
        open={open === "full"}
        onOpenChange={(next) => !next && setOpen(null)}
      />
    </>
  );
}

Streaming a reply

While a reply streams in, set streaming on the ChatMessage to show a blinking caret, and on the ChatMessageList so following jumps instead of smooth-scrolling.

tsx
import { ChatMessage, ChatMessageList } from "@cherry-design-system/react";

export function Transcript({ question, reply, streaming }: { question: string; reply: string; streaming: boolean }) {
  return (
    <ChatMessageList streaming={streaming}>
      <ChatMessage role="user">{question}</ChatMessage>
      <ChatMessage role="assistant" streaming={streaming}>
        {reply}
      </ChatMessage>
    </ChatMessageList>
  );
}

Restored history

Pass still to messages loaded from history so they skip the entrance animation.

tsx
import { ChatMessage } from "@cherry-design-system/react";

export function History({ messages }: { messages: { id: string; role: "user" | "assistant"; text: string }[] }) {
  return messages.map((m) => (
    <ChatMessage key={m.id} role={m.role} still>
      {m.text}
    </ChatMessage>
  ));
}

Composer with actions

tsx
import { ChatInput, IconButton } from "@cherry-design-system/react";
import { Paperclip } from "lucide-react";

export function Composer({ onSend }: { onSend: (text: string) => void }) {
  return (
    <ChatInput
      glow
      placeholder="Ask about your docs…"
      maxHeight={240}
      actions={<IconButton aria-label="Attach file" icon={<Paperclip />} size="sm" variant="ghost" />}
      onSend={onSend}
    />
  );
}

Properties

ChatPanel

variant"inline" | "drawer" | "fullscreen"

drawer slides in from the side (modal below 992px, beside the page above it); fullscreen covers the viewport and is modal; inline is a plain panel in the page flow. Defaults to "drawer".

side"left" | "right"

Drawer edge. Defaults to "right".

openboolean

Overlay variants: shown or not. Inline panels ignore it. Defaults to false.

onOpenChange(open: boolean) => void

Called with false when Escape or the close button asks to close.

modalboolean

Forces modal (true) or non-modal (false) instead of the variant default.

titleReactNode

Header title, rendered as an <h2>. Defaults to "AI assistant"; null hides the header text.

iconReactNode

Header icon. Defaults to sparkles; null hides it.

actionsReactNode

Extra header controls (reset, settings), placed before the close button.

hideCloseboolean

Hides the close button of an overlay panel.

closeLabelstring

Accessible name of the close button. Defaults to "Close chat".

Overlay variants render a native <dialog>; inline renders a <section>. The ref goes to whichever element renders, and other attributes pass through to it. Escape, the top layer, the inert background and the backdrop come from the platform, and focus moves to the composer on open.

ChatMessageList

streamingboolean

A reply is streaming: following jumps instead of smooth-scrolling, and turning it on re-arms following.

Renders a <div role="log">, forwards its ref to it and passes every other <div> attribute through. It follows new messages while the reader is near the bottom and stops once they scroll up.

ChatLauncher

glowboolean

Animated AI gradient border with a soft halo.

iconReactNode

Icon before the label. Defaults to sparkles.

Renders a <button type="button"> whose label defaults to "Ask AI". It forwards its ref and passes every other <button> attribute through. Pass aria-expanded to reflect the panel state.

ChatMessage

role"user" | "assistant"

user is a right-aligned bubble; assistant is a full-width reply. Defaults to "assistant".

avatarReactNode

Shown beside the message, typically an <Avatar size="sm">.

actionsReactNode

Row under the message (copy, retry), typically small IconButtons.

streamingboolean

Shows a blinking caret after the text while a reply streams in.

stillboolean

Skips the entrance animation, for messages restored from history.

Renders a <div>, forwards its ref to it and passes every other <div> attribute through. Note that role here is the message author, not an ARIA role. ChatMessage has no client JavaScript.

ChatInput

valuestring

Controlled text.

defaultValuestring

Initial text when uncontrolled. Defaults to "".

onValueChange(value: string) => void

Called with the new text on every change.

onSend(value: string) => void

Called on Enter (or the send button) with non-blank text. An uncontrolled input clears after; a controlled one is yours to clear.

loadingboolean

A reply is on its way: the send button spins and sending is blocked.

glowboolean

Animated AI gradient border with a soft halo.

actionsReactNode

Controls at the start of the footer (attach, tools), for example IconButtons.

footnoteReactNode

Small print under the composer.

maxHeightnumber

Tallest the text area grows before it scrolls, in px. Defaults to 160.

sendLabelstring

Accessible name of the send button. Defaults to "Send message".

loadingLabelstring

Accessible name of the send button while loading. Defaults to "Waiting for the reply".

ChatInput renders a wrapper <div> holding a <form> with a <textarea>. The ref and other <textarea> attributes go to the textarea, except className and style, which go to the wrapper. placeholder defaults to "Ask anything…" and aria-label to "Message".

ChatTyping

childrenReactNode

Text after the dots. Defaults to "Thinking…".

activeboolean

Keep it mounted and toggle this to let it fade and fold away (and back) instead of popping out. Defaults to true.

Renders a <div role="status">, forwards its ref to it and passes every other <div> attribute through. It has no client JavaScript.

ChatSources

labelstring

Names the list for assistive tech. Defaults to "Sources".

Renders an <ol>, forwards its ref to it and passes every other <ol> attribute through.

ChatSource

ChatSource has no props of its own. It renders an <a> inside an <li>, with its number in a circle drawn by a CSS counter, so the number follows the list order. The ref and every <a> attribute go to the link.

ChatSuggestions

labelstring

Names the group for assistive tech. Defaults to "Suggestions".

Renders a <div role="group">, forwards its ref to it and passes every other <div> attribute through.

ChatSuggestion

iconReactNode

Icon before the text.

Renders a <button type="button">, forwards its ref and passes every other <button> attribute through. Wire onClick to send its text.

Styling

PartClassAttributes and states
ChatPanel.cherry-chat-paneldata-variant (drawer, inline, fullscreen), data-side (left, right), [open], :modal
Panel parts.cherry-chat-panel__header, __title, __icon, __actions, __body (the ChatMessageList)
ChatLauncher.cherry-chat-launcher, .cherry-chat-launcher__icondata-glow
ChatMessage.cherry-chat-messagedata-role (user, assistant), data-streaming, data-still
Message parts.cherry-chat-message__avatar, __main, __content, __actions
ChatInput.cherry-chat-inputdata-glow, data-disabled; data-ready on the send button
Input parts.cherry-chat-input__frame, __surface, __textarea, __footer, __actions, __send, __arrow, __footnote
ChatTyping.cherry-chat-typingdata-active
Typing parts.cherry-chat-typing__inner, __dots, __dot, __label
ChatSources.cherry-chat-sources, .cherry-chat-sources__item, .cherry-chat-source, .cherry-chat-source__label
ChatSuggestions.cherry-chat-suggestions, .cherry-chat-suggestion, .cherry-chat-suggestion__icon, .cherry-chat-suggestion__label

The glow borders and the AI avatar color use the --cherry-ai-from and --cherry-ai-to gradient tokens. The panel reads --cherry-bg-canvas, --cherry-border-default, --cherry-elevation-xl and --cherry-bg-overlay for the backdrop; the user bubble reads --cherry-bg-muted; the send button reads --cherry-primary-default and --cherry-primary-fg. Animations are reduced under prefers-reduced-motion.

Accessibility

  • An overlay ChatPanel is a dialog named by its title (or aria-label). Escape closes it through onOpenChange, and focus goes to the composer on open.
  • ChatMessageList is a live log, so new messages are announced. A streaming message is aria-busy until it finishes.
  • ChatInput's textarea is labelled "Message" by default (override with aria-label). Enter sends, Shift+Enter adds a line, and an IME composition keeps Enter. The send button is labelled by sendLabel, or loadingLabel while a reply is on its way.
  • ChatTyping is a role="status" region, announced while active.
  • ChatSources is a labelled list of links, and ChatSuggestions a labelled group of buttons; each chip's text is its name.
  • Pass aria-expanded to ChatLauncher to reflect whether the panel is open.