Skip to content

    Carousel

    Shows one slide at a time. Previous and next sit over the frame, and a dot for each slide sits on a glass pill. `loop` wraps from the last slide to the first.

    React

    Preview

    1 of 4

    Waves on an empty beach

    Props

    • label
    • loop
    import { Carousel } from "@/components/ui/carousel"
    import { Image } from "@/components/ui/image"
    
    const places = {
      coast: {
        src: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Waves on an empty beach"
      },
      ridge: {
        src: "https://images.unsplash.com/photo-1469474968028-56623f02e42e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Sun through a stand of trees"
      },
      lake: {
        src: "https://images.unsplash.com/photo-1501785888041-af3ef285b470?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A lake between mountains"
      },
      cabin: {
        src: "https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A cabin beside a still lake"
      }
    }
    
    <Carousel label="Places" className="w-full">
      <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.cabin.src} alt={places.cabin.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
    </Carousel>

    Usage

    When to use

    • Show a few images or panels one at a time, such as photos of a place.
    • Let the reader move through a short sequence with previous, next and a dot for each item.

    When not to use

    • A long list the reader scans. Use a list or Table.
    • Steps of a flow the reader moves through. Use Stepper.
    • One image. Use Image.

    Platform support

    What each platform has, from the spec. A selected platform is highlighted.

    NameWebiOSAndroidNote
    labelYesYesYes
    countNoYesYesOnly on iOS, Android.
    indexYesYesYes
    defaultIndexYesYesYes
    onIndexChangeYesYesYes
    loopYesYesYes
    labelsYesNoNoOnly on web. Not generated for iOS and Android.
    value of keyboard: TabYesDeferredDeferred
    value of keyboard: ArrowLeft, ArrowRightYesDeferredDeferred
    value of keyboard: Home, EndYesDeferredDeferred
    value of keyboard: Enter, SpaceYesDeferredDeferred

    Examples

    Places

    Previous, next and a dot for each photo, on a glass pill.

    1 of 4

    Waves on an empty beach
    import { Carousel } from "@/components/ui/carousel"
    import { Image } from "@/components/ui/image"
    
    const places = {
      coast: {
        src: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Waves on an empty beach"
      },
      ridge: {
        src: "https://images.unsplash.com/photo-1469474968028-56623f02e42e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Sun through a stand of trees"
      },
      lake: {
        src: "https://images.unsplash.com/photo-1501785888041-af3ef285b470?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A lake between mountains"
      },
      cabin: {
        src: "https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A cabin beside a still lake"
      }
    }
    
    <Carousel label="Places" className="w-full">
      <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.cabin.src} alt={places.cabin.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
    </Carousel>

    Stops at the ends

    With loop off, Previous is disabled on the first photo and Next on the last.

    1 of 3

    Waves on an empty beach
    import { Carousel } from "@/components/ui/carousel"
    import { Image } from "@/components/ui/image"
    
    const places = {
      coast: {
        src: "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Waves on an empty beach"
      },
      ridge: {
        src: "https://images.unsplash.com/photo-1469474968028-56623f02e42e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "Sun through a stand of trees"
      },
      lake: {
        src: "https://images.unsplash.com/photo-1501785888041-af3ef285b470?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A lake between mountains"
      },
      cabin: {
        src: "https://images.unsplash.com/photo-1470770841072-f978cf4d019e?auto=format&fit=crop&w=1600&h=1000&q=80",
        alt: "A cabin beside a still lake"
      }
    }
    
    <Carousel label="Places" loop={false} className="w-full">
      <Image src={places.coast.src} alt={places.coast.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.ridge.src} alt={places.ridge.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
      <Image src={places.lake.src} alt={places.lake.alt} ratio="16 / 9" className="w-full min-w-0 max-w-full" />
    </Carousel>

    Do and don't

    Do

    • Name the region with label, in the reader's language.
    • Keep the sequence short, about three to eight slides.
    • Give each slide the same frame, such as one ratio, so the page does not jump.

    Don't

    • Don't use it for the steps of a flow.
    • Don't put another carousel inside a slide.
    • Don't autoplay. The reader moves the slides.

    Props

    NameTypeValuesDefaultDescription
    label requiredstring--Names the carousel region for assistive technology.
    count requirednumber--Native only: how many slides the carousel holds. Calls the slide slot with each index from 0 to count - 1. Only on iOS, Android.
    indexnumber--The slide on screen, from 0. Set it, with onIndexChange, to control which slide shows.
    defaultIndexnumber-0The slide shown first, from 0, when index is not set.
    onIndexChange(index: number) => void--Called with the next index when the slide changes.
    loopboolean-trueWraps from the last slide to the first, and from the first to the last. Off, Previous is disabled on the first slide and Next on the last.
    labels{ previous?: string; next?: string; indicators?: string; slide?: (current: number, total: number) => string; indicator?: (current: number) => string }--Words for the buttons and the slide names. Defaults: Previous slide, Next slide, Slides, 2 of 4, Show slide 2. Only on web. Not generated for iOS and Android.

    Slots

    NameRequiredDescription
    slidesyesOne node per slide, in order. Each is shown on its own.
    slideyesNative only: the slide for one index, called for 0 up to count - 1.

    Anatomy

    PartDescription
    rootThe region named by label.
    viewportThe frame that clips the slides, with container corners.
    trackThe row of slides. It moves by one frame width.
    itemOne slide.
    stepPrevious and next: round glass buttons over the frame.
    hitAreaTransparent ::before on Previous and Next, never smaller than size-target-min.
    pillThe glass pill along the bottom that holds the dots, with no gap between them.
    dotA fixed 16px button, on mouse and on touch. The 8px mark sits in the middle, so the space around it is the hit area.
    iconThe dot drawn inside that button.

    Accessibility

    Role: region

    KeysAction
    TabMoves through Previous, Next and the current dot.
    ArrowLeft, ArrowRightShows the previous or next slide. In right to left, the arrows swap.
    Home, EndShows the first or last slide.
    Enter, SpaceActivates the focused button.
    • The region is named by label, with aria-roledescription carousel.
    • Each slide is a group named like 2 of 4. A slide that is not showing is hidden and inert, so its contents are not in the tab order.
    • The current dot has aria-current. The other dots stay in the group and are left out of the tab order.
    • With loop off, Previous is disabled on the first slide and Next on the last.
    • Previous and Next keep a size-target-min hit area. Each dot is a fixed 16px button with an 8px mark, on mouse and on touch, so the pill does not grow on a coarse pointer.

    Install

    npx werewolf-ui add carousel

    Run npx werewolf-ui init once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See Installation for requirements and the alternative registry setup.