import React, { useEffect, useState } from 'react'; import { useDimensions } from 'soapbox/hooks'; import HStack from '../hstack/hstack'; import Icon from '../icon/icon'; interface ICarousel { children: any /** Optional height to force on controls */ controlsHeight?: number /** How many items in the carousel */ itemCount: number /** The minimum width per item */ itemWidth: number } /** * Carousel */ const Carousel: React.FC = (props): JSX.Element => { const { children, controlsHeight, itemCount, itemWidth } = props; // eslint-disable-next-line @typescript-eslint/no-unused-vars const [_ref, setContainerRef, { width: containerWidth }] = useDimensions(); const [pageSize, setPageSize] = useState(0); const [currentPage, setCurrentPage] = useState(1); const numberOfPages = Math.ceil(itemCount / pageSize); const width = containerWidth / (Math.floor(containerWidth / itemWidth)); const hasNextPage = currentPage < numberOfPages && numberOfPages > 1; const hasPrevPage = currentPage > 1 && numberOfPages > 1; const handleNextPage = () => setCurrentPage((prevPage) => prevPage + 1); const handlePrevPage = () => setCurrentPage((prevPage) => prevPage - 1); const renderChildren = () => { if (typeof children === 'function') { return children({ width: width || 'auto' }); } return children; }; useEffect(() => { if (containerWidth) { setPageSize(Math.round(containerWidth / width)); } }, [containerWidth, width]); return (
{renderChildren()}
); }; export default Carousel;