3.13.10 • Published 2 months ago

@tanstack/lit-virtual v3.13.10

Weekly downloads
-
License
MIT
Repository
github
Last release
2 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.9

3 months ago

3.13.6

5 months ago

3.13.5

5 months ago

3.13.8

4 months ago

3.13.7

4 months ago

3.13.10

2 months ago

3.13.4

5 months ago

3.13.3

5 months ago

3.13.2

6 months ago

3.13.1

6 months ago

3.13.0

7 months ago

3.12.1

7 months ago

3.12.0

7 months ago

3.11.3

7 months ago

3.11.2

8 months ago

3.10.9

10 months ago

3.10.8

11 months ago

3.10.7

12 months ago

3.10.6

12 months ago

3.10.5

12 months ago

3.10.4

12 months ago

3.10.2

1 year ago

3.10.1

1 year ago

3.10.0

1 year ago

3.9.0

1 year ago

3.8.6

1 year ago

3.8.4

1 year ago

3.8.3

1 year ago

3.8.2

1 year ago

3.8.1

1 year ago