* @param {HTMLElement} container * @param {VirtualListOptions} options
(container, options = {})
| 18 | * @param {VirtualListOptions} options |
| 19 | */ |
| 20 | constructor(container, options = {}) { |
| 21 | this.container = container; |
| 22 | this.itemHeight = options.itemHeight || 30; |
| 23 | this.buffer = options.buffer || 10; |
| 24 | this.renderItem = |
| 25 | options.renderItem || |
| 26 | ((item) => { |
| 27 | return tag("div", { textContent: String(item) }); |
| 28 | }); |
| 29 | |
| 30 | this.items = []; |
| 31 | this.renderedRange = { start: -1, end: -1 }; |
| 32 | this.pool = []; // DOM recycling pool |
| 33 | |
| 34 | // Create structure using html-tag-js |
| 35 | this.topSpacer = tag("div", { |
| 36 | className: "virtual-spacer virtual-spacer-top", |
| 37 | }); |
| 38 | this.bottomSpacer = tag("div", { |
| 39 | className: "virtual-spacer virtual-spacer-bottom", |
| 40 | }); |
| 41 | this.itemContainer = tag("div", { className: "virtual-items" }); |
| 42 | |
| 43 | this.container.append( |
| 44 | this.topSpacer, |
| 45 | this.itemContainer, |
| 46 | this.bottomSpacer, |
| 47 | ); |
| 48 | |
| 49 | // Bind scroll handler with RAF throttling |
| 50 | this.rafId = null; |
| 51 | this.onScrollBound = this.onScroll.bind(this); |
| 52 | this.container.addEventListener("scroll", this.onScrollBound, { |
| 53 | passive: true, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Set the items to render |