suiss/ui · Component
Carousel
A swipeable track of slides with buttons, dots and optional autoplay, built on Embla Carousel.
Carousel is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Carousel is built on embla-carousel-react. A Carousel root holds a CarouselContent track whose children are CarouselItem slides. Add CarouselPrevious and CarouselNext to page through slides; on their own they float outside the track's edges. To lay controls out in a row below the track instead, place them in a CarouselControls container, along with CarouselDots for per-slide indicators and CarouselPlayButton for autoplay.
Use a carousel for featured content, product shelves, testimonials and galleries where people browse a set of peers one at a time. For content everyone must see, prefer a static layout.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
Usage
Composition
Use the following composition to build a Carousel:
Examples
Multiple items
Set the width of each slide with a basis-* class on CarouselItem. basis-1/2 shows two slides at a time.
Loop
Pass opts={{ loop: true }} to wrap from the last slide back to the first.
Vertical
Set orientation="vertical" to scroll vertically. Standalone previous and next buttons are rotated 90 degrees and sit above and below the track.
Featured
Large cards that peek at the next slide, with plain dots below.
Spotlight
A looping hero with dots and a next button in a centered control bar.
Shelf
A product shelf with opts={{ dragFree: true }} and no visible buttons.
Testimonials
Quotes with dots and large previous and next buttons in a control row.
Gallery
Tall cards with controls aligned to the end below the track.
Featured autoplay
Set autoplay to a number of milliseconds, here 4000, to advance the slides automatically. The active dot fills as a progress bar.
Spotlight autoplay
The spotlight layout with autoplay. A CarouselPlayButton takes the place of the next button; it renders only when autoplay is enabled.
Shelf autoplay
The shelf advancing every three seconds and looping.
Testimonials autoplay
Testimonials advancing every six seconds.
Accessibility
- The root renders with
role="region"andaria-roledescription="carousel"; eachCarouselItemhasrole="group"andaria-roledescription="slide". Add anaria-labelto the root to name it. - Arrow keys move between slides while focus is inside the carousel: left and right for horizontal (reversed in right-to-left layouts), up and down for vertical.
CarouselPreviousandCarouselNextinclude visually hidden labels,Previous slideandNext slide. When one becomes disabled while focused, focus moves to the other.- Each dot is labelled
Go to slide N, and the current dot hasaria-current="true". CarouselPlayButtonis labelledPause slideshoworPlay slideshowto match its state.
API Reference
Carousel
The root region that owns the Embla instance, keyboard handling and autoplay state.
| Prop | Type | Default |
|---|---|---|
opts | EmblaOptionsType | - |
plugins | EmblaPluginType[] | - |
orientation | "horizontal" | "vertical" | "horizontal" |
setApi | (api: CarouselApi) => void | - |
autoplay | boolean | number | false |
Also accepts the props of React.ComponentProps<"div">.
opts are passed to Embla Carousel, for example align, loop and dragFree, and plugins are Embla plugins. setApi receives the Embla API once it is ready, so you can read or drive the carousel from outside. The scroll direction follows the surrounding DirectionProvider.
Set autoplay to true for a 5-second interval, or to a number of milliseconds. Autoplay pauses while the pointer hovers the carousel, while it has keyboard focus, and while the page is hidden, and starts paused when the user prefers reduced motion. The root sets data-playing while autoplay is running. Use the useCarousel hook inside a Carousel to build custom controls.
CarouselContent
The clipping viewport and the flex track that holds the slides.
Also accepts the props of React.ComponentProps<"div">.
CarouselItem
A single slide.
Also accepts the props of React.ComponentProps<"div">.
The track uses negative margin and slides use start padding for gutters; adjust both together, as in -ms-3 with ps-3.
CarouselPrevious
A button that scrolls to the previous slide, disabled at the start.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "secondary" | "tinted" | "ghost" | "plain" | "destructive" | "link" | "secondary" |
size | "default" | "xs" | "sm" | "lg" | "xl" | "form" | "icon" | "icon-xs" | "icon-sm" | "icon-lg" | "icon-xl" | "icon" |
Also accepts the props of Button.
CarouselNext
A button that scrolls to the next slide, disabled at the end.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "secondary" | "tinted" | "ghost" | "plain" | "destructive" | "link" | "secondary" |
size | "default" | "xs" | "sm" | "lg" | "xl" | "form" | "icon" | "icon-xs" | "icon-sm" | "icon-lg" | "icon-xl" | "icon" |
Also accepts the props of Button.
variant and size are passed to the Button, as on CarouselPrevious. On their own, the previous and next buttons float outside the track's edges; inside CarouselControls they sit in the row and are not rotated in vertical carousels.
CarouselControls
A row that lays out buttons, dots and the play button below the track.
Also accepts the props of React.ComponentProps<"div">.
CarouselDots
One button per snap point that jumps to that slide and marks the current one.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "plain" | "default" |
Also accepts the props of React.ComponentProps<"div">.
default places the dots on a blurred pill background; plain removes the background and padding. The current dot sets data-active, and the container sets data-variant and data-autoplay.
CarouselPlayButton
Toggles autoplay between playing and paused.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "secondary" | "tinted" | "ghost" | "plain" | "destructive" | "link" | "secondary" |
size | "default" | "xs" | "sm" | "lg" | "xl" | "form" | "icon" | "icon-xs" | "icon-sm" | "icon-lg" | "icon-xl" | "icon-sm" |
Also accepts the props of Button.
It renders only when autoplay is enabled. variant and size are passed to the Button.
Each part sets a data-slot, such as carousel-item or carousel-play-button, for styling.
Web preview
Carousel wraps a scroll view that snaps to each CarouselItem. It scrolls horizontally by default or vertically with orientation="vertical". CarouselPrevious and CarouselNext are round Buttons that sit outside the slides, or inline when placed in CarouselControls.
CarouselDots shows one dot per snap point, with the active dot stretched into a pill; with autoplay on, the active dot fills up as a timer. CarouselPlayButton pauses and resumes autoplay.
Installation
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/carousel.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/carousel.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/carousel.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Multiple items
Set each slide's width with basis on CarouselItem, as a fraction of the viewport. basis={0.5} shows two slides per view.
Web preview
Loop
Wraps around from the last slide to the first.
Web preview
Vertical
A vertical carousel with arrows above and below.
Web preview
With page indicator
Full-width cards with a page indicator below.
Featured
Peeking cards with plain dots and a play button.
CarouselDots takes variant="plain" to drop its background.
Web preview
Spotlight
A looping showcase with dots and an inline next button.
Web preview
Autoplay
Advances every four seconds, with progress dots and a play button.
Pass autoplay as true for the default 5-second delay, or as a number of milliseconds.
Shelf
A free-scrolling shelf of small cards with dragFree in opts, so it scrolls without snapping.
Web preview
Accessibility
- Previous and next buttons have the
buttonrole and are labelled "Previous slide" and "Next slide", with a disabled state at the ends. - Each dot is labelled "Go to slide N" and marks the current slide as selected.
- The play button is labelled "Pause slideshow" or "Play slideshow".
API Reference
Carousel
Root that holds the scroll state and options.
Also accepts the props of React.ComponentProps<typeof CarouselPrimitive.Root>.
Use opts for loop, align (start or center) and dragFree. Pass autoplay as true for the default 5-second delay, or as a number of milliseconds. Use setApi to get a CarouselApi for scrolling from outside, or call useCarousel() inside the carousel to read the selected index and scroll state.
CarouselContent
Scrolling track for the slides.
Also accepts the props of React.ComponentProps<typeof CarouselPrimitive.Content>.
CarouselItem
One slide.
Also accepts the props of React.ComponentProps<typeof CarouselPrimitive.Item>.
CarouselPrevious
Button that scrolls to the previous slide.
Also accepts the props of React.ComponentProps<typeof Button>.
CarouselNext
Button that scrolls to the next slide.
Also accepts the props of React.ComponentProps<typeof Button>.
Arrow icons are mirrored in right-to-left layouts.
CarouselControls
Row that lays out dots and buttons below the slides.
Also accepts the props of ViewProps.
Put CarouselDots, CarouselPlayButton and the previous and next buttons inside it to show them in a row below the slides.
CarouselDots
One pressable dot per snap point.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "plain" | "default" |
Also accepts the props of ViewProps.
default sets the dots on a control background, and plain drops it. Dot and progress animations respect the system Reduce Motion setting.
CarouselPlayButton
Toggles autoplay.
Also accepts the props of React.ComponentProps<typeof Button>.
CarouselOrientation
Values: .horizontal, .vertical.
CarouselDotsVariant
Values: .default, .plain.
CarouselController
Takes no parameters.
| Property | Type |
|---|---|
selectedIndex | Int |
count | Int |
canScrollPrev | Bool |
canScrollNext | Bool |
CarouselController.scrollTo
| Parameter | Type | Default |
|---|---|---|
_Â index | Int | - |
animated | Bool | true |
CarouselController.scrollPrev
Takes no parameters.
CarouselController.scrollNext
Takes no parameters.
CarouselController.setPlaying
| Parameter | Type | Default |
|---|---|---|
_Â playing | Bool | - |
Carousel
Root that holds the scroll state and options.
| Parameter | Type | Default |
|---|---|---|
orientation | CarouselOrientation: .horizontal | .vertical | .horizontal |
loop | Bool | false |
autoplay | Duration? | nil |
controller | CarouselController? | nil |
onSelect | ((Int) -> Void)? | nil |
content | () -> Content | - |
CarouselContent
Scrolling track for the slides.
| Parameter | Type | Default |
|---|---|---|
spacing | CGFloat | 16 |
content | () -> Content | - |
CarouselItem
One slide.
| Parameter | Type | Default |
|---|---|---|
basis | CGFloat | 1 |
content | () -> Content | - |
CarouselPrevious
Button that scrolls to the previous slide.
| Parameter | Type | Default |
|---|---|---|
variant | VariantButtonStyle.Variant | .secondary |
size | VariantButtonStyle.Size | .icon |
CarouselNext
Button that scrolls to the next slide.
| Parameter | Type | Default |
|---|---|---|
variant | VariantButtonStyle.Variant | .secondary |
size | VariantButtonStyle.Size | .icon |
CarouselControls
Row that lays out dots and buttons below the slides.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
CarouselDots
One pressable dot per snap point.
| Parameter | Type | Default |
|---|---|---|
variant | CarouselDotsVariant: .default | .plain | .default |
CarouselPlayButton
Toggles autoplay.
| Parameter | Type | Default |
|---|---|---|
variant | VariantButtonStyle.Variant | .secondary |
size | VariantButtonStyle.Size | .iconSm |
CarouselOrientation
Values: Horizontal, Vertical.
CarouselDotsVariant
Values: Default, Plain.
CarouselState
| Property | Type |
|---|---|
pageCount | Int get() |
selectedIndex | Int get() |
canScrollPrev | Boolean get() |
canScrollNext | Boolean get() |
isPlaying | Boolean get() |
CarouselState.scrollTo
| Parameter | Type | Default |
|---|---|---|
index | Int | - |
animate | Boolean | true |
CarouselState.scrollPrev
Takes no parameters.
CarouselState.scrollNext
Takes no parameters.
CarouselState.setPlaying
| Parameter | Type | Default |
|---|---|---|
playing | Boolean | - |
rememberCarouselState
| Parameter | Type | Default |
|---|---|---|
pageCount | Int | - |
initialPage | Int | 0 |
loop | Boolean | false |
Returns CarouselState.
Carousel
Root that holds the scroll state and options.
| Parameter | Type | Default |
|---|---|---|
state | CarouselState | - |
modifier | Modifier | Modifier |
orientation | CarouselOrientation: Horizontal | Vertical | CarouselOrientation.Horizontal |
autoplay | Boolean | false |
autoplayDelayMillis | Long | 5000L |
onSelect | ((Int) -> Unit)? | null |
content | @Composable ColumnScope.() -> Unit | - |
CarouselContent
Scrolling track for the slides.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
contentPadding | PaddingValues | PaddingValues(0.dp) |
pageSpacing | Dp | 16.dp |
key | ((Int) -> Any)? | null |
content | @Composable (page: Int) -> Unit | - |
CarouselItem
One slide.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable BoxScope.() -> Unit | - |
CarouselPrevious
Button that scrolls to the previous slide.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
variant | ButtonVariant | ButtonVariant.Secondary |
size | ButtonSize | ButtonSize.Icon |
contentDescription | String | "Previous slide" |
CarouselNext
Button that scrolls to the next slide.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
variant | ButtonVariant | ButtonVariant.Secondary |
size | ButtonSize | ButtonSize.Icon |
contentDescription | String | "Next slide" |
CarouselControls
Row that lays out dots and buttons below the slides.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable RowScope.() -> Unit | - |
CarouselDots
One pressable dot per snap point.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
variant | CarouselDotsVariant: Default | Plain | CarouselDotsVariant.Default |
CarouselPlayButton
Toggles autoplay.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
variant | ButtonVariant | ButtonVariant.Secondary |
size | ButtonSize | ButtonSize.IconSm |