WlVirtualList

Render a large list with fixed-height items.
Components / Data v1.4.0

Size and density

Render 10,000 records with only the items near the viewport in the DOM.

Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record 7
Record 8
Record 9
Record 10
Record 11
Full example
<script setup lang="ts">
import { WlButton, WlVirtualList } from "legato-ui";
import { computed, ref } from "vue";

const props = { preview: {} as Record<string, unknown> };
const records = Array.from({ length: 10000 }, (_, index) => ({ id: index + 1 }));
const items = computed(() => Array.isArray(props.preview?.items) ? props.preview.items as typeof records : records);
const list = ref<{ scrollToIndex(index: number, options?: { align: "start" | "end" }): void }>();
</script>

<template>
  <div class="wl-stack" data-space="sm">
    <div class="wl-inline" data-space="sm">
      <WlButton size="sm" :disabled="!items.length" @click="list?.scrollToIndex(0, { align: 'start' })">
        {{ "First record" }}
      </WlButton>
      <WlButton size="sm" :disabled="!items.length" @click="list?.scrollToIndex(items.length - 1, { align: 'end' })">
        {{ "Last record" }}
      </WlButton>
    </div>
    <WlVirtualList
      ref="list"
      :items="items"
      item-key="id"
      :item-height="36"
      :height="252"
      aria-label="Render 10,000 records with only the items near the viewport in the DOM."
      
    >
      <template #default="{ item }">
        <div class="virtual-record">{{ "Record" }} {{ item.id }}</div>
      </template>
    </WlVirtualList>
  </div>
</template>

<style scoped>
.virtual-record {
  display: flex;
  align-items: center;
  height: 100%;
  padding-inline: var(--wl-space-sm);
  border-bottom: 1px solid var(--wl-border);
}
</style>

Behavior

Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record 7
Record 8
Record 9
Record 10
Record 11
Full example
<script setup lang="ts">
import { WlButton, WlVirtualList } from "legato-ui";
import { computed, ref } from "vue";

const props = { preview: {} as Record<string, unknown> };
const records = Array.from({ length: 10000 }, (_, index) => ({ id: index + 1 }));
const items = computed(() => Array.isArray(props.preview?.items) ? props.preview.items as typeof records : records);
const list = ref<{ scrollToIndex(index: number, options?: { align: "start" | "end" }): void }>();
</script>

<template>
  <div class="wl-stack" data-space="sm">
    <div class="wl-inline" data-space="sm">
      <WlButton size="sm" :disabled="!items.length" @click="list?.scrollToIndex(0, { align: 'start' })">
        {{ "First record" }}
      </WlButton>
      <WlButton size="sm" :disabled="!items.length" @click="list?.scrollToIndex(items.length - 1, { align: 'end' })">
        {{ "Last record" }}
      </WlButton>
    </div>
    <WlVirtualList
      ref="list"
      :items="items"
      item-key="id"
      :item-height="36"
      :height="252"
      aria-label="Render 10,000 records with only the items near the viewport in the DOM."
      
    >
      <template #default="{ item }">
        <div class="virtual-record">{{ "Record" }} {{ item.id }}</div>
      </template>
    </WlVirtualList>
  </div>
</template>

<style scoped>
.virtual-record {
  display: flex;
  align-items: center;
  height: 100%;
  padding-inline: var(--wl-space-sm);
  border-bottom: 1px solid var(--wl-border);
}
</style>

Example data

Record 1
Record 2
Record 3
Record 4
Record 5
Record 6
Record 7
Record 8
Record 9
Record 10
Record 11
Full example
<script setup lang="ts">
import { WlButton, WlVirtualList } from "legato-ui";
import { computed, ref } from "vue";

const props = { preview: {} as Record<string, unknown> };
const records = Array.from({ length: 10000 }, (_, index) => ({ id: index + 1 }));
const items = computed(() => Array.isArray(props.preview?.items) ? props.preview.items as typeof records : records);
const list = ref<{ scrollToIndex(index: number, options?: { align: "start" | "end" }): void }>();
</script>

<template>
  <div class="wl-stack" data-space="sm">
    <div class="wl-inline" data-space="sm">
      <WlButton size="sm" :disabled="!items.length" @click="list?.scrollToIndex(0, { align: 'start' })">
        {{ "First record" }}
      </WlButton>
      <WlButton size="sm" :disabled="!items.length" @click="list?.scrollToIndex(items.length - 1, { align: 'end' })">
        {{ "Last record" }}
      </WlButton>
    </div>
    <WlVirtualList
      ref="list"
      :items="items"
      item-key="id"
      :item-height="36"
      :height="252"
      aria-label="Render 10,000 records with only the items near the viewport in the DOM."
      
    >
      <template #default="{ item }">
        <div class="virtual-record">{{ "Record" }} {{ item.id }}</div>
      </template>
    </WlVirtualList>
  </div>
</template>

<style scoped>
.virtual-record {
  display: flex;
  align-items: center;
  height: 100%;
  padding-inline: var(--wl-space-sm);
  border-bottom: 1px solid var(--wl-border);
}
</style>