UIPin
ComponentsIconsIllustrationsMotionTemplatesApps
Boards
UIPin

Free, open-source UI components, icons, illustrations, motion, templates and apps, in one search. Every item links to its source and licence.

UIPin is free. If it saved you time, you can keep it running:

Buy me a coffee
ComponentsIconsIllustrationsMotionTemplatesAppsLibrariesCompareNew this weekMCP for agentsBoardsPrivacy

Components, icons and artwork belong to their authors and are shown under their open-source licences.

SmoothUI/Ai Conversation

Other
Compiling

Theme it

Every preview, live

Brand colour

Corner radius

Font

Recolours shadcn’s tokens, Tailwind’s accent colours and colours set in the code. Red, amber and green classes stay, since they usually mean error, warning or success.

Ai Conversation

Thread scroll container that follows the bottom only while the reader is already there, offering a jump-to-latest pill when it stops.

npx shadcn@latest add https://smoothui.dev/r/ai-conversation.json
Open in v0 Docs Repo
Library
SmoothUI
License
MIT
Stack
React · Tailwind CSS · animated · base-ui
Packages
@base-ui/reactclass-variance-authoritylucide-reactmotion
Also adds
index
Size
153 lines
Demo
Written by UIPin: the library doesn’t ship a usage example, so the preview uses one of ours (in the code below).

Code

components/smoothui/ai-conversation/index.tsx

"use client";

import SmoothButton from "@/components/smoothui/smooth-button";
import { cn } from "@/lib/utils";
import { ArrowDown } from "lucide-react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import {
  type ReactNode,
  useCallback,
  useEffect,
  useLayoutEffect,
  useRef,
  useState,
} from "react";

const SPRING_DEFAULT = {
  bounce: 0.1,
  duration: 0.25,
  type: "spring" as const,
};
/**
 * How close to the bottom still counts as "at the bottom".
 *
 * Zero would break on fractional scroll heights, and anything large would keep
 * yanking the view down while someone is reading a few lines up.
 */
const BOTTOM_THRESHOLD_PX = 48;

export type AIConversationProps = {
  children: ReactNode;
  className?: string;
  /**
   * Changes whenever content grows — message count, or streamed text length.
   * Used to decide when to follow the bottom.
   */
  contentKey?: string | number;
};

/**
 * Scroll container for a thread.
 *
 * It follows the bottom **only while the reader is already there**. Scrolling up
 * during a stream is an explicit act — the user wants to read something — and
 * dragging them back down is the single most common way chat UIs become
 * unusable. When it stops following, a pill appears to offer the trip back.
 */
const AIConversation = ({
  children,
  className,
  contentKey,
}: AIConversationProps) => {
  const shouldReduceMotion = useReducedMotion();
  const viewportRef = useRef<HTMLDivElement | null>(null);
  const [isPinned, setIsPinned] = useState(true);

  const measure = useCallback(() => {
    const viewport = viewportRef.current;
    if (!viewport) {
      return;
    }
    const distance =
      viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight;
    setIsPinned(distance <= BOTTOM_THRESHOLD_PX);
  }, []);

  const scrollToBottom = useCallback((behavior: ScrollBehavior) => {
    const viewport = viewportRef.current;
    if (!viewport) {
      return;
    }
    // Element.scrollTo is missing in jsdom and in a few older engines, and it is
    // not worth throwing over — assigning scrollTop lands in the same place,
    // just without the smoothing.
    if (typeof viewport.scrollTo === "function") {
      viewport.scrollTo({ behavior, top: viewport.scrollHeight });
    } else {
      viewport.scrollTop = viewport.scrollHeight;
    }
    setIsPinned(true);
  }, []);

  // Layout effect, so the jump happens in the same frame the content grew and
  // the reader never sees a flash of the pre-scroll position.
  useLayoutEffect(() => {
    if (isPinned) {
      scrollToBottom(shouldReduceMotion ? "auto" : "smooth");
    }
  }, [contentKey, isPinned, scrollToBottom, shouldReduceMotion]);

  useEffect(() => {
    const viewport = viewportRef.current;
    if (!viewport) {
      return;
    }
    // A resize observer catches content that grows without contentKey changing —
    // an image finishing loading, a details element opening.
    const observer = new ResizeObserver(measure);
    for (const child of Array.from(viewport.children)) {
      observer.observe(child);
    }
    return () => observer.disconnect();
  }, [measure]);

  return (
    <div className={cn("relative min-h-0 w-full", className)}>
      <div
        className="h-full overflow-y-auto overscroll-contain"
        onScroll={measure}
        ref={viewportRef}
      >
        {children}
      </div>

      <AnimatePresence initial={false}>
        {!isPinned && (
          <motion.div
            animate={{ opacity: 1, scale: 1, y: 0 }}
            className="absolute inset-x-0 bottom-3 mx-auto w-fit"
            exit={
              shouldReduceMotion
                ? { opacity: 0, transition: { duration: 0 } }
                : { opacity: 0, scale: 0.96, y: 8 }
            }
            initial={
              shouldReduceMotion
                ? { opacity: 1, scale: 1, y: 0 }
                : { opacity: 0, scale: 0.96, y: 8 }
            }
            transition={shouldReduceMotion ? { duration: 0 } : SPRING_DEFAULT}
          >
            <SmoothButton
              aria-label="Jump to latest"
              className="border border-border bg-background/90 py-1.5 pr-3 pl-2.5 text-xs shadow-sm backdrop-blur"
              onClick={() =>
                scrollToBottom(shouldReduceMotion ? "auto" : "smooth")
              }
              shape="pill"
              size="xs"
              type="button"
              variant="outline"
            >
              <ArrowDown aria-hidden="true" size={13} />
              Jump to latest
            </SmoothButton>
          </motion.div>
        )}
      </AnimatePresence>
    </div>
  );
};

export default AIConversation;

More like this

Faq 2

SmoothUI·Data display

Loading Carousel

Cult UI·Feedback & loaders

Faq 1

SmoothUI·Data display

Message Scroller

shadcn/ui·Other

Testimonials 3

SmoothUI·Cards

Minimal Card

Cult UI·Cards

Github Inline Comments

Eldora UI·Other

Navigation menu

HextaUI·Overlays

Poll Widget

Cult UI·Inputs & forms

Cool Mode

Magic UI·Buttons

Message

shadcn/ui·Other

Terminal

Eldora UI·Media & 3D

Carousel

shadcn/ui·Media & 3D

Marker

HextaUI·Other

Side Panel

Cult UI·Other