carousel
agentmesh/carouselFreeComponent
shadcn/ui publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by shadcn/ui on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://raw.githubusercontent.com/ombhanushaliii/agentmesh/main/example/ui/apps/v4/public/r/styles/base-vega/carousel.jsonSource
1"use client"23import * as React from "react"4import useEmblaCarousel, {5 type UseEmblaCarouselType,6} from "embla-carousel-react"78import { cn } from "@/registry/base-vega/lib/utils"9import { Button } from "@/registry/base-vega/ui/button"10import { IconPlaceholder } from "@/app/(create)/components/icon-placeholder"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}4445function Carousel({46 orientation = "horizontal",47 opts,48 setApi,49 plugins,50 className,51 children,52 ...props53}: React.ComponentProps<"div"> & CarouselProps) {54 const [carouselRef, api] = useEmblaCarousel(55 {56 ...opts,57 axis: orientation === "horizontal" ? "x" : "y",58 },59 plugins60 )61 const [canScrollPrev, setCanScrollPrev] = React.useState(false)62 const [canScrollNext, setCanScrollNext] = React.useState(false)6364 const onSelect = React.useCallback((api: CarouselApi) => {65 if (!api) return66 setCanScrollPrev(api.canScrollPrev())67 setCanScrollNext(api.canScrollNext())68 }, [])6970 const scrollPrev = React.useCallback(() => {71 api?.scrollPrev()72 }, [api])7374 const scrollNext = React.useCallback(() => {75 api?.scrollNext()76 }, [api])7778 const handleKeyDown = React.useCallback(79 (event: React.KeyboardEvent<HTMLDivElement>) => {80 if (event.key === "ArrowLeft") {81 event.preventDefault()82 scrollPrev()83 } else if (event.key === "ArrowRight") {84 event.preventDefault()85 scrollNext()86 }87 },88 [scrollPrev, scrollNext]89 )9091 React.useEffect(() => {92 if (!api || !setApi) return93 setApi(api)94 }, [api, setApi])9596 React.useEffect(() => {97 if (!api) return98 onSelect(api)99// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from shadcn/ui
All 201 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| accordionaccordion | shadcn/ui | Components | Free | no deps | |
| alertalert | shadcn/ui | Components | Free | no deps | |
| alert-dialogalert-dialog | shadcn/ui | Components | Free | no deps | |
| aspect-ratioaspect-ratio | shadcn/ui | Components | Free | no deps | |
| avataravatar | shadcn/ui | Components | Free | no deps | |
| badgebadge | shadcn/ui | Components | Free | no deps | |
| breadcrumbbreadcrumb | shadcn/ui | Components | Free | no deps | |
| buttonbutton | shadcn/ui | Components | Free | no deps |
