For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/elements/list.md.

<list>

A virtualized scrolling list for long or growing data: only items near the screen are drawn. With <list recycle> or *lynxListFor, Angular also builds an item only when the list asks for it, and reuses the items scrolled away.

The example switches between <list recycle>, *lynxListFor and a plain @for over 1,000 rows, and counts the rows each builds: about 14 for the first two against 1,000 on an iPhone. In this web preview all three build every row, because Lynx for Web's list asks for them all.

Usage

src/app/contacts.ts
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-contacts',
  imports: [LYNX_ELEMENTS],
  template: `
    <list list-type="single" style="height: 100vh;">
      @for (contact of contacts(); track contact.id) {
        <list-item [item-key]="contact.id">
          <text>{{ contact.name }}</text>
        </list-item>
      }
    </list>
  `,
})
export class Contacts {
  readonly contacts = signal([{ id: '1', name: 'Ada Lovelace' }]);
}

Import LYNX_ELEMENTS in your component (see Elements).

Warning

Give every <list-item> a unique item-key, the same value as your @for track expression.

  • A number is fine. It reaches Lynx as a string.
  • Native keeps an item's cell by its key. Without one, a list item is keyed by its element. So an item Angular makes again, for a new object with the same data, is a new item to the list.
  • Two items with the same key confuse it.

@for builds every item's view up front, and keeps them all. That's fine for a few dozen items. For hundreds or thousands, add recycle to the list.

<list recycle>

With recycle on the <list>, its @for builds an item's view only when the list asks for it, as the item scrolls into view, and gives the views of items scrolled away to the items scrolled to. You go on writing @for.

src/app/contacts.ts
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

type Contact = { id: string; name: string; tall: boolean };

