import { For, createSignal } from "solid-js"; import type { Accessor } from "solid-js"; import type { JSX } from "@solidjs/web"; import { access } from "@solid-primitives/utils"; import type { MaybeAccessor } from "@solid-primitives/utils"; type VirtualListConfig = { items: MaybeAccessor; rootHeight: MaybeAccessor; rowHeight: MaybeAccessor; overscanCount?: MaybeAccessor; }; type VirtualListReturn = [ Accessor<{ containerHeight: number; viewerTop: number; visibleItems: T; firstIndex: number; lastIndex: number | undefined; }>, onScroll: (e: Event) => void, ]; /** * A headless virtualized list (see https://www.patterns.dev/vanilla/virtual-lists/) utility for constructing your own virtualized list components with maximum flexibility. * * @param items the list of items * @param rootHeight the height of the root element of the virtualizedList * @param rowHeight the height of individual rows in the virtualizedList * @param overscanCount the number of elements to render both before and after the visible section of the list, so passing 5 will render 5 items before the list, and 5 items after. Defaults to 1, cannot be set to zero. This is necessary to hide the blank space around list items when scrolling * @returns {VirtualListReturn} to use in the list's jsx */ export function createVirtualList({ items, rootHeight, rowHeight, overscanCount, }: VirtualListConfig): VirtualListReturn { const [offset, setOffset] = createSignal(0, { ownedWrite: true }); const getItems = () => (access(items) || []) as unknown as T; const getRootHeight = () => access(rootHeight); const getRowHeight = () => access(rowHeight); const getOverscanCount = () => access(overscanCount) || 1; return [ () => { const resolvedItems = getItems(); const rowH = getRowHeight(); const currentOffset = offset(); const overscan = getOverscanCount(); const firstIdx = Math.max(0, Math.floor(currentOffset / rowH) - overscan); const lastIdx = Math.min( resolvedItems.length, Math.floor(currentOffset / rowH) + Math.ceil(getRootHeight() / rowH) + overscan, ); return { containerHeight: resolvedItems.length * rowH, viewerTop: firstIdx * rowH, visibleItems: resolvedItems.slice(firstIdx, lastIdx) as unknown as T, firstIndex: firstIdx, // -1 because slice is an exclusive range lastIndex: lastIdx > 0 ? lastIdx - 1 : undefined, }; }, e => { const target = e.target as HTMLElement | null; if (target?.scrollTop !== undefined) setOffset(target.scrollTop); }, ]; } type VirtualListProps = { children: (item: Accessor, index: number) => U; each: T | undefined | null | false; fallback?: JSX.Element; class?: string; overscanCount?: number; rootHeight: number; rowHeight: number; }; /** * A basic, unstyled virtualized list (see https://www.patterns.dev/vanilla/virtual-lists/) component you can drop into projects without modification * * @param children the flowComponent that will be used to transform the items into rows in the list * @param each the list of items * @param fallback the optional fallback to display if the list of items to display is empty * @param class an optional class applied to the element wrapping the rendered rows, useful for styling row layout (e.g. flex/grid) * @param overscanCount the number of elements to render both before and after the visible section of the list, so passing 5 will render 5 items before the list, and 5 items after. Defaults to 1, cannot be set to zero. This is necessary to hide the blank space around list items when scrolling * @param rootHeight the height of the root element of the virtualizedList itself * @param rowHeight the height of individual rows in the virtualizedList * @returns virtualized list component */ export function VirtualList( props: VirtualListProps, ): JSX.Element { const [virtual, onScroll] = createVirtualList({ items: () => props.each, rootHeight: () => props.rootHeight, rowHeight: () => props.rowHeight, overscanCount: () => props.overscanCount || 1, }); return (
{props.children}
); }