Carousel
boldkit/carouselFreeComponent
Carousel component with navigation, dots, and touch support built on Embla
Live preview
live · rendered by the registry
Rendered by boldkit on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://boldkit.dev/r/carousel.jsonSource
1/* eslint-disable react-refresh/only-export-components */2import * as React from 'react'3import useEmblaCarousel, {4 type UseEmblaCarouselType,5} from 'embla-carousel-react'6import { ArrowLeft, ArrowRight } from 'lucide-react'7import { cn } from '@/lib/utils'8import { Button } from '@/components/ui/button'910type CarouselApi = UseEmblaCarouselType[1]11type UseCarouselParameters = Parameters<typeof useEmblaCarousel>12type CarouselOptions = UseCarouselParameters[0]13type CarouselPlugin = UseCarouselParameters[1]1415interface CarouselProps {16 opts?: CarouselOptions17 plugins?: CarouselPlugin18 orientation?: 'horizontal' | 'vertical'19 setApi?: (api: CarouselApi) => void20}2122type CarouselContextProps = {23 carouselRef: ReturnType<typeof useEmblaCarousel>[0]24 api: ReturnType<typeof useEmblaCarousel>[1]25 scrollPrev: () => void26 scrollNext: () => void27 canScrollPrev: boolean28 canScrollNext: boolean29 selectedIndex: number30 scrollSnaps: number[]31 scrollTo: (index: number) => void32} & CarouselProps3334const CarouselContext = React.createContext<CarouselContextProps | null>(null)3536function useCarousel() {37 const context = React.useContext(CarouselContext)3839 if (!context) {40 throw new Error('useCarousel must be used within a <Carousel />')41 }4243 return context44}4546const Carousel = React.forwardRef<47 HTMLDivElement,48 React.HTMLAttributes<HTMLDivElement> & CarouselProps49>(50 (51 {52 orientation = 'horizontal',53 opts,54 setApi,55 plugins,56 className,57 children,58 ...props59 },60 ref61 ) => {62 const [carouselRef, api] = useEmblaCarousel(63 {64 ...opts,65 axis: orientation === 'horizontal' ? 'x' : 'y',66 },67 plugins68 )69 const [canScrollPrev, setCanScrollPrev] = React.useState(false)70 const [canScrollNext, setCanScrollNext] = React.useState(false)71 const [selectedIndex, setSelectedIndex] = React.useState(0)72 const [scrollSnaps, setScrollSnaps] = React.useState<number[]>([])7374 const onSelect = React.useCallback((api: CarouselApi) => {75 if (!api) return7677 setSelectedIndex(api.selectedScrollSnap())78 setCanScrollPrev(api.canScrollPrev())79 setCanScrollNext(api.canScrollNext())80 }, [])8182 const scrollPrev = React.useCallback(() => {83 api?.scrollPrev()84 }, [api])8586 const scrollNext = React.useCallback(() => {87 api?.scrollNext()88 }, [api])8990 const scrollTo = React.useCallback(91 (index: number) => {92 api?.scrollTo(index)93 },94// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from boldkit
All 94 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | boldkit | Components | Free | 2 deps | |
| Alertalert | boldkit | Components | Free | 2 deps | |
| Alert Dialogalert-dialog | boldkit | Components | Free | 1 dep | |
| ASCII Shapesascii-shapes | boldkit | Components | Free | no deps | |
| Aspect Ratioaspect-ratio | boldkit | Components | Free | 1 dep | |
| Avataravatar | boldkit | Components | Free | 1 dep | |
| Badgebadge | boldkit | Components | Free | 1 dep | |
| Breadcrumbbreadcrumb | boldkit | Components | Free | 2 deps |
