{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-smooth-scroll",
  "title": "Smooth scroll",
  "description": "An opt-in wrapper that eases mouse wheel scrolling and leaves every other way of scrolling native.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/smooth-scroll.tsx",
      "content": "'use client';\nimport { type ReactNode, type RefObject, useEffect, useRef } from 'react';\nimport { cn } from '@/lib/utils';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nconst DEFAULT_LERP = 0.12;\nconst MIN_LERP = 0.01;\nconst LINE_HEIGHT = 100 / 3;\nconst REFERENCE_FRAME_MS = 1000 / 60;\nconst SETTLED_DISTANCE = 0.5;\nconst OWN_WRITE_TOLERANCE = 1.5;\nconst IGNORE_ATTRIBUTE = 'data-smooth-scroll-ignore';\n\ntype Scroller = {\n  eventTarget: HTMLElement | Window;\n  boundary: HTMLElement;\n  read: () => number;\n  max: () => number;\n  pageHeight: () => number;\n  isLocked: () => boolean;\n  write: (top: number) => void;\n};\n\nfunction hidesOverflow(element: Element) {\n  const overflowY = getComputedStyle(element).overflowY;\n  const isHidden = overflowY === 'hidden';\n  const isClipped = overflowY === 'clip';\n  return isHidden || isClipped;\n}\n\nfunction windowScroller(): Scroller {\n  const root = document.documentElement;\n\n  return {\n    eventTarget: window,\n    boundary: root,\n    read: () => window.scrollY,\n    max: () => root.scrollHeight - root.clientHeight,\n    pageHeight: () => root.clientHeight,\n    isLocked: () => {\n      const isRootLocked = hidesOverflow(root);\n      const isBodyLocked = hidesOverflow(document.body);\n      return isRootLocked || isBodyLocked;\n    },\n    write: (top) => window.scrollTo({ top, behavior: 'instant' }),\n  };\n}\n\nfunction elementScroller(element: HTMLElement): Scroller {\n  return {\n    eventTarget: element,\n    boundary: element,\n    read: () => element.scrollTop,\n    max: () => element.scrollHeight - element.clientHeight,\n    pageHeight: () => element.clientHeight,\n    isLocked: () => hidesOverflow(element),\n    write: (top) => element.scrollTo({ top, behavior: 'instant' }),\n  };\n}\n\nfunction scrollerOf(container?: RefObject<HTMLElement | null>) {\n  if (!container) {\n    return windowScroller();\n  }\n  const element = container.current;\n  if (!element) {\n    return null;\n  }\n  return elementScroller(element);\n}\n\nfunction clamp(value: number, min: number, max: number) {\n  const atLeastMin = Math.max(min, value);\n  return Math.min(max, atLeastMin);\n}\n\nfunction pixelDeltaOf(event: WheelEvent, scroller: Scroller) {\n  if (event.deltaMode === WheelEvent.DOM_DELTA_LINE) {\n    return event.deltaY * LINE_HEIGHT;\n  }\n  if (event.deltaMode === WheelEvent.DOM_DELTA_PAGE) {\n    return event.deltaY * scroller.pageHeight();\n  }\n  return event.deltaY;\n}\n\nfunction ownsWheel(element: Element, delta: number) {\n  if (element.hasAttribute(IGNORE_ATTRIBUTE)) {\n    return true;\n  }\n  const style = getComputedStyle(element);\n  const isAuto = style.overflowY === 'auto';\n  const isScroll = style.overflowY === 'scroll';\n  const room = element.scrollHeight - element.clientHeight;\n  const isScrollable = isAuto || isScroll;\n  if (!isScrollable || room <= 0) {\n    return false;\n  }\n  if (delta < 0) {\n    return element.scrollTop > 0;\n  }\n  const lastPosition = room - 1;\n  return element.scrollTop < lastPosition;\n}\n\nfunction isInsideOwnScroller(\n  start: EventTarget | null,\n  boundary: HTMLElement,\n  delta: number\n) {\n  let node: Element | null = null;\n  if (start instanceof Element) {\n    node = start;\n  }\n  while (node && node !== boundary) {\n    if (ownsWheel(node, delta)) {\n      return true;\n    }\n    node = node.parentElement;\n  }\n  return false;\n}\n\nfunction isGestureForBrowser(event: WheelEvent) {\n  const sidewaysAmount = Math.abs(event.deltaX);\n  const downwardAmount = Math.abs(event.deltaY);\n  const isHorizontal = sidewaysAmount > downwardAmount;\n  const isPinchZoom = event.ctrlKey;\n  const isSideways = event.shiftKey;\n  const isEmpty = event.deltaY === 0;\n  const isHandled = event.defaultPrevented;\n  return isHorizontal || isPinchZoom || isSideways || isEmpty || isHandled;\n}\n\nfunction attachSmoothScroll(scroller: Scroller, lerp: number) {\n  const easing = clamp(lerp, MIN_LERP, 1);\n  const keptPerFrame = 1 - easing;\n  let current = scroller.read();\n  let target = current;\n  let lastWritten = current;\n  let lastTime = 0;\n  let frameId = 0;\n\n  const stop = () => {\n    cancelAnimationFrame(frameId);\n    frameId = 0;\n  };\n\n  const step = (time: number) => {\n    let elapsed = REFERENCE_FRAME_MS;\n    if (lastTime > 0) {\n      elapsed = time - lastTime;\n    }\n    lastTime = time;\n\n    const frames = elapsed / REFERENCE_FRAME_MS;\n    const kept = Math.pow(keptPerFrame, frames);\n    const factor = 1 - kept;\n    const max = scroller.max();\n    target = clamp(target, 0, max);\n    const gap = target - current;\n    current += gap * factor;\n\n    const remainingGap = target - current;\n    const remaining = Math.abs(remainingGap);\n    const isSettled = remaining < SETTLED_DISTANCE;\n    if (isSettled) {\n      current = target;\n    }\n    lastWritten = current;\n    scroller.write(current);\n\n    if (isSettled) {\n      frameId = 0;\n      return;\n    }\n    frameId = requestAnimationFrame(step);\n  };\n\n  const onWheel = (event: WheelEvent) => {\n    if (isGestureForBrowser(event)) {\n      return;\n    }\n    if (scroller.isLocked()) {\n      return;\n    }\n    const delta = pixelDeltaOf(event, scroller);\n    if (isInsideOwnScroller(event.target, scroller.boundary, delta)) {\n      return;\n    }\n\n    const isAnimating = frameId !== 0;\n    if (!isAnimating) {\n      current = scroller.read();\n      target = current;\n    }\n\n    // At an edge with nothing left to finish, the wheel goes back to the browser: it chains to\n    // the page around a scrolling area, and keeps the overscroll bounce of the page.\n    const max = scroller.max();\n    const pushesPastTop = delta < 0 && target <= 0;\n    const pushesPastBottom = delta > 0 && target >= max;\n    const pushesPastEdge = pushesPastTop || pushesPastBottom;\n    if (pushesPastEdge && !isAnimating) {\n      return;\n    }\n\n    event.preventDefault();\n    const nextTarget = target + delta;\n    target = clamp(nextTarget, 0, max);\n    if (!isAnimating) {\n      lastTime = 0;\n      frameId = requestAnimationFrame(step);\n    }\n  };\n\n  // Keyboard, scrollbar, anchors, focus and find in page scroll natively: when the position\n  // moves without us, the smoothing stops and starts again from there.\n  const onScroll = () => {\n    const position = scroller.read();\n    const offset = position - lastWritten;\n    const drift = Math.abs(offset);\n    if (drift <= OWN_WRITE_TOLERANCE) {\n      return;\n    }\n    stop();\n    current = position;\n    target = position;\n    lastWritten = position;\n  };\n\n  const onWheelEvent = (event: Event) => {\n    if (event instanceof WheelEvent) {\n      onWheel(event);\n    }\n  };\n\n  // The wheel listener cannot be passive: taking over the wheel means cancelling its default.\n  scroller.eventTarget.addEventListener('wheel', onWheelEvent, {\n    passive: false,\n  });\n  scroller.eventTarget.addEventListener('scroll', onScroll, { passive: true });\n\n  return () => {\n    stop();\n    scroller.eventTarget.removeEventListener('wheel', onWheelEvent);\n    scroller.eventTarget.removeEventListener('scroll', onScroll);\n  };\n}\n\nexport type UseSmoothScrollOptions = {\n  container?: RefObject<HTMLElement | null>;\n  lerp?: number;\n  enabled?: boolean;\n};\n\nexport function useSmoothScroll({\n  container,\n  lerp = DEFAULT_LERP,\n  enabled = true,\n}: UseSmoothScrollOptions = {}) {\n  const prefersReducedMotion = usePrefersReducedMotion();\n\n  useEffect(() => {\n    if (!enabled) {\n      return;\n    }\n    if (prefersReducedMotion) {\n      return;\n    }\n    const scroller = scrollerOf(container);\n    if (!scroller) {\n      return;\n    }\n    return attachSmoothScroll(scroller, lerp);\n  }, [container, lerp, enabled, prefersReducedMotion]);\n}\n\nfunction areaClassOf(root: boolean) {\n  if (root) {\n    return '';\n  }\n  return 'relative overflow-y-auto';\n}\n\nexport type SmoothScrollProps = {\n  children: ReactNode;\n  root?: boolean;\n  lerp?: number;\n  enabled?: boolean;\n  className?: string;\n};\n\nexport function SmoothScroll({\n  children,\n  root = false,\n  lerp,\n  enabled = true,\n  className,\n}: SmoothScrollProps) {\n  const containerRef = useRef<HTMLDivElement>(null);\n  const smoothsWindow = root && enabled;\n  const smoothsArea = !root && enabled;\n  useSmoothScroll({ lerp, enabled: smoothsWindow });\n  useSmoothScroll({ container: containerRef, lerp, enabled: smoothsArea });\n\n  return (\n    <div ref={containerRef} className={cn(areaClassOf(root), className)}>\n      {children}\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/smooth-scroll.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}