// // 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, createContext, forwardRef, useContext, 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 { resolveMediaElement } from '../MediaPlayer/media-kind.ts'; import { MediaPlayer, type MediaPlayerProps } from '../MediaPlayer/MediaPlayer.tsx'; // // Root // /** How many slides a page shows and how many a move advances, as the machine counts them, to map a page to its slides. */ const PagingContext = createContext({ perPage: 1, perMove: 1 }); // No `autoSize`: slides fill the 16:9 frame, and one whose media is not mounted would size to nothing. type CarouselRootProps = ThemedClassName< Omit< CarouselPrimitive.RootProps, 'slideCount' | 'defaultPage' | 'loop' | 'autoplay' | 'translations' | 'orientation' | 'autoSize' > > & { /** 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 { slidesPerPage = 1, slidesPerMove = 'auto' } = props; const paging = useMemo( () => ({ perPage: Math.ceil(slidesPerPage), perMove: slidesPerMove === 'auto' ? Math.floor(slidesPerPage) : slidesPerMove, }), [slidesPerPage, slidesPerMove], ); 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(); const { perPage, perMove } = useContext(PagingContext); // Slides keep their size empty, so media mounts only where it is seen: playable media on a slide of the page, which // also stops it when the page is left (an embed cannot be paused from here), and an image on its neighbours too. // By the page, which changes once a scroll settles: a mount mid-swipe makes the machine snap back to the old page. const first = page * perMove; const shown = (slide: number) => slide >= first && slide < first + perPage; const mounted = src !== undefined && (resolveMediaElement(src, kind) === 'image' ? [index - 1, index, index + 1].some(shown) : shown(index)); return ( {children ?? (src && mounted && ( ))} ); }, ); 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 (