Skip to content
Elements InfiniteScroll

InfiniteScroll

A sentinel that asks for the next items when it comes into view, with a button and a link for keyboards, screen readers and pages without .NET.

On this page

Examples

Closest scrollable ancestor

OnLoadMore runs once each time the sentinel is reached, and not again until the load is over (an async handler counts as running until it returns). Static rendering and the moments before the page is interactive have no handler: the button is a link to Href and the next page loads from the address. The "Load more" button is always in the tab order and does the same by hand.

  • Event 1
  • Event 2
  • Event 3
  • Event 4
  • Event 5
  • Event 6
  • Event 7
  • Event 8
  • Event 9
  • Event 10
  • Event 11
  • Event 12
  • Event 13
  • Event 14
  • Event 15
  • Event 16
  • Event 17
  • Event 18
  • Event 19
  • Event 20
Load more

Showing 20 of 60

Load requests: 0

Razor
@inject NavigationManager Navigation

<div class="flex w-full max-w-md flex-col gap-3">
    <div role="region" aria-label="Activity feed" tabindex="0" data-testid="infinite-feed"
         class="h-64 overflow-y-auto rounded-lg border border-(--klyroo-border) bg-(--klyroo-bg)">
        <ul>
            @for (var i = 1; i <= Shown; i++)
            {
                <li class="border-b border-(--klyroo-border) px-3 py-2 text-sm">Event @i</li>
            }
        </ul>
        <KlyInfiniteScroll HasMore="HasMore" OnLoadMore="LoadAsync" Href="@NextHref" ShowEnd data-testid="infinite-more" />
    </div>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="infinite-count">Showing @Shown of @Total</p>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="infinite-requests">Load requests: @_requests</p>
</div>

@code {
    private const int PageSize = 20;
    private const int Total = 60;

    private int _pages = 1;
    private int _requests;

    // A load takes 600 ms here, as a request would. ?delay=<ms> stretches it (up to two minutes) to look at the busy state for as long as needed.
    private int _delay = 600;

    // The document has a <base href>, so a bare "?feed=" would resolve against the site root: keep the current path.
    private string _path = "";

    private int Shown => Math.Min(_pages * PageSize, Total);
    private bool HasMore => Shown < Total;
    private string NextHref => $"{_path}?feed={_pages + 1}";

    protected override void OnInitialized()
    {
        var uri = new Uri(Navigation.Uri);
        _path = uri.AbsolutePath;
        // Without .NET the link of the button reloads the page with one more page of events.
        var query = System.Web.HttpUtility.ParseQueryString(uri.Query);
        _pages = int.TryParse(query["feed"], out var pages) ? Math.Clamp(pages, 1, Total / PageSize) : 1;
        if (int.TryParse(query["delay"], out var delay)) _delay = Math.Clamp(delay, 0, 120_000);
    }

    // The component counts the load as running until this returns: no Loading flag is needed for an async handler.
    private async Task LoadAsync()
    {
        _requests++;
        await Task.Delay(_delay);
        _pages++;
    }
}

Explicit root and Loading

Root names the scrolling element by selector, and the parent owns Loading: the sentinel is checked again when it turns false, so a sentinel still in view loads the next items. Without Href, the button waits for the page to be interactive.

  • line 1
  • line 2
  • line 3
  • line 4
  • line 5
  • line 6
  • line 7
  • line 8
  • line 9
  • line 10
  • line 11
  • line 12
  • line 13
  • line 14
  • line 15
  • line 16
  • line 17
  • line 18
  • line 19
  • line 20

Lines: 20

Razor
<div class="flex w-full max-w-md flex-col gap-3">
    <div id="log-scroll" role="region" aria-label="Log" tabindex="0" data-testid="log-feed"
         class="h-64 overflow-y-auto rounded-lg border border-(--klyroo-border) bg-(--klyroo-bg)">
        <ul>
            @for (var i = 1; i <= _count; i++)
            {
                <li class="border-b border-(--klyroo-border) px-3 py-2 font-mono text-xs">line @i</li>
            }
        </ul>
        <KlyInfiniteScroll Root="#log-scroll" RootMargin="0px" Loading="_loading" HasMore="_count < 45" OnLoadMore="Load" ShowEnd data-testid="log-more" />
    </div>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="log-count">Lines: @_count</p>
</div>

@code {
    private int _count = 20;
    private bool _loading;

    // The handler returns at once and the parent owns Loading: the sentinel is checked again when it turns false.
    private void Load()
    {
        _loading = true;
        _ = Finish();
    }

    private async Task Finish()
    {
        await Task.Delay(500);
        _count += 15;
        _loading = false;
        await InvokeAsync(StateHasChanged);
    }
}

A load that adds nothing

After a load that did not grow the content (nothing to add, or it failed) nothing is asked again until the sentinel has left the view and come back, or the button is used: a sentinel that stays in view would loop. A real handler sets HasMore to false, or shows a retry.

  • Event 1
  • Event 2

Load requests: 0

