@biem/ui

Carousel

A set of slides that lets users move through content one slide at a time.

A carousel shows one slide of a set at a time and keeps the others out of view. Users move through the set by dragging the slides, with the previous and next buttons, or with the left and right arrow keys while focus is inside the carousel. On the first and the last slide, the button that leads no further is disabled.

Shelf photo aisle 1
Shelf photo aisle 2
Shelf photo aisle 3

Usage

carousel-default.tsx
import {
	Carousel,
	CarouselContent,
	CarouselItem,
	CarouselNext,
	CarouselPrevious,
} from "@biem/ui/components/ui/carousel";
import { generateGradient } from "@biem/ui/lib/generate-gradient";

const photos = [1, 2, 3].map((aisle) => ({
	alt: `Shelf photo aisle ${aisle}`,
	src: generateGradient(`Shelf photo aisle ${aisle}`),
}));

export function CarouselDefault() {
	return (
		<div className="w-full max-w-sm px-12">
			<Carousel>
				<CarouselContent>
					{photos.map((photo) => (
						<CarouselItem key={photo.alt}>
							<img src={photo.src} alt={photo.alt} className="aspect-video w-full rounded-lg object-cover"/>
						</CarouselItem>
					))}
				</CarouselContent>
				<CarouselPrevious/>
				<CarouselNext/>
			</Carousel>
		</div>
	);
}

Examples

Loop

Some sets have no natural first or last slide, such as the promotions running in a store this week. With loop set to true in opts, the carousel continues from the last slide to the first and back again, so users keep browsing in either direction and the buttons no longer stop at either end.

Shelf photo aisle 1
Shelf photo aisle 2
Shelf photo aisle 3

With Slide Counter

While users move through a set one slide at a time, they easily lose track of how far they have come and how much remains. Through setApi the application gains access to the carousel, so it can show the position of the current slide and update it with every move, whether users drag, press a button or use the arrow keys.

Shelf photo aisle 1
Shelf photo aisle 2
Shelf photo aisle 3

Photo 1 of 3

API Reference

Propopts
TypeEmblaOptionsType
Default–
DescriptionSets the options of Embla Carousel, the library that moves the slides, such as loop, align and startIndex. The carousel derives axis from orientation, so an axis set here has no effect.
Propplugins
TypeEmblaPluginType[]
Default–
DescriptionPasses plugins to Embla Carousel. Each plugin adds a behavior that the library doesn't include itself.
Proporientation
Type"horizontal" | "vertical"
Default"horizontal"
DescriptionSets the axis along which the slides move. A horizontal carousel places the previous and next buttons to the left and right of the slides, a vertical one places them above and below.
PropsetApi
Type(api: CarouselApi) => void
Default–
DescriptionRuns when the carousel has initialized and receives the Embla Carousel API. With the API, code reads the selected slide, scrolls the carousel and subscribes to events such as select. Pass a function with a stable identity, such as a state setter, because the carousel calls it again whenever the function changes.

CarouselContent

CarouselContent holds the slides and clips the ones that are out of view. It renders a div inside a clipping div, passes native attributes to the inner one, and has no props of its own.

CarouselItem

CarouselItem holds one slide. It renders a div that assistive technology announces as a slide, and it has no props of its own.

CarouselPrevious

Propvariant
Type"default" | "outline" | "secondary" | "ghost" | "destructive" | "link"
Default"outline"
DescriptionControls the visual emphasis and semantic role of the previous button.
Propsize
Type"default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg"
Default"icon-sm"
DescriptionControls the dimensions and spacing of the previous button.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the previous button with another component.
PropnativeButton
Typeboolean
Defaulttrue
DescriptionDeclares whether the rendered element is a native button. Set it to false when render returns a different element.
Propdisabled
Typeboolean
Default–
DescriptionOverrides the disabled state of the previous button. Without it, the carousel disables the button while there is no previous slide to scroll to.
PropfocusableWhenDisabled
Typeboolean
Defaultfalse
DescriptionKeeps the previous button focusable while it is disabled.

CarouselNext

Propvariant
Type"default" | "outline" | "secondary" | "ghost" | "destructive" | "link"
Default"outline"
DescriptionControls the visual emphasis and semantic role of the next button.
Propsize
Type"default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg"
Default"icon-sm"
DescriptionControls the dimensions and spacing of the next button.
Proprender
TypeReactElement | render function
Default–
DescriptionReplaces the native element or composes the next button with another component.
PropnativeButton
Typeboolean
Defaulttrue
DescriptionDeclares whether the rendered element is a native button. Set it to false when render returns a different element.
Propdisabled
Typeboolean
Default–
DescriptionOverrides the disabled state of the next button. Without it, the carousel disables the button while there is no next slide to scroll to.
PropfocusableWhenDisabled
Typeboolean
Defaultfalse
DescriptionKeeps the next button focusable while it is disabled.