Skip to content

<InfiniteScroll> ​

Loads the next (or previous) page of a paginated prop as the user scrolls, and keeps the items already loaded. The server sends the prop as an Inertia scroll prop: one page of items, merged into what the page already has, plus the pagination details Inertia reads from the page's scrollProps.

gts
import type { TOC } from '@ember/component/template-only';
import { InfiniteScroll, type PageSignature } from 'inertia-ember';

interface Post {
  id: number;
  title: string;
}

export default <template>
  <InfiniteScroll @data='posts'>
    {{#each @props.posts.data key='id' as |post|}}
      <article>{{post.title}}</article>
    {{/each}}
  </InfiniteScroll>
</template> satisfies TOC<PageSignature<{ posts: { data: Post[] } }>>;

The items are under data here, the usual shape for scroll props; read them from wherever your server puts them.

Arguments ​

ArgumentTypeDefaultDescription
@datastringrequiredThe name of the scroll prop.
@buffernumber0How many pixels before a trigger enters the viewport to start loading.
@asstring'div'The tag of the element that wraps the items: 'ul' when they're <li>s.
@manualbooleanfalseDon't load on scroll; load only when your <:next> or <:previous> block calls fetch.
@manualAfternumber0Load on scroll for this many requests, then switch to manual. 0 never switches.
@preserveUrlbooleanfalseKeep the URL as it is. Otherwise Inertia updates the page parameter in the URL as pages scroll by.
@reversebooleanfalseFor lists that grow upward, like a chat: the next-page trigger goes above the items and the previous-page trigger below.
@autoScrollboolean@reverseScroll the list's scroll container (or the window) to the bottom once it's set up.
@onlyNextbooleanfalseOnly load next pages.
@onlyPreviousbooleanfalseOnly load previous pages.
@paramsReloadOptions{}More options for each page request, as for router.reload (headers, data, onSuccess, …).
@itemsElementstring | () => HTMLElementthe wrapperThe element that holds the items, as a CSS selector or a function. Use it when the items aren't direct children of the wrapper.
@startElementstring | () => HTMLElementa <div>Your own element to trigger loading the previous page. Replaces the built-in one, and with it <:previous>.
@endElementstring | () => HTMLElementa <div>Your own element to trigger loading the next page. Replaces the built-in one, and with it <:next>.

The element arguments are read once, when the component sets up.

Blocks ​

BlockYieldsShows
default{ loading, loadingPrevious, loadingNext }The items, inside the wrapper.
<:previous>the previous-page state (below)Always, in the previous-page trigger. Replaces <:loading> on that side.
<:next>the next-page state (below)Always, in the next-page trigger. Replaces <:loading> on that side.
<:loading>the state of the side that's loadingIn a trigger while its page loads, when that side has no <:previous> or <:next>.

Without named blocks, the content is the default block. With them, put the items in <:default>.

<:previous>, <:next> and <:loading> yield:

KeyTypeDescription
loadingbooleanThis side's page is loading.
fetch() => voidLoad this side's page now.
hasMorebooleanThere's another page on this side.
autoModebooleanThis side loads on scroll.
manualModebooleanThe opposite of autoMode: with @manual, after @manualAfter, and on the side that @onlyNext or @onlyPrevious turns off.
loadingPrevious, loadingNextbooleanEither side's loading state.
hasPrevious, hasNextbooleanWhether there are more pages on either side.

Element and attributes ​

<InfiniteScroll> renders three siblings: a <div> trigger for the previous page, the wrapper (@as, a <div> by default) with the items, and a <div> trigger for the next page. With @reverse the triggers swap places. ...attributes go on the wrapper. The triggers are watched with an IntersectionObserver; they render empty unless a block fills them, and aren't rendered when @startElement or @endElement replaces them.

Examples ​

A grid ​

...attributes style the wrapper, and <:loading> shows under the items while the next page loads:

gts
import type { TOC } from '@ember/component/template-only';
import { InfiniteScroll, type PageSignature } from 'inertia-ember';

interface Photo {
  id: number;
  url: string;
}

export default <template>
  <InfiniteScroll @data='photos' @as='ul' @buffer={{400}} class='grid'>
    <:default>
      {{#each @props.photos.data key='id' as |photo|}}
        <li><img src={{photo.url}} alt='' /></li>
      {{/each}}
    </:default>
    <:loading>Loading more photos…</:loading>
  </InfiniteScroll>
</template> satisfies TOC<PageSignature<{ photos: { data: Photo[] } }>>;

A "Load more" button ​

@manualAfter={{3}} loads three pages on scroll, then waits for a click. The <:next> block shows a button when the next side is manual and there's more to load:

gts
import type { TOC } from '@ember/component/template-only';
import { on } from '@ember/modifier';
import { InfiniteScroll, type PageSignature } from 'inertia-ember';

interface Post {
  id: number;
  title: string;
}

export default <template>
  <InfiniteScroll @data='posts' @manualAfter={{3}}>
    <:default>
      {{#each @props.posts.data key='id' as |post|}}
        <article>{{post.title}}</article>
      {{/each}}
    </:default>
    <:next as |next|>
      {{#if next.loading}}
        <p>Loading…</p>
      {{else if next.manualMode}}
        {{#if next.hasMore}}
          <button type='button' {{on 'click' next.fetch}}>Load more</button>
        {{/if}}
      {{/if}}
    </:next>
  </InfiniteScroll>
</template> satisfies TOC<PageSignature<{ posts: { data: Post[] } }>>;

With @manual={{true}} it waits for a click from the first page.

A chat that loads older messages upward ​

With @reverse, the next-page trigger sits above the messages and the list starts scrolled to the bottom. Older messages load as the user scrolls up. Inside a scrolling container, that container is the one scrolled:

gts
import type { TOC } from '@ember/component/template-only';
import { InfiniteScroll, type PageSignature } from 'inertia-ember';

interface Message {
  id: number;
  author: string;
  body: string;
}

export default <template>
  <div style='height: 400px; overflow-y: auto'>
    <InfiniteScroll @data='messages' @reverse={{true}}>
      <:default>
        {{#each @props.messages.data key='id' as |message|}}
          <p><strong>{{message.author}}</strong> {{message.body}}</p>
        {{/each}}
      </:default>
      <:loading>Loading older messages…</:loading>
    </InfiniteScroll>
  </div>
</template> satisfies TOC<PageSignature<{ messages: { data: Message[] } }>>;

The order the items arrive in and how pages merge are up to the server and Inertia; see Inertia's infinite scroll docs for reverse mode.

Rows of a table ​

A <tbody> can't sit inside a <div>, so render the table in the default block and point @itemsElement at its body:

gts
import type { TOC } from '@ember/component/template-only';
import { InfiniteScroll, type PageSignature } from 'inertia-ember';

interface User {
  id: number;
  name: string;
  email: string;
}

export default <template>
  <InfiniteScroll @data='users' @itemsElement='#user-rows'>
    <table>
      <thead>
        <tr><th>Name</th><th>Email</th></tr>
      </thead>
      <tbody id='user-rows'>
        {{#each @props.users.data key='id' as |user|}}
          <tr><td>{{user.name}}</td><td>{{user.email}}</td></tr>
        {{/each}}
      </tbody>
    </table>
  </InfiniteScroll>
</template> satisfies TOC<PageSignature<{ users: { data: User[] } }>>;

Notes ​

  • The first page comes with the page itself, and whether there are more pages on each side comes from the page's scrollProps.
  • During server rendering nothing is observed: the HTML has the items and empty triggers, and loading starts once the page hydrates.
  • To start over from the first page (after changing a filter, say), visit the page with the prop in reset, for example router.get('/posts', { q }, { reset: ['posts'] }) or <Link @reset={{array 'posts'}}>.

Related: Props that arrive later, <Link>, and Inertia's infinite scroll and merging props docs.

Released under the MIT License.