import React, { HTMLAttributes, useRef, FC } from 'react';
import { classNames } from '../../lib/classNames';
import { getClassName } from '../../helpers/getClassName';
import { usePlatform } from '../../hooks/usePlatform';
import HorizontalScroll from '../HorizontalScroll/HorizontalScroll';
import { withAdaptivity, AdaptivityProps } from '../../hoc/withAdaptivity';
import { useDOM } from '../../lib/dom';
export interface CardScrollProps extends HTMLAttributes, AdaptivityProps {
size?: 's' | 'm' | 'l';
}
const CardScroll: FC = ({ children, className, size, sizeX, ...restProps }: CardScrollProps) => {
const platform = usePlatform();
const refContainer = useRef(null);
const gapRef = useRef(null);
const { window } = useDOM();
function getScrollToLeft(offset: number): number {
const containerWidth = refContainer.current.offsetWidth;
const slideIndex = Array
.from(refContainer.current.children)
.findIndex((el: HTMLElement) => el.offsetLeft + el.offsetWidth + parseInt(window.getComputedStyle(el).marginRight) - offset >= 0);
if (slideIndex === -1) {
return offset;
}
if (slideIndex === 0) {
return 0;
}
const slide = refContainer.current.children[slideIndex] as HTMLElement;
const scrollTo = slide.offsetLeft - (containerWidth - slide.offsetWidth) + gapRef.current.offsetWidth;
if (scrollTo <= 2 * gapRef.current.offsetWidth) {
return 0;
}
return scrollTo;
}
function getScrollToRight(offset: number): number {
const containerWidth = refContainer.current.offsetWidth;
const slide = Array.from(refContainer.current.children).find((el: HTMLElement) => el.offsetLeft + el.offsetWidth - offset > containerWidth) as HTMLElement;
if (!slide) {
return offset;
}
return slide.offsetLeft - gapRef.current.offsetWidth;
}
return (
);
};
CardScroll.defaultProps = {
size: 's',
};
export default withAdaptivity(CardScroll, { sizeX: true });