Fragiola

API: utilities

Async list

useAsyncList or injectAsyncList loads a list's pages through your loader, filtered by a text, aborting a stale load, for the virtual lists.

A list whose items come from a loader, page by page: useAsyncList (React, Vue) and injectAsyncList (Angular) keep the items, the status and the error in the framework's state, and give loadMore and setFilterText to the virtual lists (ListboxVirtual, SelectVirtual, ComboboxVirtual, AutocompleteVirtual). Base UI leaves async loading to the consumer; React Aria's useAsyncList was read for the behaviour. The items keep the loader's order.

It is a utility in its own entry, /async-list, which references no component, on the toolkit's /async-list mechanism: you bring the fetching, it brings the order of the pages.

Behaviour

  • The loader gets { signal, filterText, cursor } and returns { items, cursor? }: cursor is undefined for the first page, else the one the previous page returned; a page with no cursor (or null) is the last (hasMore false).
  • The first page loads once mounted (Angular: after the first render). The first render, on a server too, has status loading and no items.
  • loadMore() loads the next page and appends it; it does nothing while a page loads or when there is none. Wire it to a virtual list's onLoadMore.
  • setFilterText(text) loads the first page for the text; reload() the first page again. The items stay until the new page arrives.
  • A new load aborts the one in flight (its signal), and a stale page is never used, whichever arrives first: typing 1 then 12 never shows the page of 1.
  • An error sets status to error and error to what the loader threw; the items stay.
  • Destroyed: the load in flight is aborted.

Usage

import { useAsyncList } from "@fragiola/primitives-react/async-list";

const list = useAsyncList<Item, number>({
    load: async ({ signal, filterText, cursor = 0 }) => {
        const response = await fetch(`/api/items?q=${encodeURIComponent(filterText)}&from=${cursor}`, { signal });
        const page: { items: Item[]; next: number | null } = await response.json();
        return { items: page.items, cursor: page.next };
    },
});

<ListboxVirtual.Root aria-label="Items" items={list.items} loading={list.loading} onLoadMore={list.loadMore}>
    {(item) => <Listbox.Item value={item.value}>{item.label}</Listbox.Item>}
</ListboxVirtual.Root>;

Props

Options

optiontypedefaultdescription
load({ signal, filterText, cursor }) => Promise<{ items, cursor? }>required: loads a page (AsyncListLoad)
filterTextstring""the first filter text

Result

nametypedescription
itemsItem[]the items of every page loaded
filterTextstringthe text the items were asked for
status"idle" | "loading" | "loading-more" | "error"loading: the first page (or a new text); loading-more: the next one
loadingbooleanstatus is loading or loading-more: a virtual list's loading
errorunknownwhat the last failed load threw, else undefined
hasMorebooleanthe last page gave a cursor
reload()() => voidloads the first page again
loadMore()() => voidloads the next page: a virtual list's onLoadMore
setFilterText(text)(text: string) => voidloads the first page for a new text

React returns them in its state (a new object on each change), Vue as a reactive object, Angular as signals (items(), loading(), ...) and plain functions.

Exports

entryexports
@fragiola/primitives-react/async-listAsyncListLoad, AsyncListPage, AsyncListRequest, AsyncListResult, AsyncListState, AsyncListStatus, UseAsyncListOptions, useAsyncList
@fragiola/primitives-vue/async-listAsyncListLoad, AsyncListPage, AsyncListRequest, AsyncListResult, AsyncListState, AsyncListStatus, UseAsyncListOptions, useAsyncList
@fragiola/primitives-angular/async-listAsyncListLoad, AsyncListPage, AsyncListRef, AsyncListRequest, AsyncListStatus, InjectAsyncListOptions, injectAsyncList