Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

UIComponentsReact

Carousel

A row of slides that the reader scrolls through, by swiping, dragging, with the previous and next buttons or with the page buttons of the app's own.

Built on the headless Carousel.See its API in Docs

Rendered by React 19.3.1

carousel.tsx

Installation

npx shadcn@4.23.0 add https://hamur.dev/r/react/carousel.json

It also adds utils, button. The app needs its setup first: Installation.

Usage

Put CarouselItems in a CarouselContent inside Carousel, with a CarouselPrevious and a CarouselNext beside them, and give the Carousel a name (aria-label): it is a landmark, and a carousel on a page needs one to tell it from the next. Each item is a full-width slide; give it a basis-1/2 or basis-1/3 class to show several at once. orientation is horizontal or vertical (give a vertical CarouselContent a height). The options are the Carousel's own props: align, loop, slidesPerMove, snapType, defaultValue (the first slide), autoplay and the rest of the headless Root's. opts takes the Embla options that map onto them (align, loop, startIndex, slidesToScroll, axis, dragFree, watchDrag). setApi receives a handle when the carousel has rendered: scrollTo(index), next(), previous(), play(), stop(), value (the current slide, 0-based), pageCount, and on/off for 'select' and 'settle'. A carousel with autoplay rotates until the reader stops it, so render a CarouselAutoplayControl too; it is named by what pressing it does. The carousel is Base UI Next's Carousel: the viewport is a native scroll container with scroll snapping, so touch, trackpad and keyboard scrolling are the browser's, and the mouse drags it.

import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselPrevious,
  CarouselNext,
  CarouselAutoplayControl,
} from '@/components/ui/carousel'

Examples

Five square slides with previous and next buttons.

Its preview and code are at the top of the page.

Differences from shadcn's, which moved from Embla to the headless Carousel:

- No Embla. embla-carousel-react is no longer installed; the slides scroll in the viewport itself (CarouselContent is the scroller, so shadcn's overflow-hidden wrapper is gone and a className on CarouselContent lands on it, not on an inner container) and snap with CSS. The scrollbar is hidden. - **opts** keeps align, loop (the buttons rewind across the ends; there is no seamless loop), startIndex, slidesToScroll, axis, dragFree (proximity snapping) and watchDrag (a boolean: the mouse drag, on by default). duration, containScroll (the last page always ends at the end), skipSnaps (use CSS scroll-snap-stop), inViewThreshold, breakpoints (use CSS), watchResize, watchSlides, watchFocus, dragThreshold and direction (use DirectionProvider) have no equivalent. Angular takes the Root's inputs in place of opts. - **plugins** is gone; Embla's Autoplay plugin is the autoplay prop with autoplayInterval, and the new CarouselAutoplayControl export is the stop and start button WCAG 2.2.2 asks for. Rotation holds while the pointer is over the carousel, stops when keyboard focus enters it, and starts stopped under prefers-reduced-motion. - **setApi** hands out the Root's actions with value, pageCount and on/off for 'select' and 'settle', not Embla's API (scrollSnapList(), selectedScrollSnap(), canScrollPrev() and the rest are gone; use value + 1 of pageCount). 'select' listeners run when the carousel changes its slide (a button, a swipe, a drag, scrollTo), not when a controlled value changes from outside; pageCount is 0 until the slides are registered. useCarousel returns api, orientation, scrollPrev and scrollNext, without carouselRef and opts. - The arrow keys no longer move the carousel from anywhere inside it (shadcn's handler also took them from inputs in a slide). They belong to a focused viewport, which scrolls natively, and to the headless TabList when the app renders one. - The buttons are aria-disabled at the first and the last page and keep focus, where shadcn's are disabled; both are disabled with fewer than two pages. CarouselPrevious and CarouselNext move by a page (the slides in view), not by a slide. - Off-screen slides are inert, so Tab never reaches a link in a slide that is not in view; offscreenSlides="interactive" turns it off. Each slide is named "N of M". - Landmark. The root is a region with the role description "carousel" and needs a name; pass role="group" if it is not landmark-worthy.

The chevrons are lucide's ChevronLeft and ChevronRight, inline; the autoplay button's play and pause are lucide's, inline.

API reference

The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Carousel's API reference.