{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "carousel",
  "type": "registry:ui",
  "title": "Fluid Carousel",
  "description": "Accessible touch-swipeable slide carousel with directional spring animations, keyboard arrow navigation, and pagination indicators.",
  "author": "Aditya Ojha <https://akoder.xyz>",
  "categories": [
    "carousel",
    "slider",
    "motion",
    "interactive"
  ],
  "dependencies": [
    "motion"
  ],
  "meta": {
    "docs": "https://akoder.xyz/components/carousel",
    "source": "https://github.com/AdityaKodez/adityaojha/blob/main/components/ui/carousel.tsx"
  },
  "files": [
    {
      "path": "components/ui/carousel.tsx",
      "content": "\"use client\";\n\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport {\n  type KeyboardEvent,\n  type ReactNode,\n  type TouchEvent,\n  useCallback,\n  useMemo,\n  useRef,\n  useState,\n} from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\nexport type CarouselItem = {\n  id: string;\n  /** Slide heading, also used for screen-reader labels. */\n  title: string;\n  description?: string;\n  content: ReactNode;\n};\n\nexport type CarouselProps = {\n  items: CarouselItem[];\n  /** Accessible name for the carousel region. */\n  ariaLabel?: string;\n  className?: string;\n};\n\n/** Minimum horizontal travel (px) before a touch counts as a swipe. */\nconst SWIPE_THRESHOLD = 48;\n\nexport function Carousel({\n  items,\n  ariaLabel = \"Carousel\",\n  className,\n}: CarouselProps) {\n  const reduceMotion = useReducedMotion();\n  const [[index, direction], setSlide] = useState<[number, number]>([0, 0]);\n  const touchStart = useRef<{ x: number; y: number } | null>(null);\n\n  const total = items.length;\n\n  const goToIndex = useCallback((next: number) => {\n    setSlide(([current]) => [next, next > current ? 1 : -1]);\n  }, []);\n\n  const showNext = useCallback(() => {\n    setSlide(([current]) =>\n      total === 0 ? [current, 1] : [(current + 1) % total, 1],\n    );\n  }, [total]);\n\n  const showPrevious = useCallback(() => {\n    setSlide(([current]) =>\n      total === 0 ? [current, -1] : [(current - 1 + total) % total, -1],\n    );\n  }, [total]);\n\n  const handleKeyDown = useCallback(\n    (event: KeyboardEvent<HTMLDivElement>) => {\n      if (event.key === \"ArrowRight\") {\n        event.preventDefault();\n        showNext();\n      } else if (event.key === \"ArrowLeft\") {\n        event.preventDefault();\n        showPrevious();\n      } else if (event.key === \"Home\") {\n        event.preventDefault();\n        goToIndex(0);\n      } else if (event.key === \"End\") {\n        event.preventDefault();\n        goToIndex(total - 1);\n      }\n    },\n    [goToIndex, showNext, showPrevious, total],\n  );\n\n  const handleTouchStart = useCallback((event: TouchEvent<HTMLDivElement>) => {\n    const touch = event.touches[0];\n    touchStart.current = touch ? { x: touch.clientX, y: touch.clientY } : null;\n  }, []);\n\n  // Only treat the gesture as a swipe when it is mostly horizontal, so vertical\n  // page scrolling on touch devices is never hijacked.\n  const handleTouchEnd = useCallback(\n    (event: TouchEvent<HTMLDivElement>) => {\n      const start = touchStart.current;\n      touchStart.current = null;\n      const touch = event.changedTouches[0];\n      if (!start || !touch) return;\n\n      const deltaX = touch.clientX - start.x;\n      const deltaY = touch.clientY - start.y;\n      if (\n        Math.abs(deltaX) < SWIPE_THRESHOLD ||\n        Math.abs(deltaX) <= Math.abs(deltaY)\n      )\n        return;\n\n      if (deltaX < 0) showNext();\n      else showPrevious();\n    },\n    [showNext, showPrevious],\n  );\n\n  const variants = useMemo(\n    () => ({\n      enter: (dir: number) => ({\n        opacity: 0,\n        x: reduceMotion ? 0 : dir >= 0 ? 40 : -40,\n      }),\n      center: { opacity: 1, x: 0 },\n      exit: (dir: number) => ({\n        opacity: 0,\n        x: reduceMotion ? 0 : dir >= 0 ? -40 : 40,\n      }),\n    }),\n    [reduceMotion],\n  );\n\n  if (total === 0) return null;\n\n  const active = items[index] ?? items[0];\n\n  return (\n    <div className={cn(\"w-full\", className)}>\n      <div\n        role=\"group\"\n        aria-roledescription=\"carousel\"\n        aria-label={ariaLabel}\n        tabIndex={0}\n        onKeyDown={handleKeyDown}\n        className=\"rounded-md outline-none focus-visible:ring-1 focus-visible:ring-ring/50\"\n      >\n        {/* Viewport — slides share one grid cell so height never collapses\n            mid-transition. */}\n        <div\n          className=\"relative grid overflow-hidden px-6\"\n          onTouchStart={handleTouchStart}\n          onTouchEnd={handleTouchEnd}\n        >\n          <AnimatePresence initial={false} custom={direction} mode=\"popLayout\">\n            <motion.div\n              key={active.id}\n              custom={direction}\n              variants={variants}\n              initial=\"enter\"\n              animate=\"center\"\n              exit=\"exit\"\n              transition={{\n                duration: reduceMotion ? 0 : 0.24,\n                ease: [0.22, 1, 0.36, 1],\n              }}\n              role=\"group\"\n              aria-roledescription=\"slide\"\n              aria-label={`${index + 1} of ${total}: ${active.title}`}\n              className=\"col-start-1 row-start-1 w-full\"\n            >\n              <div className=\"mb-5 space-y-1\">\n                <h3 className=\"text-sm font-medium\">{active.title}</h3>\n                {active.description ? (\n                  <p className=\"text-sm text-muted-foreground\">\n                    {active.description}\n                  </p>\n                ) : null}\n              </div>\n              {active.content}\n            </motion.div>\n          </AnimatePresence>\n        </div>\n      </div>\n\n      {/* Controls: position dots on the left, counter + arrows on the right. */}\n      <div className=\"mt-5 flex items-center justify-between gap-3 px-6\">\n        <div className=\"flex items-center gap-0.5\">\n          {items.map((item, itemIndex) => (\n            <button\n              key={item.id}\n              type=\"button\"\n              onClick={() => goToIndex(itemIndex)}\n              aria-label={`Show example: ${item.title}`}\n              aria-current={itemIndex === index}\n              className=\"group flex h-9 items-center px-1 outline-none sm:h-8\"\n            >\n              <span\n                aria-hidden=\"true\"\n                className={cn(\n                  \"h-1.5 rounded-full transition-all duration-200 motion-reduce:transition-none\",\n                  itemIndex === index\n                    ? \"w-5 bg-foreground/80\"\n                    : \"w-1.5 bg-muted-foreground/30 group-hover:bg-muted-foreground/60\",\n                )}\n              />\n            </button>\n          ))}\n        </div>\n\n        <div className=\"flex items-center gap-1.5\">\n          <span\n            aria-hidden=\"true\"\n            className=\"text-xs tabular-nums text-muted-foreground mr-1\"\n          >\n            {pad(index + 1)} / {pad(total)}\n          </span>\n\n        </div>\n      </div>\n\n      <p aria-live=\"polite\" className=\"sr-only\">\n        Example {index + 1} of {total}: {active.title}\n      </p>\n    </div>\n  );\n}\n\nfunction pad(value: number) {\n  return String(value).padStart(2, \"0\");\n}\n",
      "type": "registry:ui"
    }
  ],
  "docs": "## Installation\n\n```bash\nnpx shadcn@latest add @akoder/carousel\n```\n\n## Usage\n\n```tsx\nimport { Carousel } from \"@/components/ui/carousel\";\n\nexport function SlideShow() {\n  return (\n    <Carousel\n      items={[\n        { id: \"1\", title: \"Slide 1\", content: <div>Content 1</div> },\n        { id: \"2\", title: \"Slide 2\", content: <div>Content 2</div> },\n      ]}\n    />\n  );\n}\n```"
}