{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "carousel",
  "type": "registry:component",
  "title": "Carousel",
  "description": "Scroll-snap slider with arrow buttons, dot indicators, keyboard control and optional autoplay.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "components",
    "carousel",
    "slider",
    "scroll-snap",
    "gallery",
    "autoplay"
  ],
  "docs": "https://hyperblur.dev/effects/carousel",
  "files": [
    {
      "path": "registry/hyperblur/carousel.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/carousel.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CarouselProps extends React.HTMLAttributes<HTMLDivElement> {\n  children: React.ReactNode;\n  autoplay?: number;\n  loop?: boolean;\n  gap?: number;\n  slidesPerView?: number;\n}\n\nconst css = `\n.hb-cr { position: relative; width: 100%; }\n.hb-cr-track {\n  position: relative;\n  display: flex;\n  gap: var(--hb-gap);\n  height: 100%;\n  overflow-x: auto;\n  overscroll-behavior-x: contain;\n  scroll-snap-type: x mandatory;\n  scrollbar-width: none;\n  border-radius: inherit;\n  outline: none;\n}\n.hb-cr-track::-webkit-scrollbar { display: none; }\n.hb-cr-track:focus-visible { box-shadow: 0 0 0 2px #2f7bff; }\n.hb-cr-slide {\n  flex: 0 0 calc((100% - (var(--hb-n) - 1) * var(--hb-gap)) / var(--hb-n));\n  display: flex;\n  min-width: 0;\n  scroll-snap-align: start;\n}\n.hb-cr-slide > * { flex: 1 1 auto; min-width: 0; }\n.hb-cr-btn {\n  position: absolute;\n  top: 50%;\n  z-index: 1;\n  display: grid;\n  place-items: center;\n  width: 32px;\n  height: 32px;\n  margin: -16px 0 0;\n  padding: 0;\n  border: 1px solid rgba(255, 255, 255, .2);\n  border-radius: 50%;\n  background: rgba(10, 10, 10, .6);\n  color: #fff;\n  cursor: pointer;\n  backdrop-filter: blur(6px);\n  transition: background .2s ease, opacity .2s ease;\n}\n.hb-cr-btn:hover:not(:disabled) { background: rgba(47, 123, 255, .85); }\n.hb-cr-btn:focus-visible { outline: 2px solid #2f7bff; outline-offset: 2px; }\n.hb-cr-btn:disabled { opacity: .35; cursor: default; }\n.hb-cr-prev { left: 8px; }\n.hb-cr-next { right: 8px; }\n.hb-cr-dots {\n  position: absolute;\n  left: 50%;\n  bottom: 8px;\n  z-index: 1;\n  display: flex;\n  gap: 6px;\n  padding: 5px 8px;\n  border-radius: 999px;\n  background: rgba(10, 10, 10, .5);\n  transform: translateX(-50%);\n}\n.hb-cr-dot {\n  width: 8px;\n  height: 8px;\n  padding: 0;\n  border: 0;\n  border-radius: 999px;\n  background: rgba(255, 255, 255, .4);\n  cursor: pointer;\n  transition: background .2s ease, width .25s ease;\n}\n.hb-cr-dot:hover { background: rgba(255, 255, 255, .7); }\n.hb-cr-dot:focus-visible { outline: 2px solid #2f7bff; outline-offset: 2px; }\n.hb-cr-dot[aria-current=\"true\"] { width: 18px; background: #fff; }\n@media (prefers-reduced-motion: reduce) {\n  .hb-cr-btn, .hb-cr-dot { transition: none; }\n}\n`;\n\nexport function Carousel({\n  children,\n  autoplay = 0,\n  loop = true,\n  gap = 16,\n  slidesPerView = 1,\n  className = \"\",\n  style,\n  onKeyDown,\n  onMouseEnter,\n  onMouseLeave,\n  onFocus,\n  onBlur,\n  ...rest\n}: CarouselProps) {\n  const trackRef = React.useRef<HTMLDivElement>(null);\n  const activeRef = React.useRef(0);\n  const pausedRef = React.useRef(false);\n  const [active, setActive] = React.useState(0);\n\n  const slides = React.Children.toArray(children);\n  const count = slides.length;\n  const perView = Math.max(1, slidesPerView);\n  const pages = Math.max(1, count - Math.floor(perView) + 1);\n\n  const scrollToIndex = React.useCallback(\n    (index: number) => {\n      const el = trackRef.current;\n      if (!el) return;\n      const target = el.children[Math.min(Math.max(index, 0), count - 1)] as HTMLElement | undefined;\n      if (!target) return;\n      const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n      el.scrollTo({ left: target.offsetLeft, behavior: reduce ? \"auto\" : \"smooth\" });\n    },\n    [count],\n  );\n\n  const next = React.useCallback(() => {\n    const i = activeRef.current + 1;\n    if (i < pages) scrollToIndex(i);\n    else if (loop) scrollToIndex(0);\n  }, [pages, loop, scrollToIndex]);\n\n  const prev = React.useCallback(() => {\n    const i = activeRef.current - 1;\n    if (i >= 0) scrollToIndex(i);\n    else if (loop) scrollToIndex(pages - 1);\n  }, [pages, loop, scrollToIndex]);\n\n  React.useEffect(() => {\n    const el = trackRef.current;\n    if (!el) return;\n    let frame = 0;\n\n    const measure = () => {\n      frame = 0;\n      const kids = el.children;\n      const max = el.scrollWidth - el.clientWidth;\n      let best = 0;\n      if (max > 1 && el.scrollLeft >= max - 1) {\n        best = pages - 1;\n      } else {\n        let bestDistance = Infinity;\n        for (let i = 0; i < kids.length; i++) {\n          const d = Math.abs((kids[i] as HTMLElement).offsetLeft - el.scrollLeft);\n          if (d < bestDistance) {\n            bestDistance = d;\n            best = i;\n          }\n        }\n      }\n      best = Math.min(best, pages - 1);\n      activeRef.current = best;\n      setActive(best);\n    };\n\n    const schedule = () => {\n      if (!frame) frame = requestAnimationFrame(measure);\n    };\n\n    schedule();\n    el.addEventListener(\"scroll\", schedule, { passive: true });\n    window.addEventListener(\"resize\", schedule);\n    return () => {\n      if (frame) cancelAnimationFrame(frame);\n      el.removeEventListener(\"scroll\", schedule);\n      window.removeEventListener(\"resize\", schedule);\n    };\n  }, [pages, count]);\n\n  React.useEffect(() => {\n    if (!(autoplay > 0) || pages < 2) return;\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) return;\n    const id = window.setInterval(() => {\n      if (pausedRef.current || document.hidden) return;\n      next();\n    }, autoplay);\n    return () => window.clearInterval(id);\n  }, [autoplay, pages, next]);\n\n  function handleKeyDown(e: React.KeyboardEvent<HTMLDivElement>) {\n    onKeyDown?.(e);\n    if (e.defaultPrevented) return;\n    if (e.key === \"ArrowRight\") {\n      e.preventDefault();\n      next();\n    } else if (e.key === \"ArrowLeft\") {\n      e.preventDefault();\n      prev();\n    } else if (e.key === \"Home\") {\n      e.preventDefault();\n      scrollToIndex(0);\n    } else if (e.key === \"End\") {\n      e.preventDefault();\n      scrollToIndex(pages - 1);\n    }\n  }\n\n  function handleEnter(e: React.MouseEvent<HTMLDivElement>) {\n    onMouseEnter?.(e);\n    pausedRef.current = true;\n  }\n\n  function handleLeave(e: React.MouseEvent<HTMLDivElement>) {\n    onMouseLeave?.(e);\n    pausedRef.current = false;\n  }\n\n  function handleFocus(e: React.FocusEvent<HTMLDivElement>) {\n    onFocus?.(e);\n    pausedRef.current = true;\n  }\n\n  function handleBlur(e: React.FocusEvent<HTMLDivElement>) {\n    onBlur?.(e);\n    pausedRef.current = false;\n  }\n\n  const vars = {\n    \"--hb-gap\": `${gap}px`,\n    \"--hb-n\": perView,\n    ...style,\n  } as React.CSSProperties;\n\n  return (\n    <div\n      className={`hb-cr ${className}`}\n      style={vars}\n      role=\"region\"\n      aria-roledescription=\"carousel\"\n      onKeyDown={handleKeyDown}\n      onMouseEnter={handleEnter}\n      onMouseLeave={handleLeave}\n      onFocus={handleFocus}\n      onBlur={handleBlur}\n      {...rest}\n    >\n      <style href=\"hb-carousel\" precedence=\"default\">{css}</style>\n      <div className=\"hb-cr-track\" ref={trackRef} tabIndex={0} aria-live={autoplay > 0 ? \"off\" : \"polite\"}>\n        {slides.map((slide, i) => (\n          <div\n            className=\"hb-cr-slide\"\n            key={i}\n            role=\"group\"\n            aria-roledescription=\"slide\"\n            aria-label={`${i + 1} of ${count}`}\n          >\n            {slide}\n          </div>\n        ))}\n      </div>\n      {pages > 1 && (\n        <>\n          <button\n            type=\"button\"\n            className=\"hb-cr-btn hb-cr-prev\"\n            aria-label=\"Previous slide\"\n            disabled={!loop && active === 0}\n            onClick={prev}\n          >\n            <svg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n              <path d=\"M15 5l-7 7 7 7\" />\n            </svg>\n          </button>\n          <button\n            type=\"button\"\n            className=\"hb-cr-btn hb-cr-next\"\n            aria-label=\"Next slide\"\n            disabled={!loop && active === pages - 1}\n            onClick={next}\n          >\n            <svg width=\"16\" height=\"16\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n              <path d=\"M9 5l7 7-7 7\" />\n            </svg>\n          </button>\n          <div className=\"hb-cr-dots\">\n            {Array.from({ length: pages }, (_, i) => (\n              <button\n                type=\"button\"\n                key={i}\n                className=\"hb-cr-dot\"\n                aria-label={`Go to slide ${i + 1}`}\n                aria-current={i === active ? \"true\" : undefined}\n                onClick={() => scrollToIndex(i)}\n              />\n            ))}\n          </div>\n        </>\n      )}\n    </div>\n  );\n}\n"
    }
  ]
}