{"version":3,"file":"chunk-Carousel.mjs","names":["CarouselPrimitive"],"sources":["../../src/components/Carousel/Carousel.tsx"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\n// @import-as-namespace\n\n// The zag machine owns the scroll-snap track, the page in view, wrap-around, autoplay (stopped the moment the reader\n// takes over), the indicator keys and the `region`/`slide` roles; Next owns the grid around the track and the media\n// each slide renders.\n\nimport { Carousel as CarouselPrimitive, useCarouselContext } from '@ark-ui/react/carousel';\nimport React, { type ReactNode, createContext, forwardRef, useContext, useEffect, useMemo, useRef } from 'react';\nimport { useTranslation } from 'react-i18next';\n\nimport { useComposedRefs } from '@dxos/react-hooks';\nimport { mx } from '@dxos/ui-theme';\nimport { type ThemedClassName } from '@dxos/ui-types';\n\nimport { translationKey } from '#translations';\n\nimport { recipes } from '../../recipes.ts';\nimport { animationsDisabled, useReducedMotion } from '../../util/animation.ts';\nimport { Button } from '../Button/Button.tsx';\nimport { resolveMediaElement } from '../MediaPlayer/media-kind.ts';\nimport { MediaPlayer, type MediaPlayerProps } from '../MediaPlayer/MediaPlayer.tsx';\n\n//\n// Root\n//\n\n/** How many slides a page shows and how many a move advances, as the machine counts them, to map a page to its slides. */\nconst PagingContext = createContext({ perPage: 1, perMove: 1 });\n\n// No `autoSize`: slides fill the 16:9 frame, and one whose media is not mounted would size to nothing.\ntype CarouselRootProps = ThemedClassName<\n  Omit<\n    CarouselPrimitive.RootProps,\n    'slideCount' | 'defaultPage' | 'loop' | 'autoplay' | 'translations' | 'orientation' | 'autoSize'\n  >\n> & {\n  /** Number of slides; nothing renders without any. */\n  count: number;\n  /**\n   * Milliseconds between automatic advances, until the reader takes over; `0` (default) never advances. Off under\n   * `prefers-reduced-motion` and `VITE_DX_DISABLE_ANIMATIONS`.\n   */\n  autoAdvance?: number;\n  defaultIndex?: number;\n  /** Wrap around in the direction of travel (last to first moves forward). */\n  continuous?: boolean;\n};\n\n/**\n * Ark's carousel root (`role=region`) as a three-column grid: PrevTrigger, the ItemGroup and NextTrigger share the\n * first row, and the IndicatorGroup and Caption sit under the ItemGroup's column.\n */\nconst CarouselRoot = forwardRef<HTMLDivElement, CarouselRootProps>(\n  ({ classNames, count, autoAdvance = 0, defaultIndex = 0, continuous = false, children, ...props }, forwardedRef) => {\n    const { t } = useTranslation(translationKey);\n    const { slidesPerPage = 1, slidesPerMove = 'auto' } = props;\n    const paging = useMemo(\n      () => ({\n        perPage: Math.ceil(slidesPerPage),\n        perMove: slidesPerMove === 'auto' ? Math.floor(slidesPerPage) : slidesPerMove,\n      }),\n      [slidesPerPage, slidesPerMove],\n    );\n    const reducedMotion = useReducedMotion();\n\n    // The machine names its controls in English; the app names them in the reader's language.\n    const translations = useMemo(\n      () => ({\n        prevTrigger: t('carousel-prev.label'),\n        nextTrigger: t('carousel-next.label'),\n        indicator: (index: number) => t('carousel-go-to.label', { index: index + 1 }),\n      }),\n      [t],\n    );\n\n    if (count === 0) {\n      return null;\n    }\n\n    // Unattended motion the reader never asked for, which also defeats the still-frame culling of agent recordings.\n    const autoplay = autoAdvance > 0 && !reducedMotion && !animationsDisabled();\n    return (\n      <CarouselPrimitive.Root\n        aria-label={t('carousel-viewport.label')}\n        {...props}\n        slideCount={count}\n        defaultPage={defaultIndex}\n        loop={continuous}\n        autoplay={autoplay ? { delay: autoAdvance } : false}\n        translations={translations}\n        className={mx(recipes.carousel(), classNames)}\n        ref={forwardedRef}\n      >\n        <PagingContext.Provider value={paging}>{children}</PagingContext.Provider>\n      </CarouselPrimitive.Root>\n    );\n  },\n);\n\nCarouselRoot.displayName = 'Carousel.Root';\n\n//\n// ItemGroup\n//\n\ntype CarouselItemGroupProps = ThemedClassName<CarouselPrimitive.ItemGroupProps>;\n\n/** The scroll-snap track: a 16:9 frame holding one slide at a time, and a tab stop. */\nconst CarouselItemGroup = forwardRef<HTMLDivElement, CarouselItemGroupProps>(\n  ({ classNames, ...props }, forwardedRef) => (\n    <CarouselPrimitive.ItemGroup\n      tabIndex={0}\n      {...props}\n      className={mx(recipes.carouselItemGroup(), classNames)}\n      ref={forwardedRef}\n    />\n  ),\n);\n\nCarouselItemGroup.displayName = 'Carousel.ItemGroup';\n\n//\n// Item\n//\n\ntype CarouselItemProps = ThemedClassName<Omit<CarouselPrimitive.ItemProps, 'children'>> &\n  Partial<\n    Pick<MediaPlayerProps, 'src' | 'kind' | 'alt' | 'controls' | 'autoPlay' | 'loop' | 'muted' | 'crossOrigin'>\n  > & {\n    /** Replaces the default MediaPlayer of `src`. */\n    children?: ReactNode;\n  };\n\n/** One slide: a MediaPlayer of `src` by default, or its children. */\nconst CarouselItem = forwardRef<HTMLDivElement, CarouselItemProps>(\n  (\n    { classNames, index, src, kind, alt, controls, autoPlay, loop, muted, crossOrigin, children, ...props },\n    forwardedRef,\n  ) => {\n    const { page } = useCarouselContext();\n    const { perPage, perMove } = useContext(PagingContext);\n    // Slides keep their size empty, so media mounts only where it is seen: playable media on a slide of the page, which\n    // also stops it when the page is left (an embed cannot be paused from here), and an image on its neighbours too.\n    // By the page, which changes once a scroll settles: a mount mid-swipe makes the machine snap back to the old page.\n    const first = page * perMove;\n    const shown = (slide: number) => slide >= first && slide < first + perPage;\n    const mounted =\n      src !== undefined &&\n      (resolveMediaElement(src, kind) === 'image' ? [index - 1, index, index + 1].some(shown) : shown(index));\n    return (\n      <CarouselPrimitive.Item\n        {...props}\n        index={index}\n        className={mx(recipes.carouselItem(), classNames)}\n        ref={forwardedRef}\n      >\n        {children ??\n          (src && mounted && (\n            <MediaPlayer\n              src={src}\n              kind={kind}\n              alt={alt}\n              // One plays at a time, on the page's first slide.\n              autoPlay={autoPlay && index === first}\n              loop={loop}\n              muted={muted}\n              controls={controls}\n              crossOrigin={crossOrigin}\n            />\n          ))}\n      </CarouselPrimitive.Item>\n    );\n  },\n);\n\nCarouselItem.displayName = 'Carousel.Item';\n\n//\n// PrevTrigger / NextTrigger\n//\n\ntype CarouselTriggerProps = { label?: string };\n\n/** A ghost icon-only Button stepping back a page; absent when everything fits on one page. */\nconst CarouselPrevTrigger = forwardRef<HTMLButtonElement, CarouselTriggerProps>(({ label }, forwardedRef) => {\n  const { t } = useTranslation(translationKey);\n  const { pageSnapPoints } = useCarouselContext();\n  if (pageSnapPoints.length <= 1) {\n    return null;\n  }\n\n  return (\n    <CarouselPrimitive.PrevTrigger asChild>\n      <Button\n        variant='ghost'\n        icon='ph--caret-left--regular'\n        iconOnly\n        label={label ?? t('carousel-prev.label')}\n        classNames={recipes.carouselPrevTrigger()}\n        ref={forwardedRef}\n      />\n    </CarouselPrimitive.PrevTrigger>\n  );\n});\n\nCarouselPrevTrigger.displayName = 'Carousel.PrevTrigger';\n\n/** A ghost icon-only Button stepping forward a page; absent when everything fits on one page. */\nconst CarouselNextTrigger = forwardRef<HTMLButtonElement, CarouselTriggerProps>(({ label }, forwardedRef) => {\n  const { t } = useTranslation(translationKey);\n  const { pageSnapPoints } = useCarouselContext();\n  if (pageSnapPoints.length <= 1) {\n    return null;\n  }\n\n  return (\n    <CarouselPrimitive.NextTrigger asChild>\n      <Button\n        variant='ghost'\n        icon='ph--caret-right--regular'\n        iconOnly\n        label={label ?? t('carousel-next.label')}\n        classNames={recipes.carouselNextTrigger()}\n        ref={forwardedRef}\n      />\n    </CarouselPrimitive.NextTrigger>\n  );\n});\n\nCarouselNextTrigger.displayName = 'Carousel.NextTrigger';\n\n//\n// IndicatorGroup\n//\n\ntype CarouselIndicatorGroupProps = ThemedClassName<Omit<CarouselPrimitive.IndicatorGroupProps, 'children'>>;\n\n/**\n * One dot per page, as a single tab stop: only the current dot is tabbable, and when the machine's arrow, Home and End\n * keys move the page, focus follows to its dot.\n */\nconst CarouselIndicatorGroup = forwardRef<HTMLDivElement, CarouselIndicatorGroupProps>(\n  ({ classNames, ...props }, forwardedRef) => {\n    const { page, pageSnapPoints } = useCarouselContext();\n    const groupRef = useRef<HTMLDivElement>(null);\n    const ref = useComposedRefs(forwardedRef, groupRef);\n    useEffect(() => {\n      const group = groupRef.current;\n      if (group && group.contains(document.activeElement)) {\n        group.querySelector<HTMLElement>(`[data-part=\"indicator\"][data-index=\"${page}\"]`)?.focus();\n      }\n    }, [page]);\n\n    if (pageSnapPoints.length <= 1) {\n      return null;\n    }\n\n    return (\n      <CarouselPrimitive.IndicatorGroup\n        {...props}\n        className={mx(recipes.carouselIndicatorGroup(), classNames)}\n        ref={ref}\n      >\n        {pageSnapPoints.map((_, index) => (\n          <CarouselPrimitive.Indicator\n            key={index}\n            index={index}\n            tabIndex={index === page ? 0 : -1}\n            className={recipes.carouselIndicator()}\n          />\n        ))}\n      </CarouselPrimitive.IndicatorGroup>\n    );\n  },\n);\n\nCarouselIndicatorGroup.displayName = 'Carousel.IndicatorGroup';\n\n//\n// Caption\n//\n\ntype CarouselCaptionProps = ThemedClassName<{\n  /** Renders the caption of the page in view. */\n  children: (page: number) => ReactNode;\n}>;\n\n/** Text under the ItemGroup's column describing the page in view; absent when there is nothing to say. */\nconst CarouselCaption = forwardRef<HTMLParagraphElement, CarouselCaptionProps>(\n  ({ classNames, children }, forwardedRef) => {\n    const { page } = useCarouselContext();\n    const content = children(page);\n    if (content == null || content === false || content === '') {\n      return null;\n    }\n\n    return (\n      <p\n        data-scope='carousel'\n        data-part='caption'\n        className={mx(recipes.carouselCaption(), classNames)}\n        ref={forwardedRef}\n      >\n        {content}\n      </p>\n    );\n  },\n);\n\nCarouselCaption.displayName = 'Carousel.Caption';\nexport type {\n  CarouselCaptionProps as CaptionProps,\n  CarouselIndicatorGroupProps as IndicatorGroupProps,\n  CarouselItemGroupProps as ItemGroupProps,\n  CarouselItemProps as ItemProps,\n  CarouselRootProps as RootProps,\n  CarouselTriggerProps as TriggerProps,\n};\n\nexport {\n  CarouselCaption as Caption,\n  CarouselIndicatorGroup as IndicatorGroup,\n  CarouselItem as Item,\n  CarouselItemGroup as ItemGroup,\n  CarouselNextTrigger as NextTrigger,\n  CarouselPrevTrigger as PrevTrigger,\n  CarouselRoot as Root,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA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