@Component({
  selector: 'app-contacts',
  imports: [LYNX_ELEMENTS],
  template: `
    <list recycle list-type="single" style="height: 100vh;">
      @for (contact of contacts(); track contact.id; let i = $index) {
        <list-item [estimated-main-axis-size-px]="contact.tall ? 96 : 64">
          <text>{{ i + 1 }}. {{ contact.name }}</text>
        </list-item>
      } @empty {
        <list-item><text>No contacts yet</text></list-item>
      }
    </list>
  `,
})
export class Contacts {
  readonly contacts = signal<Contact[]>([
    { id: '1', name: 'Ada Lovelace', tall: false },
  ]);
}
  • The build turns the @for into a *lynxListFor before Angular compiles it, so it behaves as that does. track gives each item its key, in place of item-key. The <list-item>'s layout attributes become its itemAttributes.
  • $index, $count, $first, $last, $even and $odd work, with their aliases (let i = $index), and so does @empty.
  • Bind a layout attribute to the item and $index only, such as [estimated-main-axis-size-px]="contact.tall ? 96 : 64". The list reads an item's layout once, for the item, so it wouldn't see a change to anything else.
  • A recycled view keeps its state, as with *lynxListFor. See what a reused view keeps.
  • The @for goes directly in the <list>, or in an @if or @switch there, and its body is one <list-item>.
  • The build warns of an @for it can't turn into *lynxListFor, and says why. That @for builds every item's view, as in a plain list. It happens when:
    • its body isn't one <list-item>;
    • the <list-item> has a structural directive;
    • a layout attribute reads more than the item and $index, or is interpolated;
    • it's inside an i18n element;
    • its component isn't standalone, so the build can't add the *lynxListFor directive to its imports. Make the component standalone;
    • its template is inline and has escape sequences, such as \n or \`, whose places the build can't map back to your file. Write the characters themselves, or, for a backtick, move the template to a templateUrl file;
    • a component in its file names its templateUrl or styleUrls with a constant rather than a string, which the build can't point at the file from the copy it compiles.
  • The build sees a <list recycle>'s @for only in the same template, written in the component or in a templateUrl given as a string. An @for in a child component's template, or in a template named by a constant, builds every item's view, with no warning. Use *lynxListFor there.
  • Write recycle as an attribute. The build reads the template before Angular compiles it, so it sees only what's written: a bound [recycle] doesn't recycle, and the build warns. It reads the value as Angular's booleanAttribute does: recycle turns recycling on, and recycle="false" turns it off.

Why recycling is opt-in

Lynx's native list reuses item views by default. AngularLynx asks for recycle because of what a reused view keeps.

  • In an @for, track ties each view to a key. With a key of the item's own, such as contact.id, Angular moves a view with its item when the items change order, and destroys it with its item. track $index ties a view to a place in the list instead, for a list that doesn't change. See why track is important.
  • Recycling gives a view to another item, whatever its key. The view of an item scrolled away goes to the item scrolled to, its components' signals included. So a row that keeps state of its own, such as a toggle or a field's text, shows it on the next item it's given.
  • An @for tracked by its items' own keys doesn't do that anywhere else in Angular. Recycling by default would change what existing @for code does on a list. With recycle, you choose it where the rows are built for it.

*lynxListFor

Repeats a <list-item> for each item, as @for does. But it builds an item's view only when the list asks for it, as the item scrolls into view.

  • It reuses views. The views of items scrolled away go to the items scrolled to.
  • A list of 1,000 rows builds about as many views as fit on the screen.
  • It has the shape of the Angular CDK's *cdkVirtualFor, and works like React Native's FlashList.
  • LYNX_ELEMENTS includes it.
src/app/contacts.ts
import { Component, signal } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

type Contact = { id: string; name: string };

@Component({
  selector: 'app-contacts',
  imports: [LYNX_ELEMENTS],
  template: `
    <list list-type="single" style="height: 100vh;">
      <list-item
        *lynxListFor="let contact of contacts(); trackBy: byId; let i = index"
      >
        <text>{{ i + 1 }}. {{ contact.name }}</text>
      </list-item>
    </list>
  `,
})
export class Contacts {
  readonly contacts = signal<Contact[]>([{ id: '1', name: 'Ada Lovelace' }]);
  readonly byId = (_: number, contact: Contact) => contact.id;
}

The list needs each item's item-key and layout before the item exists. So they come from the data, not the <list-item>'s attributes:

InputTypeDescription
ofreadonly T[] | Iterable<T> | null | undefinedThe items. An array changed in place shows too.
trackBy(index: number, item: T) => unknownEach item's key, which becomes its item-key (as a string). Make it unique and stable, such as its id.
itemAttributes(item: T, index: number) => LynxListItemAttributesEach item's layout attributes. See Item layout.
  • Without trackBy, an item is keyed by identity, so new objects for the same data are new items.
  • A repeated key is made unique, with a warning.
  • The template context is LynxListForContext. let item is the item, and index, count, first, last, even and odd are there as in @for.

Item layout

itemAttributes returns each item's estimated-main-axis-size-px, full-span, sticky-top, sticky-bottom, recyclable and reuse-identifier. They're the <list-item> attributes of those names. Written on the <list-item> itself, they aren't read until it's built.

  • Give items that look different different reuse-identifiers, such as a section header and a row. An item only reuses the view of an item with the same one.
  • Estimate each item's size closely. An estimate close to the real size keeps the scroll bar steady before the items are drawn.
<list-item
  *lynxListFor="let row of rows(); trackBy: byId; itemAttributes: layout"
>
  …
</list-item>
readonly layout = (row: Row): LynxListItemAttributes => ({
  'reuse-identifier': row.header ? 'header' : 'row',
  'estimated-main-axis-size-px': row.header ? 32 : 64,
  'sticky-top': row.header,
});

What a reused view keeps

Warning

A reused view keeps everything that isn't bound to its item: a component's own signals, a field's text and focus, a nested scroll position, an animation.

  • Derive what an item shows from the item: an input(), computed() or a linkedSignal() of it.
  • Keep what the user changes in the parent, by key.
  • Opt an item out with recyclable: false in itemAttributes if its view must never show another item, such as one with a field the user is typing in. It gets its own view back each time it scrolls into view, as it left it.

How it behaves

  • Where it goes: on the <list-item> itself, directly in the <list>. Or in content a component projects into its list, as <ui-list> does. Static <list-item>s and @for items can sit beside it.
  • When it's checked: an item's view is checked when the list asks for it, and with the rest of the page after that.
  • Views waiting to be reused keep their last item, and are checked with their parent. There are only a handful.
  • A list inside an item works, and is updated with its item.
  • Keep rows cheap to check. The list waits for each item's check while it lays out, so a quick row scrolls more smoothly.
    • The biggest cost is often an @for inside the row whose keys change from item to item, such as type badges tracked by type. @for never gives a view to another key, so a reused row builds those views again.
    • Track values like these by $index when their views keep no state. Or write out a short list with a known maximum.
    • In a measured row, these made each check 22% and 45% cheaper.
  • In the web preview, Lynx for Web's <list> asks for every item at once and never lets one go. So every item gets a view there.

Attributes

AttributeTypeDefaultDescription
list-type'single' | 'flow' | 'waterfall''single'Layout: one column, a grid with aligned rows, or a masonry grid where each item goes into the shortest column.
span-countnumber1Columns (or rows, when horizontal) for flow and waterfall.
scroll-orientation'vertical' | 'horizontal''vertical'Scroll and layout direction.
enable-scrollbooleantrueWhether the user can scroll it.
enable-nested-scrollbooleanfalseiOS and Android. Lets it scroll inside another scroll container: the inner one scrolls first.
bouncesbooleantrueiOS only. Bounce at the edges.
scroll-bar-enablebooleantrueiOS only. Shows the scroll bar.
initial-scroll-indexnumber0Item to scroll to after the first render.
upper-threshold-item-countnumber0bindscrolltoupper fires when fewer than this many items remain above the screen.
lower-threshold-item-countnumber0bindscrolltolower fires when fewer than this many items remain below the screen. Use it to load more.
scroll-event-throttlenumber200Minimum milliseconds between bindscroll events.
need-visible-item-infobooleanfalseAdds attachedCells, the items on screen, to the scroll events.
need-layout-complete-infobooleanfalseiOS and Android. Adds item positions and the update's diff to bindlayoutcomplete.
layout-idnumber-1iOS and Android. An id you set with each data update. bindlayoutcomplete reports it back.
preload-buffer-countnumber0iOS and Android. Items to render ahead, outside the screen, so they don't pop in.
item-snap{ factor: number; offset: number; maxSnapCount?: number }—Paging: after each scroll, an item stops at factor (0 top, 1 bottom) plus offset px. single lists only.
stickybooleanfalseAllows items with sticky-top or sticky-bottom to pin.
sticky-offsetnumber0Distance in px from the edge at which sticky items pin.
update-animation'default' | 'none'—'default' animates inserted and removed items. single and flow lists only.
enable-async-listbooleantrue (set by AngularLynx)Lynx's native list needs it to render items in an AngularLynx app. Leave it on.
enable-insert-platform-view-operationbooleanfalseiOS (Lynx 4.0 and later) and Android. Inserts item views through the native list's own operations.
experimental-search-ref-anchor-strategy0 | 1 | 20Experimental, Lynx 4.0 and later. How the list keeps its place when on-screen items are removed: 0 doesn't, 1 searches toward the start, 2 toward the end.

Plus the common attributes, such as id, class and style.

Item gaps are CSS properties that only <list> understands, not attributes. Set list-main-axis-gap and list-cross-axis-gap in style, such as style="list-main-axis-gap: 8px;".

Events

Read the payload from $event.detail. Angular types $event for each event: see Element events.

EventdetailDescription
bindscroll{ scrollTop: number; scrollLeft: number; scrollHeight: number; scrollWidth: number; deltaX: number; deltaY: number; listWidth: number; listHeight: number; eventSource: number; attachedCells?: object[] }Fires while it scrolls, at most every scroll-event-throttle ms. attachedCells needs need-visible-item-info.
bindscrolltoupperSame as bindscrollNear the start, by upper-threshold-item-count.
bindscrolltolowerSame as bindscrollNear the end, by lower-threshold-item-count. Use it to load more.
bindscrollstatechange{ state: 1 | 2 | 3 | 4 }iOS and Android. Scroll state: 1 stopped, 2 dragging, 3 slowing down, 4 animating.
bindlayoutcomplete{ 'layout-id': number; scrollInfo?: object; diffResult?: object; visibleItemBeforeUpdate?: object[]; visibleItemAfterUpdate?: object[] }Layout finished, after the first render or an update. The optional fields need need-layout-complete-info.
bindsnap{ position: number; currentScrollTop: number; currentScrollLeft: number; targetScrollTop: number; targetScrollLeft: number }With item-snap: about to snap to the item at position.

Plus the common events, such as bindtap.

Methods

These UI methods scroll it from code. Call them with invoke() on the element (see Call element methods).

A call from a handler returns nothing. To read a result, call invoke() in a main-thread script instead.

MethodParametersDescription
scrollToPosition{ position: number; alignTo?: 'top' | 'middle' | 'bottom'; offset?: number; itemKey?: string; smooth?: boolean }Scrolls to the item at position, or the one with itemKey. Aligns it as alignTo says (its top by default), then scrolls offset px further.
scrollBy{ offset: number }Scrolls by offset px.
autoScroll{ rate: string; start?: boolean; autoStop?: boolean }Scrolls continuously at rate per second, e.g. '60px'. It stops at the end unless autoStop is false.
getVisibleCells—Returns the items on screen.
getScrollInfo—Returns { scrollX, scrollY, maxScrollOffset } in px.
  • In the web preview, scrollToPosition scrolls to the item's top plus offset, and ignores alignTo and itemKey.
import { Component, ElementRef, viewChild } from '@angular/core';
import { LYNX_ELEMENTS } from '@blotch/angular-lynx';

@Component({
  selector: 'app-inbox',
  imports: [LYNX_ELEMENTS],
  template: `
    <list #inbox list-type="single" style="height: 400px;">
      <list-item item-key="welcome"><text>Welcome</text></list-item>
    </list>
    <view (bindtap)="toTop()"><text>Back to top</text></view>
  `,
})
export class Inbox {
  readonly inbox = viewChild.required('inbox', { read: ElementRef });

  toTop(): void {
    this.inbox().nativeElement.invoke?.('scrollToPosition', {
      position: 0,
      smooth: true,
    });
  }
}

Children

  • Every direct child must be a <list-item>.
  • Don't set z-index on a <list-item>. It breaks view reuse. Set it on the content inside.

<list-item>

AttributeTypeDefaultDescription
item-keystring | number—A unique, stable key for the item. Without one, the item is keyed by its element.
full-spanbooleanfalseTakes a whole row (or column) in a flow or waterfall list.
sticky-topbooleanfalsePins to the top while it scrolls. Needs sticky on the list.
sticky-bottombooleanfalsePins to the bottom. Needs sticky on the list.
recyclablebooleantruefalse keeps the item alive off screen, so it isn't rendered again when it comes back.
reuse-identifierstring—Only items with the same value share reused views. Give items with very different layouts different values.
estimated-main-axis-size-pxnumber-1Expected size in px along the scroll direction, used until the item renders. The closer it is, the less the list flickers.