carousel
shadcn/carouselFreeComponent
shadcn publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by shadcn on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add carouselSource
1"use client"23import * as React from "react"4import useEmblaCarousel, {5 type UseEmblaCarouselType,6} from "embla-carousel-react"7import { ArrowLeft, ArrowRight } from "lucide-react"89import { cn } from "@/lib/utils"10import { Button } from "@/registry/new-york/ui/button"1112type CarouselApi = UseEmblaCarouselType[1]13type UseCarouselParameters = Parameters<typeof useEmblaCarousel>14type CarouselOptions = UseCarouselParameters[0]15type CarouselPlugin = UseCarouselParameters[1]1617type CarouselProps = {18 opts?: CarouselOptions19 plugins?: CarouselPlugin20 orientation?: "horizontal" | "vertical"21 setApi?: (api: CarouselApi) => void22}2324type CarouselContextProps = {25 carouselRef: ReturnType<typeof useEmblaCarousel>[0]26 api: ReturnType<typeof useEmblaCarousel>[1]27 scrollPrev: () => void28 scrollNext: () => void29 canScrollPrev: boolean30 canScrollNext: boolean31} & CarouselProps3233const CarouselContext = React.createContext<CarouselContextProps | null>(null)3435function useCarousel() {36 const context = React.useContext(CarouselContext)3738 if (!context) {39 throw new Error("useCarousel must be used within a <Carousel />")40 }4142 return context43}4445const Carousel = React.forwardRef<46 HTMLDivElement,47 React.HTMLAttributes<HTMLDivElement> & CarouselProps48>(49 (50 {51 orientation = "horizontal",52 opts,53 setApi,54 plugins,55 className,56 children,57 ...props58 },59 ref60 ) => {61 const [carouselRef, api] = useEmblaCarousel(62 {63 ...opts,64 axis: orientation === "horizontal" ? "x" : "y",65 },66 plugins67 )68 const [canScrollPrev, setCanScrollPrev] = React.useState(false)69 const [canScrollNext, setCanScrollNext] = React.useState(false)7071 const onSelect = React.useCallback((api: CarouselApi) => {72 if (!api) {73 return74 }7576 setCanScrollPrev(api.canScrollPrev())77 setCanScrollNext(api.canScrollNext())78 }, [])7980 const scrollPrev = React.useCallback(() => {81 api?.scrollPrev()82 }, [api])8384 const scrollNext = React.useCallback(() => {85 api?.scrollNext()86 }, [api])8788 const handleKeyDown = React.useCallback(89 (event: React.KeyboardEvent<HTMLDivElement>) => {90 if (event.key === "ArrowLeft") {91 event.preventDefault()92 scrollPrev()93 } else if (event.key === "ArrowRight") {94 event.preventDefault()95 scrollNext()96 }97 },98 [scrollPrev, scrollNext]99 )100101 React.useEffect(() => {102// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from shadcn
All 62 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | shadcn | Components | Free | no deps | |
| alertalert | shadcn | Components | Free | no deps | |
| alert-dialogalert-dialog | shadcn | Components | Free | no deps | |
| aspect-ratioaspect-ratio | shadcn | Components | Free | no deps | |
| attachmentattachment | shadcn | Components | Free | no deps | |
| avataravatar | shadcn | Components | Free | no deps | |
| badgebadge | shadcn | Components | Free | no deps | |
| breadcrumbbreadcrumb | shadcn | Components | Free | no deps |
