> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# `<list>`

A **virtualized** scrolling list for long or growing data: only items near the screen are drawn. With [`<list recycle>`](#list-recycle) or [`*lynxListFor`](#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

```typescript title="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](/api/angular-lynx/elements/index.md)).

:::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`.

```typescript title="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`](#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](#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 `styleUrl`s 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`](https://angular.dev/api/core/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](https://angular.dev/guide/templates/control-flow#why-is-track-in-for-blocks-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.**

```typescript title="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:

| Input            | Type                                                 | Description                                                                                             |
| ---------------- | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
| `of`             | `readonly T[] \| Iterable<T> \| null \| undefined`   | The items. An array changed in place shows too.                                                         |
| `trackBy`        | `(index: number, item: T) => unknown`                | Each 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) => LynxListItemAttributes` | Each item's layout attributes. See [Item layout](#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](#list-item) of those names. Written on the `<list-item>` itself, they aren't read until it's built.

- **Give items that look different different `reuse-identifier`s,** 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.

```html
<list-item
  *lynxListFor="let row of rows(); trackBy: byId; itemAttributes: layout"
>
  …
</list-item>
```

```typescript
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>`](/dolan/list.md) 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

| Attribute                                 | Type                                                        | Default                     | Description                                                                                                                                                      |
| ----------------------------------------- | ----------------------------------------------------------- | --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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-count`                              | `number`                                                    | `1`                         | Columns (or rows, when horizontal) for `flow` and `waterfall`.                                                                                                   |
| `scroll-orientation`                      | `'vertical' \| 'horizontal'`                                | `'vertical'`                | Scroll and layout direction.                                                                                                                                     |
| `enable-scroll`                           | `boolean`                                                   | `true`                      | Whether the user can scroll it.                                                                                                                                  |
| `enable-nested-scroll`                    | `boolean`                                                   | `false`                     | iOS and Android. Lets it scroll inside another scroll container: the inner one scrolls first.                                                                    |
| `bounces`                                 | `boolean`                                                   | `true`                      | iOS only. Bounce at the edges.                                                                                                                                   |
| `scroll-bar-enable`                       | `boolean`                                                   | `true`                      | iOS only. Shows the scroll bar.                                                                                                                                  |
| `initial-scroll-index`                    | `number`                                                    | `0`                         | Item to scroll to after the first render.                                                                                                                        |
| `upper-threshold-item-count`              | `number`                                                    | `0`                         | `bindscrolltoupper` fires when fewer than this many items remain above the screen.                                                                               |
| `lower-threshold-item-count`              | `number`                                                    | `0`                         | `bindscrolltolower` fires when fewer than this many items remain below the screen. Use it to load more.                                                          |
| `scroll-event-throttle`                   | `number`                                                    | `200`                       | Minimum milliseconds between `bindscroll` events.                                                                                                                |
| `need-visible-item-info`                  | `boolean`                                                   | `false`                     | Adds `attachedCells`, the items on screen, to the scroll events.                                                                                                 |
| `need-layout-complete-info`               | `boolean`                                                   | `false`                     | iOS and Android. Adds item positions and the update's diff to `bindlayoutcomplete`.                                                                              |
| `layout-id`                               | `number`                                                    | `-1`                        | iOS and Android. An id you set with each data update. `bindlayoutcomplete` reports it back.                                                                      |
| `preload-buffer-count`                    | `number`                                                    | `0`                         | iOS 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.                                                |
| `sticky`                                  | `boolean`                                                   | `false`                     | Allows items with `sticky-top` or `sticky-bottom` to pin.                                                                                                        |
| `sticky-offset`                           | `number`                                                    | `0`                         | Distance 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-list`                       | `boolean`                                                   | `true` (set by AngularLynx) | Lynx's native list needs it to render items in an AngularLynx app. Leave it on.                                                                                  |
| `enable-insert-platform-view-operation`   | `boolean`                                                   | `false`                     | iOS (Lynx 4.0 and later) and Android. Inserts item views through the native list's own operations.                                                               |
| `experimental-search-ref-anchor-strategy` | `0 \| 1 \| 2`                                               | `0`                         | Experimental, 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](/api/elements/view.md#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](/api/angular-lynx/element-events.md#list).

| Event                   | `detail`                                                                                                                                                                                                     | Description                                                                                                       |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------- |
| `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`. |
| `bindscrolltoupper`     | Same as `bindscroll`                                                                                                                                                                                         | Near the start, by `upper-threshold-item-count`.                                                                  |
| `bindscrolltolower`     | Same as `bindscroll`                                                                                                                                                                                         | Near 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](/api/elements/view.md#events), such as `bindtap`.

## Methods

These **[UI methods](/guide/glossary.md#ui-method)** scroll it from code. Call them with `invoke()` on the element (see [Call element methods](/guide/selector-query.md)).

A call from a handler returns nothing. To read a result, call `invoke()` in a [main-thread script](/guide/glossary.md#main-thread-script) instead.

| Method             | Parameters                                                                                                           | Description                                                                                                                                       |
| ------------------ | -------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `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`.

```typescript
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>`

| Attribute                     | Type               | Default | Description                                                                                                                |
| ----------------------------- | ------------------ | ------- | -------------------------------------------------------------------------------------------------------------------------- |
| `item-key`                    | `string \| number` | —       | A unique, stable key for the item. Without one, the item is keyed by its element.                                          |
| `full-span`                   | `boolean`          | `false` | Takes a whole row (or column) in a `flow` or `waterfall` list.                                                             |
| `sticky-top`                  | `boolean`          | `false` | Pins to the top while it scrolls. Needs `sticky` on the list.                                                              |
| `sticky-bottom`               | `boolean`          | `false` | Pins to the bottom. Needs `sticky` on the list.                                                                            |
| `recyclable`                  | `boolean`          | `true`  | `false` keeps the item alive off screen, so it isn't rendered again when it comes back.                                    |
| `reuse-identifier`            | `string`           | —       | Only items with the same value share reused views. Give items with very different layouts different values.                |
| `estimated-main-axis-size-px` | `number`           | `-1`    | Expected size in px along the scroll direction, used until the item renders. The closer it is, the less the list flickers. |

## Related

- [`<scroll-view>`](/api/elements/scroll-view.md): for a short, fixed set of children
- [List](/dolan/list.md): the UI component built on it
- [CSS on Lynx](/guide/styling/index.md)
- [Call element methods](/guide/selector-query.md)
