UNPKG

@arolariu/components

Version:

🎨 60+ beautiful, accessible React components built on Radix UI. TypeScript-first, tree-shakeable, SSR-ready. Perfect for modern web apps, design systems & rapid prototyping. Zero config, maximum flexibility! ⚡

151 lines (150 loc) • 5.18 kB
"use client"; import { jsx, jsxs } from "react/jsx-runtime"; import { createContext, useCallback, useContext, useEffect, useState } from "react"; import embla_carousel_react from "embla-carousel-react"; import { ArrowLeft, ArrowRight } from "lucide-react"; import { cn } from "../../lib/utils.js"; import { Button } from "./button.js"; const CarouselContext = /*#__PURE__*/ createContext(null); function useCarousel() { const context = useContext(CarouselContext); if (!context) throw new Error("useCarousel must be used within a <Carousel />"); return context; } function Carousel({ orientation = "horizontal", opts, setApi, plugins, className, children, ...props }) { const [carouselRef, api] = embla_carousel_react({ ...opts, axis: "horizontal" === orientation ? "x" : "y" }, plugins); const [canScrollPrev, setCanScrollPrev] = useState(false); const [canScrollNext, setCanScrollNext] = useState(false); const onSelect = useCallback((api)=>{ if (!api) return; setCanScrollPrev(api.canScrollPrev()); setCanScrollNext(api.canScrollNext()); }, []); const scrollPrev = useCallback(()=>{ api?.scrollPrev(); }, [ api ]); const scrollNext = useCallback(()=>{ api?.scrollNext(); }, [ api ]); const handleKeyDown = useCallback((event)=>{ if ("ArrowLeft" === event.key) { event.preventDefault(); scrollPrev(); } else if ("ArrowRight" === event.key) { event.preventDefault(); scrollNext(); } }, [ scrollPrev, scrollNext ]); useEffect(()=>{ if (!api || !setApi) return; setApi(api); }, [ api, setApi ]); useEffect(()=>{ if (!api) return; onSelect(api); api.on("reInit", onSelect); api.on("select", onSelect); return ()=>{ api?.off("select", onSelect); }; }, [ api, onSelect ]); return /*#__PURE__*/ jsx(CarouselContext.Provider, { value: { carouselRef, api: api, opts, orientation: orientation || (opts?.axis === "y" ? "vertical" : "horizontal"), scrollPrev, scrollNext, canScrollPrev, canScrollNext }, children: /*#__PURE__*/ jsx("div", { onKeyDownCapture: handleKeyDown, className: cn("relative", className), role: "region", "aria-roledescription": "carousel", "data-slot": "carousel", ...props, children: children }) }); } function CarouselContent({ className, ...props }) { const { carouselRef, orientation } = useCarousel(); return /*#__PURE__*/ jsx("div", { ref: carouselRef, className: "overflow-hidden", "data-slot": "carousel-content", children: /*#__PURE__*/ jsx("div", { className: cn("flex", "horizontal" === orientation ? "-ml-4" : "-mt-4 flex-col", className), ...props }) }); } function CarouselItem({ className, ...props }) { const { orientation } = useCarousel(); return /*#__PURE__*/ jsx("div", { role: "group", "aria-roledescription": "slide", "data-slot": "carousel-item", className: cn("min-w-0 shrink-0 grow-0 basis-full", "horizontal" === orientation ? "pl-4" : "pt-4", className), ...props }); } function CarouselPrevious({ className, variant = "outline", size = "icon", ...props }) { const { orientation, scrollPrev, canScrollPrev } = useCarousel(); return /*#__PURE__*/ jsxs(Button, { "data-slot": "carousel-previous", variant: variant, size: size, className: cn("absolute size-8 rounded-full", "horizontal" === orientation ? "top-1/2 -left-12 -translate-y-1/2" : "-top-12 left-1/2 -translate-x-1/2 rotate-90", className), disabled: !canScrollPrev, onClick: scrollPrev, ...props, children: [ /*#__PURE__*/ jsx(ArrowLeft, {}), /*#__PURE__*/ jsx("span", { className: "sr-only", children: "Previous slide" }) ] }); } function CarouselNext({ className, variant = "outline", size = "icon", ...props }) { const { orientation, scrollNext, canScrollNext } = useCarousel(); return /*#__PURE__*/ jsxs(Button, { "data-slot": "carousel-next", variant: variant, size: size, className: cn("absolute size-8 rounded-full", "horizontal" === orientation ? "top-1/2 -right-12 -translate-y-1/2" : "-bottom-12 left-1/2 -translate-x-1/2 rotate-90", className), disabled: !canScrollNext, onClick: scrollNext, ...props, children: [ /*#__PURE__*/ jsx(ArrowRight, {}), /*#__PURE__*/ jsx("span", { className: "sr-only", children: "Next slide" }) ] }); } export { Carousel, CarouselContent, CarouselItem, CarouselNext, CarouselPrevious }; //# sourceMappingURL=carousel.js.map