Skip to documentation
Design system / Sources & standards
Current repository source
carousel.tsx
src/components/ui/carousel.tsx
This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.
"use client"
import * as React from "react"
import { cn } from "@/lib/utils"
import { useCarouselBehavior, type CarouselApi, type CarouselProps } from "./useCarouselBehavior"
import { ArrowLeft, ArrowRight } from "lucide-react"
import { Button } from "@/components/ui/button"
type CarouselContextProps = {
carouselRef: ReturnType<typeof useCarouselBehavior>["carouselRef"]
api: CarouselApi
scrollPrev: () => void
scrollNext: () => void
canScrollPrev: boolean
canScrollNext: boolean
selectedIndex: number
snapCount: number
scrollTo: (index: number) => void
} & CarouselProps
const CarouselContext = React.createContext<CarouselContextProps | null>(null)
function useCarousel() {
const context = React.useContext(CarouselContext)
if (!context) {
throw new Error("useCarousel must be used within a <Carousel />")
}
return context
}
function Carousel({
orientation = "horizontal",
opts,
setApi,
plugins,
className,
children,
...props
}: React.ComponentProps<"div"> & CarouselProps) {
const { carouselRef, api, canScrollPrev, canScrollNext, selectedIndex, snapCount, scrollPrev, scrollNext, scrollTo, handleKeyDown } =
useCarouselBehavior({ orientation, opts, plugins, setApi })
return (
<CarouselContext.Provider
value={{
carouselRef,
api: api,
opts,
orientation:
orientation || (opts?.axis === "y" ? "vertical" : "horizontal"),
scrollPrev,
scrollNext,
canScrollPrev,
canScrollNext,
selectedIndex,
snapCount,
scrollTo,
}}
>
<div
onKeyDownCapture={handleKeyDown}
className={cn("relative", className)}
role="region"
aria-roledescription="carousel"
data-slot="carousel"
{...props}
>
{children}
</div>
</CarouselContext.Provider>
)
}
function CarouselContent({ className, viewportClassName, ...props }: React.ComponentProps<"div"> & { viewportClassName?: string }) {
const { carouselRef, orientation } = useCarousel()
return (
<div
ref={carouselRef}
className={cn("overflow-hidden", viewportClassName)}
data-slot="carousel-content"
>
<div
className={cn(
"flex",
orientation === "horizontal" ? "-ml-4" : "-mt-4 flex-col",
className
)}
{...props}
/>
</div>
)
}
function CarouselItem({ className, ...props }: React.ComponentProps<"div">) {
const { orientation } = useCarousel()
return (
<div
role="group"
aria-roledescription="slide"
data-slot="carousel-item"
className={cn(
"min-w-0 shrink-0 grow-0 basis-full",
orientation === "horizontal" ? "pl-4" : "pt-4",
className
)}
{...props}
/>
)
}
/** A media-sized hover/focus owner; no measurement or pointer listeners. */
function CarouselMedia({ className, ...props }: React.ComponentProps<"div">) {
return <div data-slot="carousel-media" className={cn("group/carousel-media relative", className)} {...props} />
}
/** Omit slideIndex for one stable control pair on a positioned carousel owner. */
function CarouselOverlayControls({ slideIndex }: { slideIndex?: number }) {
const { selectedIndex } = useCarousel()
const active = slideIndex === undefined || selectedIndex === slideIndex
const visibility = "hidden md:[@media(hover:hover)_and_(pointer:fine)]:inline-flex pointer-events-none opacity-0 group-hover/carousel-media:pointer-events-auto group-hover/carousel-media:opacity-100 group-focus-within/carousel-media:pointer-events-auto group-focus-within/carousel-media:opacity-100 motion-safe:transition-opacity motion-reduce:transition-none"
return <div data-slot="carousel-overlay-controls" aria-hidden={!active}>
<CarouselPrevious tabIndex={active ? 0 : -1} className={cn("left-3 z-10", visibility)} />
<CarouselNext tabIndex={active ? 0 : -1} className={cn("right-3 z-10", visibility)} />
</div>
}
function CarouselIndicators({ className, ...props }: React.ComponentProps<"div">) {
const { selectedIndex, snapCount, scrollTo } = useCarousel()
if (snapCount < 2) return null
return <div role="group" aria-label="Slide navigation" data-slot="carousel-indicators" className={cn("flex min-w-0 flex-wrap items-center justify-center gap-1", className)} {...props}>
{Array.from({ length: snapCount }, (_, index) => <Button key={index} type="button" variant="ghost" size="icon-xs" className="rounded-full" aria-label={`Go to slide ${index + 1}`} aria-pressed={selectedIndex === index} onClick={() => scrollTo(index)}>
<span aria-hidden="true" className={cn("size-1.5 rounded-full", selectedIndex === index ? "bg-foreground" : "bg-muted-foreground")} />
</Button>)}
</div>
}
function CarouselPrevious({
className,
variant = "outline",
size = "icon",
...props
}: React.ComponentProps<typeof Button>) {
const { orientation, scrollPrev, canScrollPrev } = useCarousel()
if (!canScrollPrev) return null
return (
<Button
type="button"
data-slot="carousel-previous"
variant={variant}
size={size}
className={cn(
"absolute size-8 rounded-full",
orientation === "horizontal"
? "top-1/2 -left-12 -translate-y-1/2"
: "-top-12 left-1/2 -translate-x-1/2 rotate-90",
className
)}
disabled={!canScrollPrev}
onClick={scrollPrev}
{...props}
>
<ArrowLeft />
<span className="sr-only">Previous slide</span>
</Button>
)
}
function CarouselNext({
className,
variant = "outline",
size = "icon",
...props
}: React.ComponentProps<typeof Button>) {
const { orientation, scrollNext, canScrollNext } = useCarousel()
if (!canScrollNext) return null
return (
<Button
type="button"
data-slot="carousel-next"
variant={variant}
size={size}
className={cn(
"absolute size-8 rounded-full",
orientation === "horizontal"
? "top-1/2 -right-12 -translate-y-1/2"
: "-bottom-12 left-1/2 -translate-x-1/2 rotate-90",
className
)}
disabled={!canScrollNext}
onClick={scrollNext}
{...props}
>
<ArrowRight />
<span className="sr-only">Next slide</span>
</Button>
)
}
export {
type CarouselApi,
Carousel,
CarouselContent,
CarouselItem,
CarouselPrevious,
CarouselNext,
CarouselMedia,
CarouselOverlayControls,
CarouselIndicators,
useCarousel,
}
All sources and standards