Carousel

A row of slides that moves one at a time, by its arrow buttons, a swipe or the arrow keys, built on Embla.

1
2
3
4
5
import { Card, CardContent } from "@produkto/ui/Card";
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from "@produkto/ui/Carousel";

const slides: number[] = [1, 2, 3, 4, 5];

const Example = () => (
  <Carousel className="w-full max-w-48 sm:max-w-xs">
    <CarouselContent>
      {slides.map((slide) => (
        <CarouselItem key={slide}>
          <div className="p-1">
            <Card>
              <CardContent className="flex aspect-square items-center justify-center">
                <span className="heading-4xl">{slide}</span>
              </CardContent>
            </Card>
          </div>
        </CarouselItem>
      ))}
    </CarouselContent>
    <CarouselPrevious />
    <CarouselNext />
  </Carousel>
);

export default Example;

Usage

import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from "@produkto/ui/Carousel";
<Carousel>
  <CarouselContent>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
    <CarouselItem>...</CarouselItem>
  </CarouselContent>
  <CarouselPrevious />
  <CarouselNext />
</Carousel>

Examples

Orientation

orientation sets the way the slides move: horizontal, the default, or vertical, up and down in a height set on CarouselContent.

1
2
3
4
5
1
2
3
4
5
import { Card, CardContent } from "@produkto/ui/Card";
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from "@produkto/ui/Carousel";

const slides: number[] = [1, 2, 3, 4, 5];

const Slides = () =>
  slides.map((slide) => (
    <CarouselItem key={slide}>
      <div className="p-1">
        <Card>
          <CardContent className="flex aspect-square items-center justify-center">
            <span className="heading-4xl">{slide}</span>
          </CardContent>
        </Card>
      </div>
    </CarouselItem>
  ));

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    <Carousel className="mx-12 w-48">
      <CarouselContent>
        <Slides />
      </CarouselContent>
      <CarouselPrevious />
      <CarouselNext />
    </Carousel>
    <Carousel className="my-12 w-48" orientation="vertical">
      <CarouselContent className="h-52">
        <Slides />
      </CarouselContent>
      <CarouselPrevious />
      <CarouselNext />
    </Carousel>
  </div>
);

export default Example;

Dots

CarouselDots marks every place the carousel can stop with a dot, the current one drawn long, and moves there when one is pressed.

1
2
3
4
5
import { Card, CardContent } from "@produkto/ui/Card";
import {
  Carousel,
  CarouselContent,
  CarouselDots,
  CarouselItem,
} from "@produkto/ui/Carousel";

const slides: number[] = [1, 2, 3, 4, 5];

const Example = () => (
  <Carousel className="w-full max-w-48 sm:max-w-xs">
    <CarouselContent>
      {slides.map((slide) => (
        <CarouselItem key={slide}>
          <div className="p-1">
            <Card>
              <CardContent className="flex aspect-square items-center justify-center">
                <span className="heading-4xl">{slide}</span>
              </CardContent>
            </Card>
          </div>
        </CarouselItem>
      ))}
    </CarouselContent>
    <CarouselDots className="mt-4" />
  </Carousel>
);

export default Example;

Max

max shows at most that many of CarouselDots' dots, the window following the current place, beside one with a dot for every place as it has by default.

1
2
3
4
5
6
1
2
3
4
5
6
import { Card, CardContent } from "@produkto/ui/Card";
import {
  Carousel,
  CarouselContent,
  CarouselDots,
  CarouselItem,
} from "@produkto/ui/Carousel";

const slides: number[] = [1, 2, 3, 4, 5, 6];

const Dotted = ({ max }: { max?: number }) => (
  <Carousel className="w-48">
    <CarouselContent>
      {slides.map((slide) => (
        <CarouselItem key={slide}>
          <div className="p-1">
            <Card>
              <CardContent className="flex aspect-square items-center justify-center">
                <span className="heading-4xl">{slide}</span>
              </CardContent>
            </Card>
          </div>
        </CarouselItem>
      ))}
    </CarouselContent>
    <CarouselDots className="mt-4" max={max} />
  </Carousel>
);

