feat: improve list virtualization

This commit is contained in:
reya
2024-06-20 13:22:28 +07:00
parent f8280ec8ee
commit 9779d020c7
15 changed files with 582 additions and 313 deletions

View File

@@ -11,29 +11,25 @@ interface CarouselProps<T> {
interface CarouselRenderItemProps<T> {
readonly item: T;
readonly index: number;
readonly isSnapPoint: boolean;
}
export const Carousel = <T,>({ items, renderItem }: CarouselProps<T>) => {
const {
scrollRef,
pages,
activePageIndex,
prev,
next,
goTo,
snapPointIndexes,
} = useSnapCarousel();
const { scrollRef, pages, activePageIndex, prev, next, snapPointIndexes } =
useSnapCarousel();
return (
<div className="relative group">
<ul
ref={scrollRef}
className="relative flex overflow-auto snap-x scrollbar-none"
>
{items.map((item, i) =>
{items.map((item, index) =>
renderItem({
item,
isSnapPoint: snapPointIndexes.has(i),
index,
isSnapPoint: snapPointIndexes.has(index),
}),
)}
</ul>
@@ -74,13 +70,15 @@ interface CarouselItemProps {
readonly children?: React.ReactNode;
}
export const CarouselItem = ({ isSnapPoint, children }: CarouselItemProps) => (
<li
className={cn(
"w-[240px] h-[320px] shrink-0 pl-3 last:pr-2",
isSnapPoint ? "" : "snap-start",
)}
>
{children}
</li>
);
export const CarouselItem = ({ isSnapPoint, children }: CarouselItemProps) => {
return (
<li
className={cn(
"w-[240px] h-[320px] shrink-0 pl-3 last:pr-2",
isSnapPoint ? "" : "snap-start",
)}
>
{children}
</li>
);
};