Carousel

A carousel with motion and swipe built using Embla.

1
2
3
4
5

Installation

pnpm dlx cubix@latest add carousel --base aria

About

The carousel is built with the Embla Carousel library.

Usage

Import
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from "@/components/cubix/carousel"
Example
<Carousel>
  <CarouselContent>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
  </CarouselContent>
  <CarouselPrevious />
  <CarouselNext />
</Carousel>

Composition

Use the following composition to build a Carousel:

 
Carousel
├── CarouselContent
   ├── CarouselItem
   └── CarouselItem
├── CarouselPrevious
└── CarouselNext

Examples

Sizes

Set slide width with basis on CarouselItem.

1
2
3
4
5

Spacing

Space slides with a negative margin on CarouselContent and matching padding on each item.

1
2
3
4
5

Orientation

Use the orientation prop to set the scroll axis.

1
2
3
4
5

Options

Pass Embla options through the opts prop.

 
<Carousel
  opts={{
    align: "start",
    loop: true,
  }}
>
  <CarouselContent>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
  </CarouselContent>
</Carousel>

API

Use setApi to keep an Embla instance and read the current slide.

1
2
3
4
5
Slide 0 of 0

Plugins

Add Embla plugins with the plugins prop. Autoplay needs the extra embla-carousel-autoplay package.

 
import Autoplay from "embla-carousel-autoplay"

<Carousel
  plugins={[
    Autoplay({
      delay: 2000,
    }),
  ]}
>
  <CarouselContent>
    <CarouselItem>...</CarouselItem>
  </CarouselContent>
</Carousel>

API Reference

Note: See the Embla Carousel docs for the full list of options, events, and plugins.

Carousel

The root that provides the Embla instance to content and controls.

PropTypeDefaultDescription
orientation"horizontal" | "vertical""horizontal"Scroll axis of the carousel.
optsEmblaOptions-Embla options such as loop, align, and direction. See the Embla Carousel docs.
pluginsEmblaPlugin[]-Embla plugins such as Autoplay.
setApi(api: CarouselApi) => void-Receive the Embla API instance for events and programmatic scroll.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).

CarouselItem

A single slide. Use basis classes to show more than one item.

PropTypeDefaultDescription
classNamestring-Use basis utilities such as basis-1/2 or md:basis-1/3 to set slide width.

CarouselPrevious / CarouselNext

Previous and next buttons. They disable at the ends unless loop is on.

PropTypeDefaultDescription
variant"default" | "secondary" | "destructive" | "outline" | "ghost" | "link""outline"Visual style of the previous or next button.
size"default" | "sm" | "lg" | "icon" | "icon-sm" | "icon-lg""icon-sm"Size of the previous or next button.
classNamestring-Additional Tailwind classes merged with the component styles (last one wins).