3.13.4 • Published 3 months ago

@tanstack/lit-virtual v3.13.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

@tanstack/lit-virtual

Efficiently virtualize only the visible DOM nodes within massive scrollable elements using Lit, while maintaining complete control over markup and styles.

VirtualizerController

@tanstack/lit-virtual utilizes Reactive Controllers to create the virtualizer and integrate it with the element lifecycle:

import { LitElement } from 'lit'
import { VirtualizerController } from '@tanstack/lit-virtual'
import { Ref, createRef } from 'lit/directives/ref.js'

class MyVirtualElement extends LitElement {
  private virtualizerController: VirtualizerController<HTMLDivElement, Element>
  private scrollElementRef: Ref<HTMLDivElement> = createRef()

  constructor() {
    super()
    this.virtualizerController = new VirtualizerController(this, {
      getScrollElement: () => this.scrollElementRef.value,
      count: 10000,
      estimateSize: () => 35,
      overscan: 5,
    })
  }

  render() {
    const virtualizer = this.virtualizerController.getVirtualizer()
    const virtualItems = virtualizer.getVirtualItems()

    return html`
      <div class="list scroll-container" ${ref(this.scrollElementRef)}>
        ${virtualItems.map(
          (item) => html`<div class="item">${item.index}</div>`,
        )}
      </div>
    `
  }
}

Note that a Ref is attached to the scrolling container to allow the virtualizer to interact with it.

WindowVirtualizerController

You can also create a virtualizer controller that attaches to the Window:

import { WindowVirtualizerController } from '@tanstack/lit-virtual'

class MyWindowVirtualElement extends LitElement {
  private windowVirtualizerController: WindowVirtualizerController

  constructor() {
    super()
    this.windowVirtualizerController = new WindowVirtualizerController(this, {
      count: this.data.length,
      estimateSize: () => 350,
      overscan: 5,
    })
  }

  // Implement render and other lifecycle methods as needed
}

For more examples and detailed usage, visit the official documentation.

3.13.4

3 months ago

3.13.3

3 months ago

3.13.2

4 months ago

3.13.1

4 months ago

3.13.0

4 months ago

3.12.1

4 months ago

3.12.0

4 months ago

3.11.3

5 months ago

3.11.2

6 months ago

3.10.9

7 months ago

3.10.8

9 months ago

3.10.7

9 months ago

3.10.6

10 months ago

3.10.5

10 months ago

3.10.4

10 months ago

3.10.2

10 months ago

3.10.1

10 months ago

3.10.0

10 months ago

3.9.0

10 months ago

3.8.6

10 months ago

3.8.4

11 months ago

3.8.3

11 months ago

3.8.2

11 months ago

3.8.1

12 months ago