{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "use-follow-bottom",
  "title": "Use follow bottom",
  "description": "The use-follow-bottom hook used by ndekoui components.",
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/ndeko/hooks/use-follow-bottom.ts",
      "content": "'use client';\n\nimport { useEffect, useRef, useState } from 'react';\n\nconst PINNED_SLACK_PX = 24;\n\nfunction distanceFromBottomOf(element: HTMLElement) {\n  const bottom = element.scrollTop + element.clientHeight;\n  return element.scrollHeight - bottom;\n}\n\ntype FollowBottomOptions = {\n  /**\n   * Keeps `isPinned` in state, for a scroller that offers a way back to its last line. Without it\n   * scrolling never re-renders the component and `isPinned` stays true.\n   */\n  tracksPinned?: boolean;\n};\n\n/**\n * A scroller follows its last line only while a person has not scrolled away from it. Put\n * `scrollerRef` and `updatePinned` (as `onScroll`) on the scrolling element and `contentRef` on the\n * element inside it that grows.\n */\nexport function useFollowBottom({\n  tracksPinned = false,\n}: FollowBottomOptions = {}) {\n  const scrollerRef = useRef<HTMLDivElement>(null);\n  const contentRef = useRef<HTMLDivElement>(null);\n  const isPinnedRef = useRef(true);\n  const lastTopRef = useRef(0);\n  const [isPinned, setIsPinned] = useState(true);\n\n  useEffect(() => {\n    const scroller = scrollerRef.current;\n    const content = contentRef.current;\n    if (!scroller || !content) {\n      return;\n    }\n    const observer = new ResizeObserver(() => {\n      if (isPinnedRef.current) {\n        scroller.scrollTop = scroller.scrollHeight;\n      }\n    });\n    observer.observe(content);\n    return () => observer.disconnect();\n  }, []);\n\n  function pin(nextPinned: boolean) {\n    isPinnedRef.current = nextPinned;\n    if (tracksPinned) {\n      setIsPinned(nextPinned);\n    }\n  }\n\n  /**\n   * Only a move upward unpins. Content that grows between our own scroll to the bottom and the\n   * event it raises would otherwise read as a person leaving the last line.\n   */\n  function updatePinned(event: React.UIEvent<HTMLDivElement>) {\n    const scroller = event.currentTarget;\n    const distance = distanceFromBottomOf(scroller);\n    const isNearBottom = distance <= PINNED_SLACK_PX;\n    const movedUp = scroller.scrollTop < lastTopRef.current;\n    lastTopRef.current = scroller.scrollTop;\n    if (isNearBottom) {\n      pin(true);\n      return;\n    }\n    if (movedUp) {\n      pin(false);\n    }\n  }\n\n  function jumpToBottom() {\n    const scroller = scrollerRef.current;\n    if (!scroller) {\n      return;\n    }\n    pin(true);\n    scroller.scrollTo({ top: scroller.scrollHeight, behavior: 'smooth' });\n  }\n\n  return { scrollerRef, contentRef, isPinned, updatePinned, jumpToBottom };\n}\n",
      "type": "registry:hook"
    }
  ],
  "categories": [
    "hooks"
  ],
  "type": "registry:hook"
}