UI virtualization

The UI Virtualization plugin provides efficient rendering of large collections by only creating DOM elements for visible items. This dramatically improves performance when working with thousands of items by maintaining a small, consistent number of DOM elements regardless of collection size.

Performance at Scale: Virtual repeat maintains constant performance whether you have 100 items or 100,000 items, making it essential for data-heavy applications.

How It Works

Instead of creating DOM elements for every item in your collection, virtual repeat:

  1. Calculates visible area: Determines how many items can fit in the scrollable viewport

  2. Creates minimal views: Only renders 2× the visible items (for smooth scrolling)

  3. Manages buffers: Uses invisible spacer elements to maintain proper scroll height

  4. Recycles views: Reuses existing DOM elements as you scroll, updating their data context

  5. Handles scroll events: Efficiently responds to scrolling without expensive DOM operations

Installation

Install the plugin via npm:

npm install @aurelia/ui-virtualization

Register the plugin in your application:

import { Aurelia } from 'aurelia';
import { DefaultVirtualizationConfiguration } from '@aurelia/ui-virtualization';

Aurelia
  .register(DefaultVirtualizationConfiguration)
  .app(/* your root component */)
  .start();

Basic Usage

Simple List

Use virtual-repeat.for just like the standard repeat, with one important requirement: your container must have a fixed height and overflow: scroll or overflow: auto.

Configuration options

virtual-repeat supports several optional, kebab-cased configuration properties that can be appended after the for statement, separated by semicolons (;):

Option
Description
Example

layout

Sets the scrolling direction. Can be 'vertical' (default) or 'horizontal'.

layout: horizontal

item-height

Explicit pixel height for each repeated item. Overrides the automatic first‐item measurement.

item-height: 40

item-width

Explicit pixel width for each repeated item. Overrides the automatic first‐item measurement.

item-width: 120

variable-height

Enables variable‐height support for vertical layouts. Each item's height is measured individually.

variable-height: true

variable-width

Enables variable‐width support for horizontal layouts. Each item's width is measured individually.

variable-width: true

buffer-size

Multiplier that determines how many extra view sets are kept rendered above/below (vertical) or left/right (horizontal). Default is 2.

buffer-size: 3

min-views

Overrides the auto‐calculated minimum number of views needed to fill the viewport.

min-views: 10

Examples:

Names can be in camelCase (itemHeight, itemWidth, etc.) or kebab-case (item-height, item-width, etc.).

Horizontal Scrolling

The virtual repeat supports horizontal scrolling layouts, allowing you to create efficiently virtualized horizontal lists, carousels, and galleries.

Basic Horizontal Layout

Styling Considerations for Horizontal Layouts

  1. Set overflow: auto and white-space: nowrap on the scrolling container

  2. Use display: inline-block (or similar) on items for horizontal arrangement

  3. Specify both width and height for predictable layout

Horizontal with Configuration Options

Horizontal Infinite Scroll

Use Cases for Horizontal Scrolling

  • Image galleries

  • Product carousels

  • Timeline views

  • Tag lists

  • Dashboard widgets

Variable Sizing

The virtual repeat supports variable item sizes, which is useful when items have different heights (vertical) or widths (horizontal).

Variable Height (Vertical Layout)

Variable Width (Horizontal Layout)

Combining Variable Sizing with Other Options

Data-Driven Variable Sizing

Performance Considerations

  1. Measurement overhead: DOM measurement adds cost

  2. Use sparingly: enable variable sizing only when needed

  3. Pre-calculate sizes when possible

  4. Caching: virtual repeat caches measured sizes

Infinite Scroll

The virtual repeat dispatches events to let you load more data when the user scrolls near the top or bottom.

Event Types

Usage Example

Best Practices for Infinite Scroll

  • Prevent multiple requests with a loading flag

  • Implement error handling

  • Show loading indicators

  • Consider debouncing rapid scroll events

Basic Examples

Vertical div (default)

Horizontal div

Vertical list

Horizontal list

Table Virtualization

For tables, virtual repeat works on table rows while preserving the table structure:

Context Properties

Virtual repeat provides all standard repeat context properties:

Available context properties: $index, $length, $first, $last, $middle, $even, $odd

Dynamic Collections

Virtual repeat efficiently handles collection mutations:

Container Requirements

Scrollable Container

Virtual repeat requires a scrollable ancestor with a defined height and overflow: auto or overflow: scroll:

Item Height Requirements

All items must have equal height (measured from the first item):

Advanced Styling

Performance Considerations

Best Practices

  • Keep item templates simple

  • Use CSS classes instead of inline styles

  • Minimize watchers in item templates

  • Consider pagination for extremely large datasets

Memory Usage

Virtual repeat maintains only a small number of views (typically 2× visible count):

Common Patterns

Loading States

  1. <template/> is not supported as the root element of a virtual repeat template. Items need calculatable dimensions.

  2. Other template controllers (if, with, etc.) cannot sit on the same element as virtual-repeat. Nest them inside the repeated element.

  3. Avoid CSS pseudo-selectors like :nth-child; virtual repeat recycles DOM. Use context properties and classes.

  4. Views are reused; lifecycle hooks (attached, etc.) may not fire each scroll. Use reactive bindings or change handlers.

  5. Horizontal layout tips: white-space: nowrap, display: inline-block, explicit widths, vertical-align: top.

  6. Variable sizing performance: Measure only when needed, pre-calculate sizes, test with real data.

Empty States

Filtering and Searching

Template Controllers Limitation

Virtual repeat cannot combine with other template controllers on the same element:

Root Template Element Limitation

You cannot use <template> as the root for a virtual-repeat:

CSS Pseudo-selectors

Avoid selectors that rely on DOM order:

Component Lifecycle

  • created and attached fire on initial view creation

  • Views are reused on scroll; binding happens more frequently

  • Use reactive change handlers instead of relying on lifecycle timing

Integration with Other Features

With Binding Behaviors

With Value Converters

Troubleshooting

Common Issues

  • Items not rendering: check container height and overflow

  • Scroll jumps: inconsistent item heights

  • Performance issues: simplify templates, reduce bindings

  • Collection updates: ensure the array or reference updates

Debugging

Last updated

Was this helpful?