<WhenVisible>
Asks the server for props when its spot on the page scrolls into view, and shows a fallback until they arrive. Pair it with props the server marks as optional, which aren't in the first response.
import type { TOC } from '@ember/component/template-only';
import { WhenVisible, type PageSignature } from 'inertia-ember';
export default <template>
<WhenVisible @data='comments'>
<:default>{{@props.comments.length}} comments</:default>
<:fallback>Loading comments…</:fallback>
</WhenVisible>
</template> satisfies TOC<PageSignature<{ comments?: { id: number }[] }>>;Arguments
| Argument | Type | Default | Description |
|---|---|---|---|
@data | string | string[] | none | The props to load, sent as the reload's only. Dot paths work. The content shows once all of them are defined. |
@params | ReloadOptions | {} | More options for the reload, as for router.reload (data, headers, onSuccess, …). preserveErrors defaults to true. @data, when given, sets only. |
@buffer | number | 0 | How many pixels before it enters the viewport to start loading (the observer's rootMargin). |
@always | boolean | false | Reload every time it comes back into view, not just the first time. |
@as | string | 'div' | The tag of the empty element that's watched for visibility. ...attributes go on it. |
Blocks
| Block | Yields | Shows |
|---|---|---|
<:default> | { fetching: boolean } | Once the props in @data are defined, or after the first load finished. |
<:fallback> | nothing | Until then. |
fetching is true while a reload is in flight. It's mostly useful with @always, where the content stays on screen while it reloads.
Element and attributes
<WhenVisible> renders an empty <div data-inertia-when-visible> (or the tag in @as) in front of its blocks, and watches it with an IntersectionObserver. The element is removed once the props have loaded, unless @always is set. The blocks render next to it, not inside it. ...attributes go on that empty element (to give it a height or a class, say); to style the area the content fills, wrap <WhenVisible> in your own element.
Examples
Load comments when the reader gets to them
The server leaves comments out of the first response. They load when the section is 300 px away from the viewport:
import type { TOC } from '@ember/component/template-only';
import { WhenVisible, type PageSignature } from 'inertia-ember';
interface Props {
post: { title: string; body: string };
comments?: { id: number; author: string; body: string }[];
}
export default <template>
<article>
<h1>{{@props.post.title}}</h1>
<p>{{@props.post.body}}</p>
</article>
<section>
<h2>Comments</h2>
<WhenVisible @data='comments' @buffer={{300}}>
<:default>
{{#each @props.comments key='id' as |comment|}}
<p><strong>{{comment.author}}</strong>: {{comment.body}}</p>
{{else}}
<p>No comments yet.</p>
{{/each}}
</:default>
<:fallback><p>Loading comments…</p></:fallback>
</WhenVisible>
</section>
</template> satisfies TOC<PageSignature<Props>>;Fresh data every time it's shown
With @always, the prop reloads each time the element scrolls back into view. Once it has loaded, the old value stays on screen while the new one loads:
import type { TOC } from '@ember/component/template-only';
import { WhenVisible, type PageSignature } from 'inertia-ember';
export default <template>
<WhenVisible @data='activity' @always={{true}}>
<:default as |state|>
{{#if state.fetching}}<small>Updating…</small>{{/if}}
{{#each @props.activity key='id' as |event|}}<p>{{event.text}}</p>{{/each}}
</:default>
<:fallback>Loading activity…</:fallback>
</WhenVisible>
</template> satisfies TOC<PageSignature<{ activity?: { id: number; text: string }[] }>>;Extra reload options
@params takes the same options as router.reload. Here the request carries a query parameter and a callback:
import Component from '@glimmer/component';
import { hash } from '@ember/helper';
import { WhenVisible, type PageSignature } from 'inertia-ember';
export default class Product extends Component<PageSignature<{ reviews?: { id: number; text: string }[] }>> {
loaded = () => console.log('reviews loaded');
<template>
<WhenVisible @data='reviews' @params={{hash data=(hash sort='newest') onSuccess=this.loaded}}>
<:default>
{{#each @props.reviews key='id' as |review|}}<p>{{review.text}}</p>{{/each}}
</:default>
<:fallback>Loading reviews…</:fallback>
</WhenVisible>
</template>
}Inside a list
The watched element is a <div> by default. Inside a <ul>, make it an <li> so the markup stays valid:
<ul>
{{#each @props.sections key='id' as |section|}}<li>{{section.name}}</li>{{/each}}
<WhenVisible @data='moreSections' @as='li'>
<:default>…</:default>
<:fallback><li>Loading…</li></:fallback>
</WhenVisible>
</ul>Notes
- Without
@data, the first reload asks for every prop (or whatever@paramssays), and the content shows once it finishes. - During server rendering the observer doesn't run: the HTML has the empty element and the fallback.
- For props the server sends on its own right after the page renders, use
<Deferred>instead.
Related: Props that arrive later, <Deferred>, router for the reload options, and Inertia's load when visible docs.