|
| 1 | +/** |
| 2 | + * Virtualizes a list to render only visible items for performance. |
| 3 | + * |
| 4 | + * @template {Record<string, unknown>} T The type of items in the array (must be a Record) |
| 5 | + * @param {Object} config |
| 6 | + * @param {T[]} config.items - Full array of items to virtualize |
| 7 | + * @param {number} config.itemHeight - Height of each item in pixels |
| 8 | + * @param {number} config.containerHeight - Visible container height in pixels |
| 9 | + * @param {number} config.scrollTop - Current scroll position |
| 10 | + * @param {number} [config.overscan=3] - Extra items to render above/below viewport |
| 11 | + * @param {number} [config.maxItems] - Cap maximum rendered items |
| 12 | + * @param {number} [config.threshold=100] - Minimum items before virtualization activates |
| 13 | + * |
| 14 | + * @returns {{ |
| 15 | + * visibleItems: T[], |
| 16 | + * startIndex: number, |
| 17 | + * endIndex: number, |
| 18 | + * offsetY: number, |
| 19 | + * totalHeight: number, |
| 20 | + * isVirtualized: boolean |
| 21 | + * }} |
| 22 | + */ |
| 23 | +export function virtualize({ |
| 24 | + items, |
| 25 | + itemHeight, |
| 26 | + containerHeight, |
| 27 | + scrollTop, |
| 28 | + overscan = 3, |
| 29 | + maxItems = undefined, |
| 30 | + threshold = 100, |
| 31 | +}) { |
| 32 | + // Auto-disable if below threshold |
| 33 | + if (items.length < threshold) { |
| 34 | + return { |
| 35 | + visibleItems: items, |
| 36 | + startIndex: 0, |
| 37 | + endIndex: items.length, |
| 38 | + offsetY: 0, |
| 39 | + totalHeight: items.length * itemHeight, |
| 40 | + isVirtualized: false, |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + const totalHeight = items.length * itemHeight; |
| 45 | + |
| 46 | + // Calculate visible range |
| 47 | + const startIndex = Math.max(0, Math.floor(scrollTop / itemHeight) - overscan); |
| 48 | + let endIndex = Math.min( |
| 49 | + items.length, |
| 50 | + Math.ceil((scrollTop + containerHeight) / itemHeight) + overscan, |
| 51 | + ); |
| 52 | + |
| 53 | + // Apply maxItems cap if specified |
| 54 | + if (maxItems && endIndex - startIndex > maxItems) { |
| 55 | + endIndex = startIndex + maxItems; |
| 56 | + } |
| 57 | + |
| 58 | + const offsetY = startIndex * itemHeight; |
| 59 | + |
| 60 | + return { |
| 61 | + visibleItems: items.slice(startIndex, endIndex), |
| 62 | + startIndex, |
| 63 | + endIndex, |
| 64 | + offsetY, |
| 65 | + totalHeight, |
| 66 | + isVirtualized: true, |
| 67 | + }; |
| 68 | +} |
0 commit comments