Skip to content

Commit

Permalink
Merge pull request #291 from sima-land/290-product-carousel-margin
Browse files Browse the repository at this point in the history
#290 ProductCarousel: добавить возможность регулировать отступы зоны загрузки
  • Loading branch information
krutoo authored Sep 2, 2024
2 parents 4432263 + 58d0181 commit b82e60e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/desktop/components/product-carousel/product-carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const getSizeClasses = (size: ProductCarouselProps['itemSize']) =>
]
: [];

const defaultNeedRequestOptions: IntersectionObserverInit = {
rootMargin: '200px 0px 200px 0px',
};

/**
* Карусель рекомендованных товаров.
* @param props Свойства.
Expand All @@ -37,6 +41,7 @@ export function ProductCarousel({
children,
controlProps,
itemImageRatio = 1,
needRequestOptions = defaultNeedRequestOptions,
}: ProductCarouselProps) {
const layer = useLayer();
const needBigArrows = useMedia('(min-width: 1600px)');
Expand All @@ -54,7 +59,15 @@ export function ProductCarousel({
const itemWidth = useClientWidth(firstItemRef, [items.length]);

// инициируем загрузку данных, когда компонент почти попал в зону видимости
const options = useMemo(() => ({ rootMargin: '200px 0px 200px 0px' }), []);
const { root, rootMargin, threshold } = needRequestOptions;
const options = useMemo<IntersectionObserverInit>(
() => ({
root,
rootMargin,
threshold,
}),
[root, rootMargin, threshold],
);
useIntersection(
rootRef,
entry => {
Expand Down
3 changes: 3 additions & 0 deletions src/desktop/components/product-carousel/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface ProductCarouselProps {
/** Сработает при попадании карусели в область достаточно близкую к viewport'у. */
onNeedRequest?: () => void;

/** Опции области, которая вызовет onNeedRequest. */
needRequestOptions?: IntersectionObserverInit;

/** Предоставит свойства для элемента карусели. */
itemProps?: { style?: CSSProperties; className?: string };

Expand Down

0 comments on commit b82e60e

Please sign in to comment.