codefast/ui

Command Palette

Search for a command to run...

Source
Display

Carousel

Embla-powered slide carousel with prev/next controls. Supports horizontal and vertical axes.

Examples

Multiple per view

Show several items at once with basis utilities.

1
2
3
4
5
29 lines
import { Card, CardContent } from "@codefast/ui/card";
import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious } from "@codefast/ui/carousel";

export function CarouselMultiple() {
  return (
    <Carousel
      className="mx-auto max-w-xs sm:max-w-sm"
      opts={{
        align: "start",
      }}
    >
      <CarouselContent>
        {Array.from({ length: 5 }).map((_, index) => (
          <CarouselItem key={index} className="sm:basis-1/2 lg:basis-1/3">
            <div className="p-1">
              <Card>
                <CardContent className="flex aspect-square items-center justify-center p-6">
                  <span className="text-3xl font-semibold">{index + 1}</span>
                </CardContent>
              </Card>
            </div>
          </CarouselItem>
        ))}
      </CarouselContent>
      <CarouselPrevious className="hidden sm:inline-flex" />
      <CarouselNext className="hidden sm:inline-flex" />
    </Carousel>
  );
}

Usage

The minimal import and composition — see Examples below for styled, real-world variants.

19 lines
import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious } from "@codefast/ui/carousel";

export function CarouselUsage() {
  return (
    <Carousel className="w-full max-w-xs">
      <CarouselContent>
        {Array.from({ length: 5 }, (_, index) => (
          <CarouselItem key={index}>
            <div className="flex aspect-square items-center justify-center rounded-xl border text-4xl font-semibold">
              {index + 1}
            </div>
          </CarouselItem>
        ))}
      </CarouselContent>
      <CarouselPrevious />
      <CarouselNext />
    </Carousel>
  );
}

Anatomy

How the parts nest — every slot the component exposes, in composition order.

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

Features

  • Built on Embla; setApi exposes the live Embla instance to read the current index or call scrollTo() imperatively.
  • Arrow-key navigation follows reading direction — in RTL, Left advances instead of Right.
  • CarouselPrevious/CarouselNext disable themselves automatically at the ends of an unlooped carousel.

API reference

Props for each part of the component. All native element props are also forwarded.

Carousel

Embla-powered. Owns the viewport and exposes the underlying api.

orientation"horizontal" | "vertical"

Scroll axis.

Default"horizontal"

optsEmblaOptionsType

Embla options, e.g. { align: "start", loop: true }.

setApi(api) => void

Receive the Embla api to read the index or call scrollTo().

pluginsEmblaPluginType[]

Embla plugins such as Autoplay.

CarouselItem

classNamestring

Set basis-1/2, basis-1/3… to show multiple items per view.

Accessibility

Built to be keyboard-navigable and screen-reader friendly out of the box.

KeyFunction
TabFocuses the carousel region, then the prev/next buttons.
Arrow+LeftScrolls to the previous slide.
Arrow+RightScrolls to the next slide.
  • The region is labelled as a carousel; prev/next buttons disable at the ends.
  • Provide a text counter or labelled dots so position isn’t conveyed only visually.
  • Avoid autoplay for essential content, or pause it on hover/focus.

Guidelines

Conventions that keep usage consistent across an app.

Do

  • Show position with a counter or dot indicators.
  • Use basis utilities on items to reveal a peek of the next slide.

Don’t

  • Don’t hide critical content inside an autoplaying carousel.
  • Don’t remove the prev/next controls on non-touch devices.

Explore further

Ready to integrate?

Follow the Getting Started guide to install @codefast/ui, or browse the full component gallery.