UNPKG

@penaprieto/design-system

Version:

Multi-brand React design system with design tokens from Figma

102 lines (101 loc) 5.42 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Carousel = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importStar(require("react")); const Icon_1 = require("../Icon"); require("./Carousel.css"); const Carousel = ({ children, autoPlay = false, interval = 3000, showIndicators = true, showControls = true, loop = true, className = '', }) => { const [currentIndex, setCurrentIndex] = (0, react_1.useState)(0); const [isTransitioning, setIsTransitioning] = (0, react_1.useState)(false); const timerRef = (0, react_1.useRef)(undefined); const items = react_1.default.Children.toArray(children); const totalItems = items.length; const handleNext = (0, react_1.useCallback)(() => { if (isTransitioning) return; setIsTransitioning(true); setCurrentIndex((prev) => { if (prev === totalItems - 1) { return loop ? 0 : totalItems - 1; } return prev + 1; }); setTimeout(() => setIsTransitioning(false), 300); }, [isTransitioning, totalItems, loop]); (0, react_1.useEffect)(() => { if (autoPlay && totalItems > 1) { timerRef.current = window.setInterval(() => { handleNext(); }, interval); return () => { if (timerRef.current) { clearInterval(timerRef.current); } }; } }, [autoPlay, interval, totalItems, handleNext]); const handlePrev = () => { if (isTransitioning) return; setIsTransitioning(true); setCurrentIndex((prev) => { if (prev === 0) { return loop ? totalItems - 1 : 0; } return prev - 1; }); setTimeout(() => setIsTransitioning(false), 300); }; const handleIndicatorClick = (index) => { if (isTransitioning || index === currentIndex) return; setIsTransitioning(true); setCurrentIndex(index); setTimeout(() => setIsTransitioning(false), 300); }; const rootClassName = ['ds-carousel', className].filter(Boolean).join(' '); return ((0, jsx_runtime_1.jsxs)("div", { className: rootClassName, children: [(0, jsx_runtime_1.jsx)("div", { className: "ds-carousel__viewport", children: (0, jsx_runtime_1.jsx)("div", { className: "ds-carousel__track", style: { transform: `translateX(-${currentIndex * 100}%)`, transition: isTransitioning ? 'transform 0.3s ease-in-out' : 'none', }, children: items.map((item, index) => ((0, jsx_runtime_1.jsx)("div", { className: "ds-carousel__slide", children: item }, index))) }) }), showControls && totalItems > 1 && ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("button", { type: "button", className: "ds-carousel__control ds-carousel__control--prev", onClick: handlePrev, disabled: !loop && currentIndex === 0, "aria-label": "Previous slide", children: (0, jsx_runtime_1.jsx)(Icon_1.Icon, { name: "chevron-left", size: 24 }) }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "ds-carousel__control ds-carousel__control--next", onClick: handleNext, disabled: !loop && currentIndex === totalItems - 1, "aria-label": "Next slide", children: (0, jsx_runtime_1.jsx)(Icon_1.Icon, { name: "chevron-right", size: 24 }) })] })), showIndicators && totalItems > 1 && ((0, jsx_runtime_1.jsx)("div", { className: "ds-carousel__indicators", children: items.map((_, index) => ((0, jsx_runtime_1.jsx)("button", { type: "button", className: [ 'ds-carousel__indicator', index === currentIndex && 'ds-carousel__indicator--active', ] .filter(Boolean) .join(' '), onClick: () => handleIndicatorClick(index), "aria-label": `Go to slide ${index + 1}` }, index))) }))] })); }; exports.Carousel = Carousel;