{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "back-to-top",
  "title": "Back to Top",
  "description": "A floating scroll-to-top button that fades in past a threshold, with an optional progress ring showing how far down the page you are.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/back-to-top/back-to-top.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type BackToTopPosition = \"bottom-right\" | \"bottom-left\" | \"bottom-center\";\nexport type BackToTopSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface BackToTopProps {\n  /** Scroll distance (px) before the button appears. */\n  threshold?: number;\n  showProgress?: boolean;\n  smooth?: boolean;\n  /** Scrollable element to track/scroll; defaults to the window. */\n  containerRef?: React.RefObject<HTMLElement | null>;\n  icon?: React.ReactNode;\n  label?: string;\n  position?: BackToTopPosition;\n  offset?: number;\n  size?: BackToTopSize;\n  theme?: \"dark\" | \"light\";\n  accentColor?: string;\n  /** Positions absolutely within a relative-positioned parent instead of fixed to the viewport — for demos. */\n  contained?: boolean;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { bg: \"#0E0E0E\", border: \"rgba(255,255,255,0.1)\", text: \"#F5F4F1\", track: \"rgba(255,255,255,0.12)\", shadow: \"0 12px 32px rgba(0,0,0,0.45)\" },\n  light: { bg: \"#FFFFFF\", border: \"rgba(10,10,10,0.1)\", text: \"#0A0A0A\", track: \"rgba(10,10,10,0.12)\", shadow: \"0 12px 32px rgba(0,0,0,0.16)\" },\n};\n\nconst SIZES: Record<BackToTopSize, { box: number; icon: number; stroke: number }> = {\n  sm: { box: 38, icon: 15, stroke: 2 },\n  md: { box: 44, icon: 17, stroke: 2.5 },\n  lg: { box: 50, icon: 19, stroke: 3 },\n};\n\nfunction ArrowUpIcon({ size }: { size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d=\"M8 13V3M3.5 7.5L8 3L12.5 7.5\" />\n    </svg>\n  );\n}\n\nfunction getScrollTop(el: HTMLElement | Window) {\n  return el === window ? window.scrollY : (el as HTMLElement).scrollTop;\n}\n\nfunction getScrollableHeight(el: HTMLElement | Window) {\n  if (el === window) return Math.max(1, document.documentElement.scrollHeight - window.innerHeight);\n  const node = el as HTMLElement;\n  return Math.max(1, node.scrollHeight - node.clientHeight);\n}\n\nexport function BackToTop({\n  threshold = 400,\n  showProgress = true,\n  smooth = true,\n  containerRef,\n  icon,\n  label = \"Back to top\",\n  position = \"bottom-right\",\n  offset = 24,\n  size = \"md\",\n  theme = \"dark\",\n  accentColor = \"#F2A841\",\n  contained = false,\n  className,\n}: BackToTopProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const [visible, setVisible] = React.useState(false);\n  const [progress, setProgress] = React.useState(0);\n  // Only meaningful when `contained`: position: absolute inside a\n  // scrollable container is relative to that container's full (tall)\n  // content box, not the slice of it currently on screen — so a plain\n  // `bottom: offset` would pin the button near the bottom of the *content*,\n  // leaving it scrolled out of view the rest of the time. Tracking the\n  // scroll position lets us compute a `top` that keeps it in the visible\n  // corner instead, the way `position: fixed` does for free against the\n  // real viewport.\n  const [containedTop, setContainedTop] = React.useState<number | null>(null);\n  const ticking = React.useRef(false);\n\n  React.useEffect(() => {\n    const target: HTMLElement | Window = containerRef?.current ?? window;\n\n    const measure = () => {\n      const top = getScrollTop(target);\n      setVisible(top > threshold);\n      if (showProgress) setProgress(Math.min(1, top / getScrollableHeight(target)));\n      if (contained && target instanceof HTMLElement) {\n        setContainedTop(top + target.clientHeight - offset - s.box);\n      }\n      ticking.current = false;\n    };\n    const onScroll = () => {\n      if (ticking.current) return;\n      ticking.current = true;\n      requestAnimationFrame(measure);\n    };\n\n    measure();\n    target.addEventListener(\"scroll\", onScroll, { passive: true });\n    window.addEventListener(\"resize\", onScroll);\n    return () => {\n      target.removeEventListener(\"scroll\", onScroll);\n      window.removeEventListener(\"resize\", onScroll);\n    };\n  }, [containerRef, threshold, showProgress, contained, offset, s.box]);\n\n  const scrollToTop = () => {\n    const target = containerRef?.current ?? window;\n    target.scrollTo({ top: 0, behavior: smooth ? \"smooth\" : \"auto\" });\n  };\n\n  const posStyle: React.CSSProperties = {\n    bottom: contained && containedTop !== null ? undefined : offset,\n    top: contained && containedTop !== null ? containedTop : undefined,\n    left: position === \"bottom-left\" ? offset : position === \"bottom-center\" ? \"50%\" : undefined,\n    right: position === \"bottom-right\" ? offset : undefined,\n    transform: position === \"bottom-center\" ? \"translateX(-50%)\" : undefined,\n  };\n\n  const r = (s.box - s.stroke * 2) / 2;\n  const circumference = 2 * Math.PI * r;\n\n  return (\n    <AnimatePresence>\n      {visible && (\n        <motion.button\n          type=\"button\"\n          onClick={scrollToTop}\n          aria-label={label}\n          className={cn(contained ? \"absolute\" : \"fixed\", \"z-[9997] flex cursor-pointer items-center justify-center rounded-full border-none outline-none focus-visible:ring-2\", className)}\n          style={{ ...posStyle, width: s.box, height: s.box, background: p.bg, border: `1px solid ${p.border}`, boxShadow: p.shadow, color: p.text, [\"--tw-ring-color\" as string]: accentColor }}\n          initial={{ opacity: 0, scale: 0.6, y: 12 }}\n          animate={{ opacity: 1, scale: 1, y: 0 }}\n          exit={{ opacity: 0, scale: 0.6, y: 12 }}\n          transition={{ type: \"spring\", stiffness: 420, damping: 30 }}\n          whileHover={{ y: -2 }}\n          whileTap={{ scale: 0.92 }}\n        >\n          {showProgress && (\n            <svg width={s.box} height={s.box} viewBox={`0 0 ${s.box} ${s.box}`} className=\"absolute inset-0\" aria-hidden=\"true\">\n              <circle cx={s.box / 2} cy={s.box / 2} r={r} fill=\"none\" stroke={p.track} strokeWidth={s.stroke} />\n              <circle\n                cx={s.box / 2}\n                cy={s.box / 2}\n                r={r}\n                fill=\"none\"\n                stroke={accentColor}\n                strokeWidth={s.stroke}\n                strokeLinecap=\"round\"\n                strokeDasharray={circumference}\n                strokeDashoffset={circumference * (1 - progress)}\n                transform={`rotate(-90 ${s.box / 2} ${s.box / 2})`}\n                style={{ transition: \"stroke-dashoffset 0.1s linear\" }}\n              />\n            </svg>\n          )}\n          <span className=\"relative flex items-center justify-center\">{icon ?? <ArrowUpIcon size={s.icon} />}</span>\n        </motion.button>\n      )}\n    </AnimatePresence>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}