UNPKG

@navinc/base-react-components

Version:
165 lines (162 loc) 7.96 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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Carousel = exports.CarouselBase = exports.useCarousel = void 0; const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importStar(require("react")); const styled_components_1 = __importDefault(require("styled-components")); const icon_js_1 = require("./icon.js"); const CarouselContainer = styled_components_1.default.div.withConfig({ displayName: "brc-sc-CarouselContainer", componentId: "brc-sc-4dh4b8" }) ` position: relative; height: 100%; width: 100%; display: flex; padding: 0 62px 36px; `; const CarouselWrapper = styled_components_1.default.div.withConfig({ displayName: "brc-sc-CarouselWrapper", componentId: "brc-sc-17khwms" }) ` position: relative; overflow: hidden; height: 100%; width: 100%; flex-shrink: 0; `; const CarouselSlides = styled_components_1.default.div.withConfig({ displayName: "brc-sc-CarouselSlides", componentId: "brc-sc-bv7v5f" }) ` display: flex; height: 100%; width: 100%; transition: transform 0.5s ease; transform: ${(props) => props.transform}; `; const CarouselSlide = styled_components_1.default.div.withConfig({ displayName: "brc-sc-CarouselSlide", componentId: "brc-sc-16fo9ty" }) ` flex-shrink: 0; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; visibility: ${(props) => (props.isActive ? 'visible' : 'hidden')}; img { max-width: 100%; max-height: 100%; } `; const Arrow = styled_components_1.default.button.withConfig({ displayName: "brc-sc-Arrow", componentId: "brc-sc-dnltkr" }) ` position: absolute; top: 55%; transform: translateY(-50%); background-color: ${({ theme }) => theme.navNeutral100}; border: none; border-radius: 50%; width: 42px; height: 42px; cursor: pointer; z-index: 2; `; const ArrowIcon = (0, styled_components_1.default)(icon_js_1.Icon).withConfig({ displayName: "brc-sc-ArrowIcon", componentId: "brc-sc-f7vqrx" }) ` fill: ${({ theme }) => theme.navNeutralDark}; `; const LeftArrow = (0, styled_components_1.default)(Arrow).withConfig({ displayName: "brc-sc-LeftArrow", componentId: "brc-sc-19qnory" }) ` left: 0; & > div { transform: translate(-2px, 2px); /* fixes an issue with extra padding on the svg causing icon to be uncentered */ } `; const RightArrow = (0, styled_components_1.default)(Arrow).withConfig({ displayName: "brc-sc-RightArrow", componentId: "brc-sc-yxxu84" }) ` right: 0; & > div { transform: translate(2px, 2px); /* fixes an issue with extra padding on the svg causing icon to be uncentered */ } `; const Dot = styled_components_1.default.button.withConfig({ displayName: "brc-sc-Dot", componentId: "brc-sc-rgkkqx" }) ` background-color: ${({ theme, active }) => (active ? theme.navPrimary400 : theme.navNeutral200)}; border: none; border-radius: 50%; width: 8px; height: 8px; cursor: pointer; flex-shrink: 0; display: flex; padding: 0; margin: 0; `; const DotWrapper = styled_components_1.default.div.withConfig({ displayName: "brc-sc-DotWrapper", componentId: "brc-sc-1gu1n1d" }) ` position: absolute; bottom: 0; left: 50%; transform: translateX(-50%); display: flex; gap: 24px; z-index: 2; `; /** Hook to provide carousel functionality to any component */ function useCarousel({ infinite, slides }) { const [activeIndex, setActiveIndex] = (0, react_1.useState)(0); const slideCount = slides.length; const nextSlide = (0, react_1.useCallback)(() => { setActiveIndex((prev) => (infinite || prev < slideCount - 1 ? (prev + 1) % slideCount : prev)); }, [infinite, slideCount]); const prevSlide = (0, react_1.useCallback)(() => { setActiveIndex((prev) => (infinite || prev > 0 ? (prev - 1 + slideCount) % slideCount : prev)); }, [infinite, slideCount]); const goToSlide = (0, react_1.useCallback)((index) => { setActiveIndex(index); }, []); const changeNavItem = (0, react_1.useCallback)((e) => { if (e.key === 'ArrowLeft') { prevSlide(); } else if (e.key === 'ArrowRight') { nextSlide(); } }, [nextSlide, prevSlide]); return { goToSlide, changeNavItem, prevSlide, nextSlide, activeIndex, slideCount, }; } exports.useCarousel = useCarousel; /** Base Carousel component that can be composed into more specific carousels via prop configurations */ const CarouselBase = ({ children, infinite, minSlideHeight = 'none', maxSlideHeight = 'none', onSlideChange, }) => { const slides = react_1.default.Children.toArray(children); const { activeIndex, nextSlide, prevSlide, changeNavItem, goToSlide, slideCount } = useCarousel({ infinite, slides }); (0, react_1.useEffect)(() => { if (onSlideChange) { onSlideChange({ activeIndex }); } }, [onSlideChange, activeIndex]); return ((0, jsx_runtime_1.jsxs)(CarouselContainer, { tabIndex: 0, "aria-label": "Carousel", "data-testid": "carousel", children: [(0, jsx_runtime_1.jsx)(CarouselWrapper, { style: { minHeight: minSlideHeight, maxHeight: maxSlideHeight }, children: (0, jsx_runtime_1.jsx)(CarouselSlides, { transform: `translateX(-${activeIndex * 100}%)`, children: slides.map((child, i) => ((0, jsx_runtime_1.jsx)(CarouselSlide, { isActive: i === activeIndex, "data-testid": `carousel-slide-${i}`, children: child }, i))) }) }), (0, jsx_runtime_1.jsxs)("div", { "data-testid": "carouselNav", onKeyDown: changeNavItem, role: "presentation", "aria-label": "Carousel navigation", children: [activeIndex === 0 && !infinite ? null : ((0, jsx_runtime_1.jsx)(LeftArrow, { "data-testid": "carousel:leftArrow", onClick: prevSlide, "aria-label": "Previous button", children: (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(ArrowIcon, { name: "actions/carrot-left" }) }) })), activeIndex === slides.length - 1 && !infinite ? null : ((0, jsx_runtime_1.jsx)(RightArrow, { "data-testid": "carousel:rightArrow", onClick: nextSlide, "aria-label": "Next button", children: (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)(ArrowIcon, { name: "actions/carrot-right" }) }) }))] }), (0, jsx_runtime_1.jsx)(DotWrapper, { children: Array.from({ length: slideCount }).map((_, i) => ((0, jsx_runtime_1.jsx)(Dot, { "data-testid": `carousel-dot-${i}`, active: i === activeIndex, onClick: () => goToSlide(i) }, i))) })] })); }; exports.CarouselBase = CarouselBase; exports.CarouselBase.displayName = 'Carousel'; const StyledCarouselExp = (0, styled_components_1.default)(exports.CarouselBase).withConfig({ displayName: "brc-sc-StyledCarouselExp", componentId: "brc-sc-psg7mg" }) ``; exports.Carousel = StyledCarouselExp; //# sourceMappingURL=carousel.js.map