carousel
nrjdalal/carouselFreeComponent
acme publishes no description for this item.
Install it
npx shadcn@latest add https://nrjdalal.dev/r/carousel.jsonSource
1"use client"23import { Button } from "@/components/ui/button"4import { cn } from "@/lib/utils"5import useEmblaCarousel, {6 type UseEmblaCarouselType,7} from "embla-carousel-react"8import { ArrowLeft, ArrowRight } from "lucide-react"9import * as React from "react"1011type CarouselApi = UseEmblaCarouselType[1]12type UseCarouselParameters = Parameters<typeof useEmblaCarousel>13type CarouselOptions = UseCarouselParameters[0]14type CarouselPlugin = UseCarouselParameters[1]1516type CarouselProps = {17 opts?: CarouselOptions18 plugins?: CarouselPlugin19 orientation?: "horizontal" | "vertical"20 setApi?: (api: CarouselApi) => void21}2223type CarouselContextProps = {24 carouselRef: ReturnType<typeof useEmblaCarousel>[0]25 api: ReturnType<typeof useEmblaCarousel>[1]26 scrollPrev: () => void27 scrollNext: () => void28 canScrollPrev: boolean29 canScrollNext: boolean30} & CarouselProps3132const CarouselContext = React.createContext<CarouselContextProps | null>(null)3334function useCarousel() {35 const context = React.useContext(CarouselContext)3637 if (!context) {38 throw new Error("useCarousel must be used within a <Carousel />")39 }4041 return context42}4344function Carousel({45 orientation = "horizontal",46 opts,47 setApi,48 plugins,49 className,50 children,51 ...props52}: React.ComponentProps<"div"> & CarouselProps) {53 const [carouselRef, api] = useEmblaCarousel(54 {55 ...opts,56 axis: orientation === "horizontal" ? "x" : "y",57 },58 plugins,59 )60 const [canScrollPrev, setCanScrollPrev] = React.useState(false)61 const [canScrollNext, setCanScrollNext] = React.useState(false)6263 const onSelect = React.useCallback((api: CarouselApi) => {64 if (!api) return65 setCanScrollPrev(api.canScrollPrev())66 setCanScrollNext(api.canScrollNext())67 }, [])6869 const scrollPrev = React.useCallback(() => {70 api?.scrollPrev()71 }, [api])7273 const scrollNext = React.useCallback(() => {74 api?.scrollNext()75 }, [api])7677 const handleKeyDown = React.useCallback(78 (event: React.KeyboardEvent<HTMLDivElement>) => {79 if (event.key === "ArrowLeft") {80 event.preventDefault()81 scrollPrev()82 } else if (event.key === "ArrowRight") {83 event.preventDefault()84 scrollNext()85 }86 },87 [scrollPrev, scrollNext],88 )8990 React.useEffect(() => {91 if (!api || !setApi) return92 setApi(api)93 }, [api, setApi])9495 React.useEffect(() => {96 if (!api) return97 onSelect(api)98 api.on("reInit", onSelect)99 api.on("select", onSelect)100101// … truncated
What it pulls in
npm packages
Files it writes
More from acme
All 49 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | acme | Components | Free | 5 deps · 2 files | |
| alertalert | acme | Components | Free | 4 deps · 2 files | |
| alert-dialogalert-dialog | acme | Components | Free | 6 deps · 3 files | |
| aspect-ratioaspect-ratio | acme | Components | Free | 1 dep | |
| avataravatar | acme | Components | Free | 4 deps · 2 files | |
| badgebadge | acme | Components | Free | 5 deps · 2 files | |
| breadcrumbbreadcrumb | acme | Components | Free | 5 deps · 2 files | |
| buttonbutton | acme | Components | Free | 5 deps · 2 files |
