// // Copyright 2026 DXOS.org // // @import-as-namespace // The zag machine owns the scroll-snap track, the page in view, wrap-around, autoplay (stopped the moment the reader // takes over), the indicator keys and the `region`/`slide` roles; Next owns the grid around the track and the media // each slide renders. import { Carousel as CarouselPrimitive, useCarouselContext } from '@ark-ui/react/carousel'; import React, { type ReactNode, forwardRef, useEffect, useMemo, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { useComposedRefs } from '@dxos/react-hooks'; import { mx } from '@dxos/ui-theme'; import { type ThemedClassName } from '@dxos/ui-types'; import { translationKey } from '#translations'; import { recipes } from '../../recipes.ts'; import { animationsDisabled, useReducedMotion } from '../../util/animation.ts'; import { Button } from '../Button/Button.tsx'; import { MediaPlayer, type MediaPlayerProps } from '../MediaPlayer/MediaPlayer.tsx'; // // Root // type CarouselRootProps = ThemedClassName< Omit > & { /** Number of slides; nothing renders without any. */ count: number; /** * Milliseconds between automatic advances, until the reader takes over; `0` (default) never advances. Off under * `prefers-reduced-motion` and `VITE_DX_DISABLE_ANIMATIONS`. */ autoAdvance?: number; defaultIndex?: number; /** Wrap around in the direction of travel (last to first moves forward). */ continuous?: boolean; }; /** * Ark's carousel root (`role=region`) as a three-column grid: PrevTrigger, the ItemGroup and NextTrigger share the * first row, and the IndicatorGroup and Caption sit under the ItemGroup's column. */ const CarouselRoot = forwardRef( ({ classNames, count, autoAdvance = 0, defaultIndex = 0, continuous = false, children, ...props }, forwardedRef) => { const { t } = useTranslation(translationKey); const reducedMotion = useReducedMotion(); // The machine names its controls in English; the app names them in the reader's language. const translations = useMemo( () => ({ prevTrigger: t('carousel-prev.label'), nextTrigger: t('carousel-next.label'), indicator: (index: number) => t('carousel-go-to.label', { index: index + 1 }), }), [t], ); if (count === 0) { return null; } // Unattended motion the reader never asked for, which also defeats the still-frame culling of agent recordings. const autoplay = autoAdvance > 0 && !reducedMotion && !animationsDisabled(); return ( {children} ); }, ); CarouselRoot.displayName = 'Carousel.Root'; // // ItemGroup // type CarouselItemGroupProps = ThemedClassName; /** The scroll-snap track: a 16:9 frame holding one slide at a time, and a tab stop. */ const CarouselItemGroup = forwardRef( ({ classNames, ...props }, forwardedRef) => ( ), ); CarouselItemGroup.displayName = 'Carousel.ItemGroup'; // // Item // type CarouselItemProps = ThemedClassName> & Partial< Pick > & { /** Replaces the default MediaPlayer of `src`. */ children?: ReactNode; }; /** One slide: a MediaPlayer of `src` by default, or its children. */ const CarouselItem = forwardRef( ( { classNames, index, src, kind, alt, controls, autoPlay, loop, muted, crossOrigin, children, ...props }, forwardedRef, ) => { const { page } = useCarouselContext(); return ( {children ?? (src && ( ))} ); }, ); CarouselItem.displayName = 'Carousel.Item'; // // PrevTrigger / NextTrigger // type CarouselTriggerProps = { label?: string }; /** A ghost icon-only Button stepping back a page; absent when everything fits on one page. */ const CarouselPrevTrigger = forwardRef(({ label }, forwardedRef) => { const { t } = useTranslation(translationKey); const { pageSnapPoints } = useCarouselContext(); if (pageSnapPoints.length <= 1) { return null; } return (