themeocean

Streaming Markdown Message

AI chats

About

An AI chat message that renders Markdown live as it streams in. Supports headings, lists, quotes, links and copyable code blocks, with a typing caret, Stop and Regenerate, and feedback buttons.

Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Streaming Markdown Message to your project and fits it to your theme.

Code

How Streaming Markdown Message is used: the demo's code. The component's own source comes with a free account.

Usage.tsx

// This is a file with a demo for your component
// That's what users will see in the preview
// Create new files in this directory to add more demos

"use client";

import * as React from "react";
import { RotateCcw } from "lucide-react";
import { Component } from "@/components/ui/streaming-markdown-message";
import { cn } from "@/lib/utils";



/* ------------------------------------------------------------------ */
/* A pretend model that writes Markdown a few characters at a time     */
/* ------------------------------------------------------------------ */

const QUESTION = "How do I debounce a search box in React?";

const ANSWER = [
  "## Debounce a search box",
  "",
  "A **debounce** waits until the person stops typing before it runs your search. That saves *a lot* of requests.",
  "",
  "Here is a small hook you can reuse:",
  "",
  "```tsx",
  "import { useEffect, useState } from \"react\";",
  "",
  "export function useDebounce<T>(value: T, delay = 300) {",
  "  const [debounced, setDebounced] = useState(value);",
  "",
  "  useEffect(() => {",
  "    const id = setTimeout(() => setDebounced(value), delay);",
  "    return () => clearTimeout(id);",
  "  }, [value, delay]);",
  "",
  "  return debounced;",
  "}",
  "```",
  "",
  "Then use it like this:",
  "",
  "1. Keep the input value in `useState`",
  "2. Pass it to `useDebounce(query, 300)`",
  "3. Run the search inside a `useEffect` that depends on the debounced value",
  "",
  "> Start with 300 ms. Go lower if the search feels slow, higher if your API is costly.",
  "",
  "Read more in the [React docs](https://react.dev).",
].join("\n");

const SPEEDS = [
  { label: "Slow", chars: 2 },
  { label: "Normal", chars: 4 },
  { label: "Fast", chars: 10 },
];

export default function DemoOne() {
  const [text, setText] = React.useState("");
  const [streaming, setStreaming] = React.useState(false);
  const [speed, setSpeed] = React.useState(1);

  // Each run gets a number, so a stopped or replaced run never writes again
  const run = React.useRef(0);
  React.useEffect(() => () => void (run.current += 1), []);

  const start = React.useCallback(() => {
    const id = ++run.current;
    setText("");
    setStreaming(true);

    let shown = 0;
    const tick = window.setInterval(() => {
      if (run.current !== id) return window.clearInterval(tick);
      shown = Math.min(ANSWER.length, shown + SPEEDS[speed].chars);
      setText(ANSWER.slice(0, shown));
      if (shown >= ANSWER.length) {
        window.clearInterval(tick);
        setStreaming(false);
      }
    }, 30);
  }, [speed]);

  const stop = () => {
    run.current += 1;
    setStreaming(false);
  };

  // Start writing as soon as the demo opens
  React.useEffect(() => {
    start();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  return (
    <div className="flex min-h-[640px] w-full flex-col items-center justify-center gap-6 bg-background px-4 py-10 text-foreground">
      <div className="w-full max-w-2xl space-y-6">
        {/* The question */}
        <div className="flex justify-end">
          <p className="max-w-[85%] rounded-2xl rounded-br-md bg-primary px-4 py-2.5 text-sm leading-6 text-primary-foreground">{QUESTION}</p>
        </div>

        {/* The streaming answer */}
        <Component content={text} isStreaming={streaming} timestamp="just now" onStop={stop} onRegenerate={start} />
      </div>

      {/* Controls for the demo */}
      <div className="flex flex-wrap items-center justify-center gap-3">
        <div role="group" aria-label="Streaming speed" className="inline-flex rounded-full border border-border bg-card p-0.5">
          {SPEEDS.map((s, i) => (
            <button
              key={s.label}
              type="button"
              aria-pressed={speed === i}
              onClick={() => setSpeed(i)}
              className={cn(
                "h-8 rounded-full px-3.5 text-xs font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
                speed === i ? "bg-primary text-primary-foreground" : "text-muted-foreground hover:text-foreground"
              )}
            >
              {s.label}
            </button>
          ))}
        </div>

        <button
          type="button"
          onClick={start}
          className="inline-flex h-10 items-center gap-2 rounded-full border border-border bg-card px-5 text-sm font-medium transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
        >
          <RotateCcw className="size-4" aria-hidden="true" />
          Replay
        </button>
      </div>
    </div>
  );
}

Dependencies

More by themeocean

View all
  • Interactive Tech Stack Bento Grid with Hover Effects

    Interactive Tech Stack Bento Grid with Hover Effects

    780
  • Floating Glassmorphic Navbar with Morphing Pill Indicator

    Floating Glassmorphic Navbar with Morphing Pill Indicator

    87
  • Mac-Style Floating Dock

    Mac-Style Floating Dock

    79
  • Nexus 3D Animated Login - Futuristic Authentication UI

    Nexus 3D Animated Login - Futuristic Authentication UI

    72
  • Animated 3D Perspective Hero with Floating Glass Layers

    Animated 3D Perspective Hero with Floating Glass Layers

    51
  • Interactive AI Agent Workflow Node Card

    Interactive AI Agent Workflow Node Card

    38
  • Interactive LLM Token & Context Window Visualizer

    Interactive LLM Token & Context Window Visualizer

    31
  • Interactive AI Voice Assistant Audio Visualizer

    Interactive AI Voice Assistant Audio Visualizer

    24

More in AI chats

View all
  • Agent Chat

    Agent Chat

    21
  • AI Suggestions

    AI Suggestions

    27
  • AI Actions

    AI Actions

    372
  • Aquarium Prompt

    Aquarium Prompt

    223
  • Message Parts

    Message Parts

    11
  • AI SaaS Workflow Hero

    AI SaaS Workflow Hero

    37
  • AI Chat Composer

    AI Chat Composer

    22
  • Tool Memory

    Tool Memory

    10