- Label
- Icon label
- Field
- Form
- Frontmatter form
- Questionnaire
- Input
- Input group
- Input OTP
- Password input
- Password strength
- Code input
- Number field
- Tag input
- Textarea
- Checkbox
- Radio group
- Language toggle group
- Emoji appearance toggle group
- Emoji picker
- Avatar picker
- AI provider picker
- Select
- Native select
- Combobox
- Language combobox
- Slider
- Calendar
- Switch
- Alert
- Avatar
- Badge
- Card
- AI provider card
- Model info card
- Model list
- Empty
- Item
- Tree item
- File tree
- Icon media
- File type icon
- Font preview
- Image preview
- Kbd
- Table
- Data table
- Data table column header
- Chart
- Code Block
- Code card
- Markdown view
- Attachment
- Bubble
- Marker
- Message
- Chat message
- Chat suggestion item
- Message scroller
- Trace
- Progress
- Spinner
- Sonner
- Toast
- Unsaved changes toolbar
- Unsaved indicator
- Status indicator
- Permission card
- Separator
- Skeleton
- Animated grid pattern
- Meteors
Installation#
pnpm dlx shadcn@latest add @zui/carouselThe 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#
import { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious } from '@/components/ui/carousel';<Carousel className="mx-12">
<CarouselContent>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
<CarouselItem>...</CarouselItem>
</CarouselContent>
<CarouselPrevious />
<CarouselNext />
</Carousel>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#
Carousel#
Takes every prop of React Native's View, plus:
| Prop | Type | Default |
|---|---|---|
opts | CarouselOptions | - |
orientation | 'horizontal' | 'vertical' | 'horizontal' |
setApi | (api: CarouselApi | undefined) => void | - |
opts takes these of Embla's options, with Embla's meaning:
| Option | Type | Default |
|---|---|---|
align | 'start' | 'center' | 'end' | 'center' |
loop | boolean | false |
startIndex | number | 0 |
watchDrag | boolean | true |
containScroll | 'trimSnaps' | 'keepSnaps' | false | 'trimSnaps' |
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:
const [api, setApi] = useState<CarouselApi>();
const [current, setCurrent] = useState(0);
useEffect(() => {
api?.on('select', () => setCurrent(api.selectedScrollSnap() + 1));
}, [api]);
<Carousel setApi={setApi}>...</Carousel>;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.
optstakes the Embla options above, andpluginsis left out: Embla's plugins drive Embla's own engine. - The api leaves out Embla's
rootNode,containerNode,slideNodesandslideRegistry, which hand out DOM nodes React Native does not have, andinternalEngineandplugins, which are Embla's engine. Thescrollevent is left out because the scroll runs on the UI thread, and a listener on each frame would call into JavaScript on every one.reInitanddestroyare left out because React owns both: the carousel follows a newoptsand every new layout itself, and unmounting it destroys it.emitand theslidesChangedandslideFocusevents 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
classNamereaches 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.