const Example = () => (
  <div className="flex flex-wrap items-center gap-2">
    <Dotted />
    <Dotted max={3} />
  </div>
);

export default Example;

Moving by itself

With an interval in milliseconds it moves to the next slide by itself while at least 30% of it is on screen and no pointer or focus is on it, wrapping round from the last when it loops, and stays still for anyone who asks for less motion.

1
2
3
4
5
import { Card, CardContent } from "@produkto/ui/Card";
import {
  Carousel,
  CarouselContent,
  CarouselDots,
  CarouselItem,
} from "@produkto/ui/Carousel";

const slides: number[] = [1, 2, 3, 4, 5];

const Example = () => (
  <Carousel
    className="w-full max-w-48 sm:max-w-xs"
    interval={3000}
    opts={{ loop: true }}
  >
    <CarouselContent>
      {slides.map((slide) => (
        <CarouselItem key={slide}>
          <div className="p-1">
            <Card>
              <CardContent className="flex aspect-square items-center justify-center">
                <span className="heading-4xl">{slide}</span>
              </CardContent>
            </Card>
          </div>
        </CarouselItem>
      ))}
    </CarouselContent>
    <CarouselDots className="mt-4" />
  </Carousel>
);

export default Example;

Fading

With fade the slides crossfade in place instead of sliding across, on Embla's own fade plugin; the dots, the arrow keys and an interval move it as they move a sliding one.

1
2
3
4
5
import { Card, CardContent } from "@produkto/ui/Card";
import {
  Carousel,
  CarouselContent,
  CarouselDots,
  CarouselItem,
} from "@produkto/ui/Carousel";

const slides: number[] = [1, 2, 3, 4, 5];

const Example = () => (
  <Carousel
    className="w-full max-w-48 sm:max-w-xs"
    fade
    interval={3000}
    opts={{ loop: true }}
  >
    <CarouselContent>
      {slides.map((slide) => (
        <CarouselItem key={slide}>
          <div className="p-1">
            <Card>
              <CardContent className="flex aspect-square items-center justify-center">
                <span className="heading-4xl">{slide}</span>
              </CardContent>
            </Card>
          </div>
        </CarouselItem>
      ))}
    </CarouselContent>
    <CarouselDots className="mt-4" />
  </Carousel>
);

export default Example;

API Reference

Carousel

CarouselProps · components/Carousel/Carousel.types.ts
PropTypeDefault
optsCarouselOptions-
pluginsCarouselPlugin[]-
orientationCarouselOrientation"horizontal" | "vertical""horizontal"
setApi(api: CarouselApi) => void-
intervalnumber-
fadebooleanfalse

CarouselContent

CarouselContentProps · takes the props of <div>

CarouselItem

CarouselItemProps · takes the props of <div>

CarouselPrevious

CarouselPreviousProps · also takes the props of <button>
PropTypeDefault
hrefstring-
targetHTMLAttributeAnchorTarget-
relstring-
prefetchboolean-
appearanceButtonAppearance"solid" | "soft" | "outline" | "transparent"-
toneButtonTone"neutral" | "accent" | "destructive" | "success" | "warning" | "info"-
sizeButtonSize"xs" | "sm" | "base" | "lg"-
wrapboolean-
blockboolean-
mutedboolean-
alignButtonAlign"start" | "center" | "end"-
borderRadiusButtonBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full"-
loadingboolean-
onClickMouseEventHandler<HTMLElement>-

CarouselNext

CarouselNextProps · also takes the props of <button>
PropTypeDefault
hrefstring-
targetHTMLAttributeAnchorTarget-
relstring-
prefetchboolean-
appearanceButtonAppearance"solid" | "soft" | "outline" | "transparent"-
toneButtonTone"neutral" | "accent" | "destructive" | "success" | "warning" | "info"-
sizeButtonSize"xs" | "sm" | "base" | "lg"-
wrapboolean-
blockboolean-
mutedboolean-
alignButtonAlign"start" | "center" | "end"-
borderRadiusButtonBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full"-
loadingboolean-
onClickMouseEventHandler<HTMLElement>-

CarouselDots

CarouselDotsProps · also takes the props of <div>
PropTypeDefault
maxnumber-

It also takes the props of <div>.