<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
Import LYNX_ELEMENTS in your component (see Elements).
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.
- The build turns the
@forinto a*lynxListForbefore Angular compiles it, so it behaves as that does.trackgives each item its key, in place ofitem-key. The<list-item>'s layout attributes become itsitemAttributes. $index,$count,$first,$last,$evenand$oddwork, with their aliases (let i = $index), and so does@empty.- Bind a layout attribute to the item and
$indexonly, 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
@forgoes directly in the<list>, or in an@ifor@switchthere, and its body is one<list-item>. - The build warns of an
@forit can't turn into*lynxListFor, and says why. That@forbuilds 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
i18nelement; - its component isn't standalone, so the build can't add the
*lynxListFordirective to itsimports. Make the component standalone; - its template is inline and has escape sequences, such as
\nor\`, whose places the build can't map back to your file. Write the characters themselves, or, for a backtick, move the template to atemplateUrlfile; - a component in its file names its
templateUrlorstyleUrls with a constant rather than a string, which the build can't point at the file from the copy it compiles.
- its body isn't one
- The build sees a
<list recycle>'s@foronly in the same template, written in the component or in atemplateUrlgiven as a string. An@forin a child component's template, or in a template named by a constant, builds every item's view, with no warning. Use*lynxListForthere. - Write
recycleas 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'sbooleanAttributedoes:recycleturns recycling on, andrecycle="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,trackties each view to a key. With a key of the item's own, such ascontact.id, Angular moves a view with its item when the items change order, and destroys it with its item.track $indexties a view to a place in the list instead, for a list that doesn't change. See whytrackis 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
@fortracked by its items' own keys doesn't do that anywhere else in Angular. Recycling by default would change what existing@forcode does on a list. Withrecycle, 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_ELEMENTSincludes it.
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:
- 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 itemis the item, andindex,count,first,last,evenandoddare 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.
What a reused view keeps
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 alinkedSignal()of it. - Keep what the user changes in the parent, by key.
- Opt an item out with
recyclable: falseinitemAttributesif 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@foritems 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
@forinside the row whose keys change from item to item, such as type badges tracked by type.@fornever gives a view to another key, so a reused row builds those views again. - Track values like these by
$indexwhen 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.
- The biggest cost is often an
- 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
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.
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.
- In the web preview,
scrollToPositionscrolls to the item's top plusoffset, and ignoresalignToanditemKey.
Children
- Every direct child must be a
<list-item>. - Don't set
z-indexon a<list-item>. It breaks view reuse. Set it on the content inside.
<list-item>
Related
<scroll-view>: for a short, fixed set of children- List: the UI component built on it
- CSS on Lynx
- Call element methods
