All components
Carousel
Sliding panels with previous and next controls and clickable indicator dots.
1234567891011121314151617181920212223242526272829import { Carousel } from "#components/ui/ui-sdk/carousel.tsx";import { indigoPalette } from "@stylexblocks/ui/themes/palettes.ts";
const slides = ["Mountains", "Coastline", "Forest"];
export function Example() { return ( // UI SDK's carousel wearing the recipe: native scroll-snap swiping, // arrow keys on the viewport, and the indicators as a tablist. Pages // are 0-based like the other engines (page / defaultPage / // onPageChange); the current dot carries data-selected <Carousel.Root aria-label="Destinations" palette={indigoPalette}> <Carousel.ItemGroup> {slides.map((label) => ( <Carousel.Item key={label}>{label}</Carousel.Item> ))} </Carousel.ItemGroup> <Carousel.Control> <Carousel.PrevTrigger>‹</Carousel.PrevTrigger> <Carousel.IndicatorGroup aria-label="Choose a slide"> {slides.map((label, index) => ( <Carousel.Indicator key={label} index={index} /> ))} </Carousel.IndicatorGroup> <Carousel.NextTrigger>›</Carousel.NextTrigger> </Carousel.Control> </Carousel.Root> );}Install
1npx stylexblocks add carouselSource
components/ui/ui-sdk/carousel.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254import * as stylex from "@stylexjs/stylex";import { Carousel as UiSdkCarousel, CarouselContent as UiSdkCarouselContent, CarouselIndicator as UiSdkCarouselIndicator, CarouselIndicatorGroup as UiSdkCarouselIndicatorGroup, CarouselItem as UiSdkCarouselItem, CarouselNext as UiSdkCarouselNext, CarouselPrevious as UiSdkCarouselPrevious, CarouselViewport as UiSdkCarouselViewport,} from "@ui-sdk/react/carousel";import { mergeProps, renderElement, type PartProps } from "@ui-sdk/react/primitive";import type { StyleOverrideProps } from "#lib/recipe-props.ts";import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'import { cursor } from '@stylexblocks/ui/tokens/cursor.stylex.ts'import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
const carouselRoot = stylex.create({ base: { position: 'relative', display: 'flex', gap: spacing._2, },})
const carouselItem = stylex.create({ base: {},})
const carouselControl = stylex.create({ base: { display: 'flex', alignItems: 'center', },})
const carouselIndicatorGroup = stylex.create({ base: { display: 'flex', justifyContent: 'center', gap: spacing._3, },})
const carouselIndicator = stylex.create({ base: { width: sizes._2_5, height: sizes._2_5, borderRadius: radii.full, backgroundColor: colorPalette.subtle, cursor: cursor.button, },})
// The recipe styles the shell; stacking direction, full-width slides, the// hidden native scrollbar and the active-dot accent are the component's own// structural styles. UI SDK's carousel scrolls natively: the viewport is the// scroller, the content the row of slides. The current dot carries// `data-selected`; Previous/Next at their end carry `data-disabled`.const carouselLocal = stylex.create({ root: { flexDirection: "column" }, viewport: { width: "100%", scrollbarWidth: "none" }, item: { flexBasis: "100%", minWidth: 0 }, indicator: { backgroundColor: { default: colorPalette.subtle, ":is([data-selected])": colorPalette.solid, }, },});
// recipe maps hold stylex.props-compatible values by construction; one cast// bridges StyleX's variadic genericsfunction sx( styles: ReadonlyArray<unknown>, palette: StyleOverrideProps["palette"], style: StyleOverrideProps["style"],) { return stylex.props( ...([palette, ...styles, style] as unknown as ReadonlyArray<never>), );}
type UiSdkCarouselProps = React.ComponentProps<typeof UiSdkCarousel>;
export type CarouselRootProps = Omit< UiSdkCarouselProps, "style" | "page" | "defaultPage" | "onPageChange"> & StyleOverrideProps & { /** Total number of slides. UI SDK counts the slides itself; kept for parity with the other engines. */ slideCount?: number; /** Controlled page index, from 0. */ page?: number; /** Initial page index when uncontrolled, from 0. */ defaultPage?: number; onPageChange?: (page: number) => void; };
/** * Carousel — UI SDK's carousel wearing the carousel recipe. Paging, native * scroll-snap swiping and dragging, arrow keys on the viewport, the * indicators' tablist, looping and autoplay are UI SDK's. The page index is * 0-based like the other engines (UI SDK counts from 1; the Root * translates). `ItemGroup` renders UI SDK's viewport and content around the * slides. Every part composes via `render`. */function Root(allProps: CarouselRootProps) { const { slideCount: _slideCount, page, defaultPage, onPageChange, palette, style, ...rest } = allProps; return ( <UiSdkCarousel {...mergeProps( sx([carouselRoot.base, carouselLocal.root], palette, style), rest, )} page={page === undefined ? undefined : page + 1} defaultPage={defaultPage === undefined ? undefined : defaultPage + 1} onPageChange={onPageChange ? (next) => onPageChange(next - 1) : undefined} /> );}Root.displayName = "CarouselRoot";
/** A plain styled part (no behavior of its own). */function createPart<T extends keyof React.JSX.IntrinsicElements>( tag: T, styles: ReadonlyArray<unknown>, displayName: string,) { function Part(allProps: Omit<PartProps<T>, "style"> & StyleOverrideProps) { const { render, palette, style, ...rest } = allProps as Record<string, unknown>; return renderElement( tag, mergeProps(sx(styles, palette as never, style as never), rest), render as PartProps<T>["render"], ); } Part.displayName = displayName; return Part;}
/** Wraps a UI SDK part with fixed styles plus the `palette`/`style` overrides. */function styled<P extends object>( Component: React.ComponentType<P>, styles: ReadonlyArray<unknown>, displayName: string, defaults: Partial<P> = {},) { const Element = Component as React.ElementType; function Part(allProps: Omit<P, "style"> & StyleOverrideProps) { const { palette, style, ...rest } = allProps as Record<string, unknown>; return ( <Element {...mergeProps(defaults, sx(styles, palette as never, style as never), rest)} /> ); } Part.displayName = displayName; return Part;}
export type CarouselItemGroupProps = Omit< React.ComponentProps<typeof UiSdkCarouselViewport>, "style"> & StyleOverrideProps;
function ItemGroup(allProps: CarouselItemGroupProps) { const { palette, style, children, ...rest } = allProps; return ( <UiSdkCarouselViewport {...mergeProps(sx([carouselLocal.viewport], palette, style), rest)} > <UiSdkCarouselContent>{children as React.ReactNode}</UiSdkCarouselContent> </UiSdkCarouselViewport> );}ItemGroup.displayName = "CarouselItemGroup";
export type CarouselItemProps = Omit< React.ComponentProps<typeof UiSdkCarouselItem>, "style"> & StyleOverrideProps & { /** This slide's index. UI SDK infers it from the order; kept for parity. */ index?: number; };
function Item({ index: _index, ...rest }: CarouselItemProps) { return <StyledItem {...rest} />;}Item.displayName = "CarouselItem";
const StyledItem = styled( UiSdkCarouselItem, [carouselItem.base, carouselLocal.item], "CarouselItem",);
export type CarouselIndicatorProps = Omit< React.ComponentProps<typeof UiSdkCarouselIndicator>, "style" | "page"> & StyleOverrideProps & { /** The page this dot selects, from 0. */ index: number; };
function Indicator({ index, ...rest }: CarouselIndicatorProps) { return ( <StyledIndicator page={index + 1} aria-label={`Go to slide ${index + 1}`} {...rest} /> );}Indicator.displayName = "CarouselIndicator";
const StyledIndicator = styled( UiSdkCarouselIndicator, [carouselIndicator.base, carouselLocal.indicator], "CarouselIndicator",);
export const Carousel = { Root, ItemGroup, Item, Control: createPart("div", [carouselControl.base], "CarouselControl"), PrevTrigger: styled(UiSdkCarouselPrevious, [], "CarouselPrevTrigger", { "aria-label": "Previous slide", }), NextTrigger: styled(UiSdkCarouselNext, [], "CarouselNextTrigger", { "aria-label": "Next slide", }), IndicatorGroup: styled( UiSdkCarouselIndicatorGroup, [carouselIndicatorGroup.base], "CarouselIndicatorGroup", ), Indicator,};