ZUI

Search documentation

Find a page or a heading.

Carousel

A row of slides that a swipe or its previous and next buttons scroll through.

Installation

bash

The CLI writes components/ui/carousel.tsx and lib/motion.ts, installs Direction, Button, Icon and Utils, and adds lucide-react-native, react-native-reanimated and react-native-worklets.

Usage

tsx
tsx

The previous and next buttons sit 3rem outside the carousel's sides, as upstream's do, so leave that room beside it or move them with a class.

API reference

Takes every prop of React Native's View, plus:

opts takes these of Embla's options, with Embla's meaning:

A swipe along the carousel moves the slides with the finger, and on release they settle on the snap nearest where the swipe was heading; a swipe across a quarter of the view moves at least one snap. A swipe that starts on a button in a slide takes over from the button once it has moved 8px along the carousel. Past either end the slides follow at a third of the finger's pace and spring back. With loop the last slide is followed by the first; when the slides are too few to fill the view and one more, they do not loop, as Embla's do not. A scroll to a snap takes 300ms, and none when the system asks for reduced motion.

setApi is called with an api shaped as Embla's: scrollPrev, scrollNext, scrollTo(index), canScrollPrev, canScrollNext, selectedScrollSnap, previousScrollSnap, scrollSnapList, scrollProgress, slidesInView, slidesNotInView and on and off for the init, reInit, resize, select, settle, pointerDown and pointerUp events, each listener called with the api and the event's name. A new layout, such as a turned phone, fires reInit and then resize; settle fires when a scroll ends, at once after a jump or with reduced motion:

tsx

On the web the carousel is a region described as a carousel, each slide a group described as a slide, and the left and right arrow keys move it.

CarouselContent

Takes every prop of React Native's View, which go to the row of slides inside the view they scroll through.

CarouselItem

Takes every prop of React Native's View. A slide is the whole view wide, less the row's 1rem gap; a basis class such as basis-1/2 sets how much of it the slide takes.

CarouselPrevious and CarouselNext

Take every prop of Button except children, with variant 'outline' and size 'icon-sm' by default. They are named "Previous slide" and "Next slide", and each is disabled when there is no snap that way. Vertical, they sit above and below the carousel, turned a quarter.

useCarousel

Inside a Carousel, returns its api, opts, orientation, scrollPrev, scrollNext, canScrollPrev and canScrollNext. It throws outside one.

Right to left

When Direction says text reads right to left, a horizontal carousel runs from the right: the first slide is drawn at the right, a swipe to the right moves it on, Previous sits on the right and Next on the left, and their chevrons are mirrored, as upstream's cn-rtl-flip mirrors them. The arrow keys do as upstream's do whichever way text reads: the right arrow goes to the next slide and the left arrow to the previous one. The row is laid out left to right and drawn mirrored, with each slide mirrored back, so its scroll is measured as in text read left to right. On a device it runs the provider's way even where the system lays the app out the other way, placing its buttons and laying out its slides' content as the system does.

Differences from upstream

  • Built on React Native views moved by Reanimated, where upstream is Embla, which scrolls DOM nodes. opts takes the Embla options above, and plugins is left out: Embla's plugins drive Embla's own engine.
  • The api leaves out Embla's rootNode, containerNode, slideNodes and slideRegistry, which hand out DOM nodes React Native does not have, and internalEngine and plugins, which are Embla's engine. The scroll event is left out because the scroll runs on the UI thread, and a listener on each frame would call into JavaScript on every one. reInit and destroy are left out because React owns both: the carousel follows a new opts and every new layout itself, and unmounting it destroys it. emit and the slidesChanged and slideFocus events are not ported, though the platform does not stop them.
  • The previous and next buttons each sit in a View that places them, so their className reaches the button and not its place; React Native has no percentage translate to centre an absolute child by.
  • useCarousel returns no carouselRef, which only Embla reads.