{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "carousel",
  "title": "Carousel",
  "description": "Slides seen a few at a time, moved by a drag or a swipe, Previous and Next, the indicators and the arrow keys. The track scrolls a row along under a window; the deck stacks the slides as cards and throws the top one off.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/button.json",
    "https://kinoui.com/r/animated-number.json",
    "https://kinoui.com/r/announce.json",
    "https://kinoui.com/r/deck.json",
    "https://kinoui.com/r/resize.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/carousel.tsx",
      "content": "'use client'\n\nimport { AnimatedNumber } from '@/registry/components/kino/animated-number'\nimport { buttonVariants } from '@/registry/components/kino/button'\nimport { IconPlaceholder } from '@/registry/icons/icon-placeholder'\nimport { announce } from '@/registry/lib/announce'\nimport { deck } from '@/registry/lib/deck'\nimport { withoutTransition } from '@/registry/lib/resize'\nimport type { ChangeDetails } from '@ui-sdk/core'\nimport type { CarouselPageChangeReason } from '@ui-sdk/core/carousel'\nimport {\n  CarouselContent as CarouselContentPrimitive,\n  CarouselIndicatorGroup as CarouselIndicatorGroupPrimitive,\n  CarouselIndicator as CarouselIndicatorPrimitive,\n  CarouselItem as CarouselItemPrimitive,\n  CarouselNext as CarouselNextPrimitive,\n  CarouselPrevious as CarouselPreviousPrimitive,\n  Carousel as CarouselPrimitive,\n  CarouselViewport as CarouselViewportPrimitive,\n} from '@ui-sdk/react/carousel'\nimport { createParts, type PartProps } from '@ui-sdk/react/primitive'\nimport type { VariantProps } from 'class-variance-authority'\nimport {\n  Children,\n  createContext,\n  type CSSProperties,\n  isValidElement,\n  type ReactNode,\n  use,\n  useEffect,\n  useLayoutEffect,\n  useMemo,\n  useRef,\n  useState,\n} from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * A carousel of slides, moved by a drag, Previous and Next, the indicators\n * and the arrow keys. The track is ui-sdk's Carousel: a row that scrolls\n * natively under a window, a page at a time. The deck stacks the slides as\n * cards and throws the top one off, on the deck primitive. Both take the\n * same props and parts, and page from 1.\n */\n\ntype CarouselVariant = 'track' | 'deck'\n\ninterface CarouselContextValue {\n  variant: CarouselVariant\n  loop: boolean\n  /** The page in view, from 1. */\n  page: number\n  /** How many pages there are, or null before the slides are measured. */\n  pageCount: number | null\n  /** How many slides there are. */\n  slides: number\n  setSlides: (slides: number) => void\n}\n\nconst CarouselContext = createContext<CarouselContextValue | null>(null)\n\nfunction useCarousel() {\n  const context = use(CarouselContext)\n  if (!context) throw new Error('Carousel parts must be inside <Carousel>.')\n  return context\n}\n\n/** A Kino part's props: ui-sdk's, with a string class name and plain children. */\ntype CarouselPartProps<Tag extends 'div' | 'button'> = Omit<\n  PartProps<Tag>,\n  'className' | 'children' | 'style'\n> & {\n  className?: string\n  children?: ReactNode\n  style?: CSSProperties\n}\n\ntype CarouselProps = Omit<\n  CarouselPartProps<'div'>,\n  'defaultValue' | 'onChange' | 'render'\n> & {\n  /** The page in view, from 1. Pass it to control the carousel. */\n  page?: number\n  defaultPage?: number\n  /** Called when the page wants to change, with why in `details.reason`; `details.cancel()` stays. */\n  onPageChange?: (page: number, details: ChangeDetails<CarouselPageChangeReason>) => void\n  /** Whether the last page goes on to the first. The deck always does. */\n  loop?: boolean\n  /** `track` scrolls a row along; `deck` stacks the slides as cards. */\n  variant?: CarouselVariant\n}\n\nfunction Carousel({\n  variant = 'track',\n  page,\n  defaultPage,\n  onPageChange,\n  loop = false,\n  className,\n  children,\n  ...props\n}: CarouselProps) {\n  const [slides, setSlides] = useState(0)\n  const scope = (state: { page: number; pageCount: number | null }) => (\n    <CarouselScope\n      variant={variant}\n      loop={loop || variant === 'deck'}\n      page={state.page}\n      pageCount={state.pageCount}\n      slides={slides}\n      setSlides={setSlides}\n    >\n      {children}\n    </CarouselScope>\n  )\n  const rootClassName = cn(\n    'group/carousel relative flex min-w-0 flex-col gap-(--surface-gap)',\n    className,\n  )\n  if (variant === 'deck') {\n    return (\n      <DeckRoot\n        data-variant='deck'\n        page={page}\n        defaultPage={defaultPage}\n        onPageChange={onPageChange}\n        className={rootClassName}\n        {...props}\n      >\n        {scope}\n      </DeckRoot>\n    )\n  }\n  return (\n    <CarouselPrimitive\n      data-variant='track'\n      page={page}\n      defaultPage={defaultPage}\n      onPageChange={onPageChange}\n      loop={loop}\n      // A page is as many slides as show, so Next moves a whole view.\n      slidesPerPage='auto'\n      className={rootClassName}\n      {...props}\n    >\n      {scope}\n    </CarouselPrimitive>\n  )\n}\n\n/** Shares the carousel's state with its parts; the deck also says which card is on top once it changes. */\nfunction CarouselScope({\n  children,\n  variant,\n  loop,\n  page,\n  pageCount,\n  slides,\n  setSlides,\n}: CarouselContextValue & { children: ReactNode }) {\n  const value = useMemo(\n    () => ({ variant, loop, page, pageCount, slides, setSlides }),\n    [variant, loop, page, pageCount, slides, setSlides],\n  )\n  // ui-sdk's slides are a live region; the deck's cards under the top one are\n  // inert, so it says the card that comes up instead. Nothing on load.\n  const announced = useRef<number | null>(null)\n  useEffect(() => {\n    if (variant !== 'deck' || pageCount === null) return\n    if (announced.current === null) announced.current = page\n    if (announced.current === page) return\n    announced.current = page\n    announce(`Slide ${page} of ${pageCount}`)\n  }, [variant, page, pageCount])\n  return <CarouselContext value={value}>{children}</CarouselContext>\n}\n\n/*\n * A tab stop while the slides in view hold nothing that takes focus, so the\n * arrow keys reach it: ui-sdk's on the track, the deck's own on the deck. Its\n * outline sits outside, since the slides would cover one inside. The track\n * scrolls, which clips what it holds: room above and below keeps a slide's\n * shadow and focus ring whole.\n */\nconst viewportClassName =\n  'group/carousel-viewport -my-1 scrollbar-none rounded-(--surface-radius) py-1 focus-visible:outline-3 focus-visible:outline-offset-2 focus-visible:outline-ring/50 data-dragging:select-none'\n\nfunction CarouselViewport({ className, ...props }: CarouselPartProps<'div'>) {\n  const { variant } = useCarousel()\n  if (variant === 'deck') {\n    return <DeckViewport className={cn(viewportClassName, className)} {...props} />\n  }\n  return (\n    <CarouselViewportPrimitive className={cn(viewportClassName, className)} {...props} />\n  )\n}\n\n/** Counts the slides for the parts that name pages by them. */\nfunction useSlideCount(count: number) {\n  const { setSlides } = useCarousel()\n  useLayoutEffect(() => {\n    setSlides(count)\n  }, [count, setSlides])\n}\n\nfunction CarouselContent({ className, children, ...props }: CarouselPartProps<'div'>) {\n  const { variant } = useCarousel()\n  const slides = Children.toArray(children)\n  useSlideCount(slides.length)\n  if (variant === 'deck') {\n    return (\n      <DeckContent\n        className={cn(\n          'grid touch-pan-y pb-[calc(var(--carousel-recede,0.75rem)*2)] group-data-dragging/carousel-viewport:select-none',\n          className,\n        )}\n        {...props}\n      >\n        {slides.map((child, index) => (\n          <ItemIndexProvider\n            key={keyOf(child, index)}\n            index={index}\n            count={slides.length}\n          >\n            {child}\n          </ItemIndexProvider>\n        ))}\n      </DeckContent>\n    )\n  }\n  return (\n    <CarouselContentPrimitive className={className} {...props}>\n      {children}\n    </CarouselContentPrimitive>\n  )\n}\n\nfunction CarouselItem({ className, ...props }: CarouselPartProps<'div'>) {\n  const { variant } = useCarousel()\n  if (variant === 'deck') return <DeckItem className={className} {...props} />\n  return (\n    <CarouselItemPrimitive className={cn('min-w-0 basis-full', className)} {...props} />\n  )\n}\n\ntype CarouselStepProps = CarouselPartProps<'button'> &\n  Pick<VariantProps<typeof buttonVariants>, 'variant' | 'size'>\n\n/** Whether a page holds more than one slide, so Previous and Next move several. */\nfunction severalPerPage(slides: number, pageCount: number | null) {\n  return pageCount !== null && pageCount > 0 && slides > pageCount\n}\n\n/*\n * The Button's look on ui-sdk's own element, which stays focusable while it\n * is off at an end, so the way back works from it.\n */\nfunction CarouselPrevious({\n  className,\n  variant = 'outline',\n  size = 'icon-sm',\n  children,\n  ...props\n}: CarouselStepProps) {\n  const { variant: kind, slides, pageCount } = useCarousel()\n  const step = {\n    'data-carousel-previous': '',\n    'aria-label': severalPerPage(slides, pageCount)\n      ? 'Show previous slides'\n      : 'Show previous slide',\n    className: cn(\n      buttonVariants({ variant, size }),\n      'rtl:[&_svg]:-scale-x-100',\n      className,\n    ),\n    ...props,\n  }\n  const icon = children ?? (\n    <IconPlaceholder\n      lucide='ChevronLeftIcon'\n      tabler='IconChevronLeft'\n      hugeicons='ArrowLeft01Icon'\n      phosphor='CaretLeftIcon'\n      remixicon='RiArrowLeftSLine'\n      aria-hidden\n    />\n  )\n  return kind === 'deck' ? (\n    <DeckPrevious {...step}>{icon}</DeckPrevious>\n  ) : (\n    <CarouselPreviousPrimitive {...step}>{icon}</CarouselPreviousPrimitive>\n  )\n}\n\nfunction CarouselNext({\n  className,\n  variant = 'outline',\n  size = 'icon-sm',\n  children,\n  ...props\n}: CarouselStepProps) {\n  const { variant: kind, slides, pageCount } = useCarousel()\n  const step = {\n    'data-carousel-next': '',\n    'aria-label': severalPerPage(slides, pageCount)\n      ? 'Show next slides'\n      : 'Show next slide',\n    className: cn(\n      buttonVariants({ variant, size }),\n      'rtl:[&_svg]:-scale-x-100',\n      className,\n    ),\n    ...props,\n  }\n  const icon = children ?? (\n    <IconPlaceholder\n      lucide='ChevronRightIcon'\n      tabler='IconChevronRight'\n      hugeicons='ArrowRight01Icon'\n      phosphor='CaretRightIcon'\n      remixicon='RiArrowRightSLine'\n      aria-hidden\n    />\n  )\n  return kind === 'deck' ? (\n    <DeckNext {...step}>{icon}</DeckNext>\n  ) : (\n    <CarouselNextPrimitive {...step}>{icon}</CarouselNextPrimitive>\n  )\n}\n\n/** More pages than this show as a count, \"3 / 12\", which fits any column. */\nconst MAX_INDICATORS = 8\n\n/*\n * One indicator per page, each a 24px target. The current page's is longer,\n * so it never shows by colour alone, and it travels to the new page on\n * `layout`. Before the slides are measured, and with one page, the row\n * keeps its height and shows nothing.\n */\nfunction CarouselIndicatorGroup({\n  className,\n  ...props\n}: Omit<CarouselPartProps<'div'>, 'children'>) {\n  const { variant, page, pageCount, loop } = useCarousel()\n  if (pageCount === null || pageCount < 2) {\n    return (\n      <div aria-hidden data-carousel-indicators='' className={cn('h-6', className)} />\n    )\n  }\n  if (pageCount > MAX_INDICATORS) {\n    return (\n      <div\n        data-carousel-indicators=''\n        className={cn(\n          'flex h-6 items-center self-center text-xs font-medium text-muted-foreground tabular-nums',\n          className,\n        )}\n        {...props}\n      >\n        {/* ui-sdk's live region, or the deck's announcement, says where it is; this is for the eye. */}\n        <span aria-hidden className='flex items-center gap-1'>\n          <AnimatedNumber value={page} locale='en-US' className='text-foreground' />\n          <span>/</span>\n          <span>{pageCount}</span>\n        </span>\n      </div>\n    )\n  }\n  const group = {\n    'aria-label': 'Slides',\n    className: cn('relative flex h-6 min-w-0 items-center self-center', className),\n    ...props,\n  }\n  const indicators = (\n    <>\n      {Array.from({ length: pageCount }, (_, index) => (\n        <CarouselIndicator key={index} page={index + 1} />\n      ))}\n      <IndicatorPill page={page} pageCount={pageCount} loop={loop} />\n    </>\n  )\n  return variant === 'deck' ? (\n    <DeckIndicatorGroup {...group}>{indicators}</DeckIndicatorGroup>\n  ) : (\n    <CarouselIndicatorGroupPrimitive {...group}>\n      {indicators}\n    </CarouselIndicatorGroupPrimitive>\n  )\n}\n\n/** An indicator's name: the slide it shows, or the page when a page holds several. */\nfunction indicatorLabel(page: number, pageCount: number, slides: number) {\n  return slides > pageCount\n    ? `Show page ${page} of ${pageCount}`\n    : `Show slide ${page} of ${pageCount}`\n}\n\nfunction CarouselIndicator({\n  page,\n  className,\n  ...props\n}: Omit<CarouselPartProps<'button'>, 'children'> & {\n  /** The page it shows and goes to, from 1. */\n  page: number\n}) {\n  const { variant, pageCount, slides } = useCarousel()\n  const indicator = {\n    'aria-label': indicatorLabel(page, pageCount ?? page, slides),\n    className: cn(\n      'group/indicator flex size-6 shrink-0 items-center justify-center rounded-full focus-visible:ring-3 focus-visible:ring-ring/50 focus-visible:outline-hidden',\n      className,\n    ),\n    ...props,\n  }\n  const dot = (\n    <span className='size-1.5 rounded-full bg-foreground/50 transition-colors duration-(--motion-press-duration) ease-(--motion-press-fade-easing) group-hover/indicator:bg-foreground/75 forced-colors:bg-[CanvasText]' />\n  )\n  return variant === 'deck' ? (\n    <DeckIndicator page={page} {...indicator}>\n      {dot}\n    </DeckIndicator>\n  ) : (\n    <CarouselIndicatorPrimitive page={page} {...indicator}>\n      {dot}\n    </CarouselIndicatorPrimitive>\n  )\n}\n\n/** Whether going from page `from` to `to` goes round the end of a loop, where the indicator jumps rather than sweep back across every dot. */\nfunction wrapsRound(from: number, to: number, pageCount: number, loop: boolean) {\n  return loop && pageCount > 2 && Math.abs(to - from) === pageCount - 1\n}\n\n/** The current page's mark, which travels between the indicators. */\nfunction IndicatorPill({\n  page,\n  pageCount,\n  loop,\n}: {\n  page: number\n  pageCount: number\n  loop: boolean\n}) {\n  const ref = useRef<HTMLSpanElement>(null)\n  const [first] = useState(page)\n  const shown = useRef(page)\n  useLayoutEffect(() => {\n    const pill = ref.current\n    const from = shown.current\n    shown.current = page\n    if (!pill || from === page) return\n    const write = () => pill.style.setProperty('--carousel-page-index', `${page - 1}`)\n    if (wrapsRound(from, page, pageCount, loop)) withoutTransition(pill, write)\n    else write()\n  }, [page, pageCount, loop])\n  return (\n    <span\n      ref={ref}\n      aria-hidden\n      data-carousel-indicator=''\n      style={{ '--carousel-page-index': first - 1 } as CSSProperties}\n      className='pointer-events-none absolute start-0 top-0 flex size-6 items-center justify-center [translate:calc(var(--carousel-page-index,0)*100%)_0] transition-[translate] duration-[calc(var(--motion-layout-duration)*var(--carousel-swipe-strength,1))] ease-(--motion-layout-easing) motion-reduce:transition-none rtl:[translate:calc(var(--carousel-page-index,0)*-100%)_0]'\n    >\n      <span className='h-1.5 w-4 rounded-full bg-foreground forced-colors:bg-[Highlight]' />\n    </span>\n  )\n}\n\n/*\n * Deck: the slides are a stack of cards, the next two showing under the one\n * on top. The top card follows a drag and tilts with it; let go far or fast\n * enough, it is thrown off and goes to the bottom of the deck while the rest\n * step up. Previous brings the last card back from the side it left. The\n * deck primitive holds which card is on top, and moves them as it changes.\n */\n\nconst ItemIndexContext = createContext({ index: 0, count: 0 })\n\n/** Gives each card its place, so it knows its depth and names itself \"3 of 8\" from the first render. */\nfunction ItemIndexProvider({\n  index,\n  count,\n  children,\n}: {\n  index: number\n  count: number\n  children: ReactNode\n}) {\n  const place = useMemo(() => ({ index, count }), [index, count])\n  return <ItemIndexContext value={place}>{children}</ItemIndexContext>\n}\n\n/** A slide's own key, which `Children.toArray` made unique, so a card keeps its place as others come and go. */\nfunction keyOf(child: ReactNode, index: number) {\n  return isValidElement(child) && child.key !== null ? child.key : index\n}\n\n/** The deck primitive's parts, each on the element ui-sdk's Carousel uses for it. */\nconst DeckParts = createParts(deck, {\n  root: 'div',\n  viewport: 'div',\n  content: 'div',\n  item: 'div',\n  previous: 'button',\n  next: 'button',\n  'indicator-group': 'div',\n  indicator: 'button',\n})\n\nconst DeckRoot = DeckParts.Root\nconst DeckViewport = DeckParts.Viewport\nconst DeckContent = DeckParts.Content\nconst DeckPrevious = DeckParts.Previous\nconst DeckNext = DeckParts.Next\nconst DeckIndicatorGroup = DeckParts.IndicatorGroup\nconst DeckIndicator = DeckParts.Indicator\n\nfunction DeckItem({ className, ...props }: CarouselPartProps<'div'>) {\n  const { index, count } = use(ItemIndexContext)\n  return (\n    <DeckParts.Item\n      index={index}\n      count={count}\n      className={cn(\n        // Cards under the top one step back and peek out below it; while the\n        // top one is dragged off, they step up towards their next place.\n        'z-(--carousel-z) min-w-0 origin-bottom [grid-area:1/1] [--carousel-step:max(0,calc(var(--carousel-depth)-var(--carousel-swipe-progress,0)))] [translate:0_calc(var(--carousel-step)*var(--carousel-recede,0.75rem))] [scale:calc(1-var(--carousel-step)*var(--carousel-recede-scale,0.05))] [opacity:clamp(0,calc(3-var(--carousel-step)),1)]',\n        // The drag moves the top card on `transform`, which follows the pointer\n        // at once, while its place in the deck keeps settling on `layout`.\n        '[transform:translateX(var(--carousel-drag-x,0px))_rotate(calc(var(--carousel-drag-share,0)*var(--carousel-tilt,8deg)))]',\n        '[transition:translate_var(--motion-layout-duration)_var(--motion-layout-easing),scale_var(--motion-layout-duration)_var(--motion-layout-easing),rotate_var(--motion-layout-duration)_var(--motion-layout-easing),opacity_var(--motion-layout-duration)_var(--motion-layout-easing),transform_var(--motion-layout-duration)_var(--motion-layout-easing)] motion-safe:group-data-dragging/carousel-viewport:[transition:translate_var(--motion-layout-duration)_var(--motion-layout-easing),scale_var(--motion-layout-duration)_var(--motion-layout-easing),rotate_var(--motion-layout-duration)_var(--motion-layout-easing),opacity_var(--motion-layout-duration)_var(--motion-layout-easing)] motion-reduce:[transition:opacity_var(--motion-layout-duration)_var(--motion-layout-easing)]',\n        // Thrown, it leaves sideways and fades, above the rest, sooner the faster\n        // it was flicked; with less motion, it fades where it was let go.\n        'data-thrown:z-50 data-thrown:[translate:calc(var(--carousel-throw,-1)*var(--carousel-slide,60%))_0] data-thrown:[rotate:calc(var(--carousel-throw,-1)*var(--carousel-tilt,8deg))] data-thrown:[scale:1] data-thrown:opacity-0 data-thrown:duration-[calc(var(--motion-exit-duration)*var(--carousel-swipe-strength,1))] data-thrown:ease-(--motion-exit-easing) motion-reduce:data-thrown:[translate:0_0] motion-reduce:data-thrown:[rotate:0deg]',\n        className,\n      )}\n      {...props}\n    />\n  )\n}\n\nexport {\n  Carousel,\n  CarouselContent,\n  CarouselIndicator,\n  CarouselIndicatorGroup,\n  CarouselItem,\n  CarouselNext,\n  CarouselPrevious,\n  CarouselViewport,\n  indicatorLabel,\n  severalPerPage,\n  wrapsRound,\n}\nexport type { CarouselProps, CarouselVariant }\n",
      "type": "registry:component",
      "target": "@components/kino/carousel.tsx"
    }
  ],
  "type": "registry:component"
}