8-bit Card
8bitcn/carouselFreeComponent
A simple 8-bit carousel component
Install it
npx shadcn@latest add https://www.8bitcn.com/r/carousel.jsonSource
1"use client";23import * as React from "react";45import useEmblaCarousel, {6 type UseEmblaCarouselType,7} from "embla-carousel-react";89import { cn } from "@/lib/utils";1011import { Button } from "@/components/ui/8bit/button";1213type CarouselApi = UseEmblaCarouselType[1];14type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;15type CarouselOptions = UseCarouselParameters[0];16type CarouselPlugin = UseCarouselParameters[1];1718type CarouselProps = {19 opts?: CarouselOptions;20 plugins?: CarouselPlugin;21 orientation?: "horizontal" | "vertical";22 setApi?: (api: CarouselApi) => void;23};2425type 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;3334const 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 context;44}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);7172 const onSelect = React.useCallback((api: CarouselApi) => {73 if (!api) {74 return;75 }7677 setCanScrollPrev(api.canScrollPrev());78 setCanScrollNext(api.canScrollNext());79 }, []);8081 const scrollPrev = React.useCallback(() => {82 console.log("scrolled prev");83 api?.scrollPrev();84 }, [api]);8586 const scrollNext = React.useCallback(() => {87 console.log("scrolled next");88 api?.scrollNext();89 }, [api]);9091 const handleKeyDown = React.useCallback(92 (event: React.KeyboardEvent<HTMLDivElement>) => {93 if (event.key === "ArrowLeft") {94 event.preventDefault();95 scrollPrev();96 } else if (event.key === "ArrowRight") {97 event.preventDefault();98 scrollNext();99 }100 },101// … truncated
What it pulls in
Other registry items
Files it writes
More from 8bitcn
All 121 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| 8-bit Accordionaccordion | 8bitcn | Components | Free | no deps · 2 files | |
| 8-bit Alertalert | 8bitcn | Components | Free | no deps · 2 files | |
| 8-bit Alert Dialogalert-dialog | 8bitcn | Components | Free | no deps · 2 files | |
| 8-bit Avataravatar | 8bitcn | Components | Free | no deps · 2 files | |
| 8-bit Badgebadge | 8bitcn | Components | Free | no deps · 2 files | |
| 8-bit Breadcrumbbreadcrumb | 8bitcn | Components | Free | no deps · 2 files | |
| 8-bit Buttonbutton | 8bitcn | Components | Free | no deps · 2 files | |
| 8-bit Button Groupbutton-group | 8bitcn | Components | Free | no deps · 4 files |
