BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/9ui/carousel

carousel

9ui/carousel
FreeComponent

9ui publishes no description for this item.

Live preview

live · rendered by the registry
Rendered by 9ui on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add https://9ui.dev/r/carousel.json

Source

ui/carousel.tsx9ui.dev/r/carousel.json
1"use client"
2
3import * as React from "react"
4import useEmblaCarousel, {
5 type UseEmblaCarouselType,
6} from "embla-carousel-react"
7import { ArrowLeft, ArrowRight } from "lucide-react"
8
9import { Button } from "@/components/ui/button"
10
11import { cn } from "@/lib/utils"
12
13type CarouselApi = UseEmblaCarouselType[1]
14type UseCarouselParameters = Parameters<typeof useEmblaCarousel>
15type CarouselOptions = UseCarouselParameters[0]
16type CarouselPlugin = UseCarouselParameters[1]
17
18type CarouselProps = {
19 opts?: CarouselOptions
20 plugins?: CarouselPlugin
21 orientation?: "horizontal" | "vertical"
22 setApi?: (api: CarouselApi) => void
23}
24
25type CarouselContextProps = {
26 carouselRef: ReturnType<typeof useEmblaCarousel>[0]
27 api: ReturnType<typeof useEmblaCarousel>[1]
28 scrollPrev: () => void
29 scrollNext: () => void
30 canScrollPrev: boolean
31 canScrollNext: boolean
32} & CarouselProps
33
34const CarouselContext = React.createContext<CarouselContextProps | null>(null)
35
36function useCarousel() {
37 const context = React.useContext(CarouselContext)
38
39 if (!context) {
40 throw new Error("useCarousel must be used within a <Carousel />")
41 }
42
43 return context
44}
45
46function Carousel({
47 orientation = "horizontal",
48 opts,
49 setApi,
50 plugins,
51 className,
52 children,
53 ...props
54}: React.ComponentProps<"div"> & CarouselProps) {
55 const [carouselRef, api] = useEmblaCarousel(
56 {
57 ...opts,
58 axis: orientation === "horizontal" ? "x" : "y",
59 },
60 plugins
61 )
62 const [canScrollPrev, setCanScrollPrev] = React.useState(false)
63 const [canScrollNext, setCanScrollNext] = React.useState(false)
64
65 const onSelect = React.useCallback((api: CarouselApi) => {
66 if (!api) return
67 setCanScrollPrev(api.canScrollPrev())
68 setCanScrollNext(api.canScrollNext())
69 }, [])
70
71 const scrollPrev = React.useCallback(() => {
72 api?.scrollPrev()
73 }, [api])
74
75 const scrollNext = React.useCallback(() => {
76 api?.scrollNext()
77 }, [api])
78
79 const handleKeyDown = React.useCallback(
80 (event: React.KeyboardEvent<HTMLDivElement>) => {
81 if (event.key === "ArrowLeft") {
82 event.preventDefault()
83 scrollPrev()
84 } else if (event.key === "ArrowRight") {
85 event.preventDefault()
86 scrollNext()
87 }
88 },
89 [scrollPrev, scrollNext]
90 )
91
92 React.useEffect(() => {
93 if (!api || !setApi) return
94 setApi(api)
95 }, [api, setApi])
96
97 React.useEffect(() => {
98 if (!api) return
99 onSelect(api)
100 api.on("reInit", onSelect)
101 api.on("select", onSelect)
102
103 return () => {
104 api?.off("select", onSelect)
105 }
106 }, [api, onSelect])
107
108 return (
109// … truncated

What it pulls in

npm packages

  • embla-carousel
  • embla-carousel-react

Other registry items

  • @9ui/button

Files it writes

  • src/components/ui/carousel.tsx

Facts

Registry
9ui
Type
registry:ui
Access
Free
Files written
1
Licence
MIT
First indexed
2026-08-03
Last confirmed
today

Go to the source

Docs on 9ui 9ui.dev borabaloglu/9ui on GitHub Raw registry item This item as JSON

More from 9ui

All 56 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
accordionaccordion9uiComponentsFreeno deps
alertalert9uiComponentsFreeno deps
alert-dialogalert-dialog9uiComponentsFreeno deps
aspect-ratioaspect-ratio9uiComponentsFreeno deps
autocompleteautocomplete9uiComponentsFreeno deps
avataravatar9uiComponentsFreeno deps
badgebadge9uiComponentsFreeno deps
breadcrumbsbreadcrumbs9uiComponentsFreeno deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex