---
title: "Carousel · UI SDK — StyleX Blocks"
description: "Sliding panels with previous and next controls and clickable indicator dots."
url: "https://stylexblocks.com/components/ui-sdk/carousel/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Carousel

Sliding panels with previous and next controls and clickable indicator dots.

- Behavior engine: UI SDK
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/carousel.md), [Base UI](https://stylexblocks.com/components/base-ui/carousel.md)
- Category: Display
- HTML page: https://stylexblocks.com/components/ui-sdk/carousel/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style ui-sdk
```

Then add the item as source you own:

```sh
npx stylexblocks add carousel
```

Equivalent: `npx shadcn add @stylexblocks/carousel`. Registry item JSON: https://stylexblocks.com/r/ui-sdk/carousel.json

## Usage

```tsx
import { 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>
  );
}
```

## Source

`components/ui/ui-sdk/carousel.tsx`

```tsx
import * 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 generics
function 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,
};
```