Razor
<div class="flex w-full max-w-md flex-col gap-3">
    <div class="rounded-lg border border-(--klyroo-border) bg-(--klyroo-bg)" data-testid="empty-feed">
        <ul>
            <li class="border-b border-(--klyroo-border) px-3 py-2 text-sm">Event 1</li>
            <li class="border-b border-(--klyroo-border) px-3 py-2 text-sm">Event 2</li>
        </ul>
        <KlyInfiniteScroll OnLoadMore="LoadAsync" data-testid="empty-more" />
    </div>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="empty-requests">Load requests: @_requests</p>
</div>

@code {
    private int _requests;

    // A load that adds nothing (it failed, or there was nothing): the sentinel is still in view, and asking again at once would loop.
    // It is asked again when the sentinel has left the view and come back, or by the button. A real handler would set HasMore to false, or show a retry.
    private Task LoadAsync()
    {
        _requests++;
        return Task.CompletedTask;
    }
}

The page scrolls

A wrapper with overflow-x-hidden has a computed overflow-y of auto though nothing scrolls in it: only an ancestor that really scrolls is taken as the root, else the viewport. Put aria-busy on your list while it loads: the component does not, because it would hide its own announcement.

  • Row 1
  • Row 2
  • Row 3
  • Row 4
  • Row 5
  • Row 6
  • Row 7
  • Row 8
  • Row 9
  • Row 10
  • Row 11
  • Row 12
  • Row 13
  • Row 14
  • Row 15
  • Row 16
  • Row 17
  • Row 18
  • Row 19
  • Row 20

Showing 20 of 60

Load requests: 0

Razor
<div class="flex w-full max-w-md flex-col gap-3">
    @* overflow-x-hidden makes the computed overflow-y "auto" too, though nothing scrolls here: the page does. *@
    <div class="overflow-x-hidden rounded-lg border border-(--klyroo-border) bg-(--klyroo-bg)" data-testid="wrap-feed">
        <ul>
            @for (var i = 1; i <= _shown; i++)
            {
                <li class="border-b border-(--klyroo-border) px-3 py-2 text-sm">Row @i</li>
            }
        </ul>
        <KlyInfiniteScroll HasMore="_shown < 60" OnLoadMore="LoadAsync" data-testid="wrap-more" />
    </div>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="wrap-count">Showing @_shown of 60</p>
    <p class="text-sm text-(--klyroo-text-muted)" data-testid="wrap-requests">Load requests: @_requests</p>
</div>

@code {
    private int _shown = 20;
    private int _requests;

    private async Task LoadAsync()
    {
        _requests++;
        await Task.Delay(400);
        _shown += 20;
    }
}

API

KlyInfiniteScroll

Load-more point for a growing list: it reports each time the user scrolls near it.

Parameters

Name Type Default Description
Class string Classes added to the root element, merged last so they win over the theme.
HasMore bool true Whether there is anything left to load. When false the sentinel and the button are removed and the end is announced.
Href string Where the next items are, for a page without .NET (static rendering, or before the page is interactive): the button becomes a link to it, like ?page=2 or the next cursor URL. Once the page is interactive the button loads in place instead.
Label string Text of the button. Defaults to the translated "Load more".
Loading bool false A load is running. While it is true nothing is requested again; when it turns false the sentinel is checked again, so a sentinel still in view asks for the next items. A load started through OnLoadMore counts as running until the callback returns, so an async handler does not have to set this. The component does not set aria-busy (it would hide its own announcement): put aria-busy on your list while it loads.
Root string A CSS selector of the element that scrolls ( "#feed" ). By default the closest scrollable ancestor is used ( overflow-y auto or scroll), or the viewport when there is none.
RootMargin string "0px 0px 200px 0px" How far before the sentinel is really in view the load starts, as the margin of an IntersectionObserver (pixels or percent, CSS order top right bottom left). The default loads 200px ahead of the end of a list; for a list that loads upward put the component first and use "200px 0px 0px 0px" .
ShowEnd bool false Shows the translated "No more items" when HasMore is false. It is announced either way.
Ui InfiniteScrollUi Per-instance class overrides of the slots of the infinite scroll, see InfiniteScrollUi . They are merged after the theme and the Class parameter wins on the root slot.

Events

Name Type Description
OnLoadMore EventCallback Asked for the next items: each time the sentinel comes into view and when the button is used, never twice for one visibility until the load is over. Only runs when the page is interactive. After a load that added nothing (or threw), nothing is asked again until the sentinel has left the view and come back, or the button is used: a handler that has nothing more to give, or failed, should set HasMore to false, or show a retry.

Ui slots

Ui takes a InfiniteScrollUi with one class string per slot, merged after the theme.

Slot Styles
Root Classes merged into the root slot of KlyInfiniteScroll , after the theme and the app configuration.
Sentinel Classes merged into the sentinel slot of KlyInfiniteScroll , after the theme and the app configuration.
Trigger Classes merged into the trigger slot of KlyInfiniteScroll , after the theme and the app configuration.
Spinner Classes merged into the spinner slot of KlyInfiniteScroll , after the theme and the app configuration.
Status Classes merged into the status slot of KlyInfiniteScroll , after the theme and the app configuration.
End Classes merged into the end slot of KlyInfiniteScroll , after the theme and the app configuration.
Marker Classes merged into the marker slot of KlyInfiniteScroll , after the theme and the app configuration.