{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "message-scroller",
  "title": "Message scroller",
  "description": "A chat viewport that follows new messages while the reader is at the bottom. Adapted from beUI (MIT).",
  "dependencies": [
    "motion@^12.23.12"
  ],
  "registryDependencies": [
    "@khaeli/utils"
  ],
  "files": [
    {
      "path": "registry/khaeli/ui/message-scroller.tsx",
      "content": "\"use client\"\n\n/*\n * beUI, https://github.com/starc007/ui-components\n * MIT License, Copyright (c) 2026 Saurabh Chauhan\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n// Adapted from beUI (MIT) https://beui.dev — components/agents/message-scroller.\n// Kept from beUI: the reader-aware viewport — follow-state tracking against a\n// configurable live-edge threshold, the programmatic-scroll guard, the\n// wheel/touch/key \"leave the live edge\" intent handling, and the transcript's\n// role=log semantics. Dropped: the preview-rail navigation (this product\n// anchors long answers at their question instead, so the consumer owns the\n// positioning via `followOutput={false}` + `viewportRef`/`contentRef`).\n\nimport {\n  type ComponentPropsWithRef,\n  type Ref,\n  useCallback,\n  useEffect,\n  useLayoutEffect,\n  useRef,\n} from \"react\"\nimport { useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\n\nexport interface MessageScrollerProps extends ComponentPropsWithRef<\"div\"> {\n  /** Keep streamed output pinned while the reader remains near the end. */\n  followOutput?: boolean\n  /** Distance from the end that still counts as following the output. */\n  followThreshold?: number\n  /** Smoothly follow growing content. */\n  smooth?: boolean\n  /** Reports when the reader leaves or returns to the live edge. */\n  onFollowChange?: (following: boolean) => void\n  /** Accessible label for the scrollable transcript. */\n  label?: string\n  /** Marks the transcript as waiting for more streamed content. */\n  busy?: boolean\n  viewportClassName?: string\n  contentClassName?: string\n  viewportRef?: Ref<HTMLElement>\n  contentRef?: Ref<HTMLDivElement>\n}\n\nexport function MessageScroller({\n  followOutput = true,\n  followThreshold = 56,\n  smooth = true,\n  onFollowChange,\n  label = \"Conversation\",\n  busy,\n  viewportClassName,\n  contentClassName,\n  viewportRef: externalViewportRef,\n  contentRef: externalContentRef,\n  className,\n  children,\n  ...props\n}: MessageScrollerProps) {\n  const reduce = useReducedMotion() ?? false\n  const viewportRef = useRef<HTMLElement>(null)\n  const contentRef = useRef<HTMLDivElement>(null)\n  const followingRef = useRef(followOutput)\n  const programmaticScrollRef = useRef(false)\n  const scrollTimerRef = useRef<number | undefined>(undefined)\n  const frameRef = useRef<number | undefined>(undefined)\n\n  const setViewportRef = useCallback(\n    (node: HTMLElement | null) => {\n      viewportRef.current = node\n      if (typeof externalViewportRef === \"function\") externalViewportRef(node)\n      else if (externalViewportRef) externalViewportRef.current = node\n    },\n    [externalViewportRef]\n  )\n\n  const setContentRef = useCallback(\n    (node: HTMLDivElement | null) => {\n      contentRef.current = node\n      if (typeof externalContentRef === \"function\") externalContentRef(node)\n      else if (externalContentRef) externalContentRef.current = node\n    },\n    [externalContentRef]\n  )\n\n  const setFollowing = useCallback(\n    (next: boolean) => {\n      if (followingRef.current === next) return\n      followingRef.current = next\n      onFollowChange?.(next)\n    },\n    [onFollowChange]\n  )\n\n  const scrollToEnd = useCallback((behavior: ScrollBehavior) => {\n    const viewport = viewportRef.current\n    if (!viewport) return\n\n    programmaticScrollRef.current = true\n    viewport.scrollTo({ top: viewport.scrollHeight, behavior })\n    if (scrollTimerRef.current) window.clearTimeout(scrollTimerRef.current)\n    scrollTimerRef.current = window.setTimeout(\n      () => {\n        programmaticScrollRef.current = false\n      },\n      behavior === \"smooth\" ? 320 : 0\n    )\n  }, [])\n\n  const handleScroll = useCallback(() => {\n    const viewport = viewportRef.current\n    if (!viewport || programmaticScrollRef.current) return\n\n    const distance =\n      viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight\n    setFollowing(distance <= followThreshold)\n  }, [followThreshold, setFollowing])\n\n  const leaveLiveEdge = useCallback(() => {\n    programmaticScrollRef.current = false\n  }, [])\n\n  useLayoutEffect(() => {\n    followingRef.current = followOutput\n    if (!followOutput) return\n\n    frameRef.current = requestAnimationFrame(() => scrollToEnd(\"auto\"))\n    return () => {\n      if (frameRef.current) cancelAnimationFrame(frameRef.current)\n    }\n  }, [followOutput, scrollToEnd])\n\n  useEffect(() => {\n    const content = contentRef.current\n    if (!content || typeof ResizeObserver === \"undefined\") return\n\n    const observer = new ResizeObserver(() => {\n      if (!followOutput || !followingRef.current) return\n      scrollToEnd(reduce || !smooth ? \"auto\" : \"smooth\")\n    })\n    observer.observe(content)\n\n    return () => observer.disconnect()\n  }, [followOutput, reduce, scrollToEnd, smooth])\n\n  useEffect(\n    () => () => {\n      if (scrollTimerRef.current) window.clearTimeout(scrollTimerRef.current)\n      if (frameRef.current) cancelAnimationFrame(frameRef.current)\n    },\n    []\n  )\n\n  return (\n    <div\n      data-slot=\"message-scroller\"\n      className={cn(\"min-h-0\", className)}\n      {...props}\n    >\n      <section\n        ref={setViewportRef}\n        aria-label={label}\n        // A scrolling region with no focusable content cannot be scrolled from\n        // the keyboard unless it takes focus itself.\n        tabIndex={0}\n        onScroll={handleScroll}\n        onWheel={leaveLiveEdge}\n        onTouchStart={leaveLiveEdge}\n        onKeyDown={(event) => {\n          if ([\"ArrowUp\", \"PageUp\", \"Home\"].includes(event.key)) {\n            leaveLiveEdge()\n          }\n        }}\n        className={cn(\n          \"h-full overflow-y-auto overscroll-contain outline-none [overflow-anchor:none] [scrollbar-gutter:stable] focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring\",\n          viewportClassName\n        )}\n      >\n        <div\n          ref={setContentRef}\n          role=\"log\"\n          aria-live=\"polite\"\n          aria-relevant=\"additions text\"\n          aria-busy={busy}\n          className={contentClassName}\n        >\n          {children}\n        </div>\n      </section>\n    </div>\n  )\n}\n",
      "type": "registry:ui"
    }
  ],
  "docs": "Install @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.",
  "type": "registry:ui"
}