Pixelact Carousel
pixelact-ui/carouselFreeComponent
A simple carousel component.
Live preview
live · rendered by the registry
Rendered by Pixelact UI on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://pixelactui.com/r/carousel.jsonSource
1import * as React from "react";2import useEmblaCarousel, {3 type UseEmblaCarouselType,4} from "embla-carousel-react";56import { cn } from "@/lib/utils";7import { Button } from "@/components/ui/button";8import "@/components/ui/pixelact-ui/styles/styles.css";910type CarouselApi = UseEmblaCarouselType[1];11type UseCarouselParameters = Parameters<typeof useEmblaCarousel>;12type CarouselOptions = UseCarouselParameters[0];13type CarouselPlugin = UseCarouselParameters[1];1415type CarouselProps = {16 opts?: CarouselOptions;17 plugins?: CarouselPlugin;18 orientation?: "horizontal" | "vertical";19 setApi?: (api: CarouselApi) => void;20};2122type CarouselContextProps = {23 carouselRef: ReturnType<typeof useEmblaCarousel>[0];24 api: ReturnType<typeof useEmblaCarousel>[1];25 scrollPrev: () => void;26 scrollNext: () => void;27 canScrollPrev: boolean;28 canScrollNext: boolean;29} & CarouselProps;3031const CarouselContext = React.createContext<CarouselContextProps | null>(null);3233function useCarousel() {34 const context = React.useContext(CarouselContext);3536 if (!context) {37 throw new Error("useCarousel must be used within a <Carousel />");38 }3940 return context;41}4243function Carousel({44 orientation = "horizontal",45 opts,46 setApi,47 plugins,48 className,49 children,50 ...props51}: React.ComponentProps<"div"> & CarouselProps) {52 const [carouselRef, api] = useEmblaCarousel(53 {54 ...opts,55 axis: orientation === "horizontal" ? "x" : "y",56 },57 plugins58 );59 const [canScrollPrev, setCanScrollPrev] = React.useState(false);60 const [canScrollNext, setCanScrollNext] = React.useState(false);6162 const onSelect = React.useCallback((api: CarouselApi) => {63 if (!api) return;64 setCanScrollPrev(api.canScrollPrev());65 setCanScrollNext(api.canScrollNext());66 }, []);6768 const scrollPrev = React.useCallback(() => {69 api?.scrollPrev();70 }, [api]);7172 const scrollNext = React.useCallback(() => {73 api?.scrollNext();74 }, [api]);7576 const handleKeyDown = React.useCallback(77 (event: React.KeyboardEvent<HTMLDivElement>) => {78 if (event.key === "ArrowLeft") {79 event.preventDefault();80 scrollPrev();81 } else if (event.key === "ArrowRight") {82 event.preventDefault();83 scrollNext();84 }85 },86 [scrollPrev, scrollNext]87 );8889 React.useEffect(() => {90 if (!api || !setApi) return;91 setApi(api);92 }, [api, setApi]);9394 React.useEffect(() => {95 if (!api) return;96 onSelect(api);97 api.on("reInit", onSelect);98// … truncated
What it pulls in
Other registry items
Files it writes
More from Pixelact UI
All 28 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Pixelact Accordionaccordion | Pixelact UI | Components | Free | no deps · 2 files | |
| Pixelact Alertalert | Pixelact UI | Components | Free | no deps · 2 files | |
| Pixelact Alert Dialogalert-dialog | Pixelact UI | Components | Free | no deps · 2 files | |
| Pixelact Avataravatar | Pixelact UI | Components | Free | no deps · 2 files | |
| Pixelact Badgebadge | Pixelact UI | Components | Free | no deps · 2 files | |
| Pixelact Breadcrumbbreadcrumb | Pixelact UI | Components | Free | no deps · 2 files | |
| Pixelact Buttonbutton | Pixelact UI | Components | Free | no deps · 3 files | |
| Pixelact Calendarcalendar | Pixelact UI | Components | Free | no deps · 2 files |
