AI Streaming Text

Readable text block for partial assistant output with an optional live cursor while tokens stream in.

Report a bug

Preview

Switch between light and dark to inspect the embedded Storybook preview.

Installation

pnpm dlx shadcn@latest add https://ui.vllnt.ai/r/ai-streaming-text.json
bash

Storybook

Explore all variants, controls, and accessibility checks in the interactive Storybook playground.

View in Storybook

2 stories available:

Code

import { forwardRef } from "react";

import { cn } from "../../lib/utils";

export type AIStreamingTextProps = React.ComponentPropsWithoutRef<"div"> & {
  /** Cursor glyph shown while streaming. */
  cursor?: string;
  /** Whether new content is still arriving. */
  isStreaming?: boolean;
  /** Whether to show the streaming cursor. */
  showCursor?: boolean;
  /** Text content available from the stream. */
  text: string;
};

const AIStreamingText = forwardRef<HTMLDivElement, AIStreamingTextProps>(
  (
    {
      className,
      cursor = "▍",
      isStreaming = false,
      showCursor = true,
      text,
      ...props
    },
    ref,
  ) => {
    return (
      <div
        aria-live={isStreaming ? "polite" : undefined}
        className={cn(
          "text-sm leading-6 text-foreground whitespace-pre-wrap",
          className,
        )}
        ref={ref}
        {...props}
      >
        {text}
        {isStreaming && showCursor ? (
          <span
            aria-hidden="true"
            className="ml-0.5 inline-block animate-pulse text-muted-foreground"
          >
            {cursor}
          </span>
        ) : null}
      </div>
    );
  },
);

AIStreamingText.displayName = "AIStreamingText";

export { AIStreamingText };
typescript

Dependencies

  • @vllnt/ui@^0.2.1