Sub topic 8
on viewport, on interaction and on hover
This chapter assumes you know how @defer blocks and @placeholder work, and that you can read standalone component imports. The three triggers here wait for the user, either by scrolling, clicking or pointing. The deferred chunk does not download until one of those things happens.
How the three triggers work
Each trigger watches a DOM element called the trigger element. When the browser reports the matching event, Angular starts loading the chunk and swaps the placeholder for the real content.
| Trigger | Browser mechanism | Fires when |
|---|---|---|
| `on viewport` | `IntersectionObserver` | The element enters the visible area |
| `on interaction` | `click` and `keydown` events | The user clicks or presses a key on the element |
| `on hover` | `mouseenter` and `focusin` events | The pointer enters the element or it receives focus |
Keyboard events are included for interaction and hover, so these triggers also work for people who do not use a mouse.
on viewport
Without an argument, the trigger element is the @placeholder content. This makes the placeholder required.
@defer (on viewport) {
<app-comments [postId]="postId" />
} @placeholder {
<div class="comments-placeholder">Comments load when this area is visible.</div>
}The placeholder must have exactly one root element. A placeholder that contains only text gives Angular nothing to observe. Give the placeholder a size, such as a fixed min-height in CSS, so that the space does not collapse before the content arrives.
You can also observe another element by passing a template reference variable:
<footer #pageEnd>Thanks for reading</footer>
@defer (on viewport(pageEnd)) {
<app-related-posts />
} @placeholder {
<div class="related-placeholder"></div>
}on interaction
Use on interaction for content that most visitors never ask for, such as a chart or an editor. As with on viewport, the placeholder is the default trigger element, and a reference variable lets another element act as the trigger.
<button type="button" #showChart>Show chart</button>
@defer (on interaction(showChart)) {
<app-chart />
} @placeholder {
<p>Press the button to see the chart.</p>
}The trigger element stays in the page after the block renders. The button above remains visible, and clicking it again does nothing more.
on hover
on hover starts loading when the pointer enters the trigger element or the element gains focus. It works best when the user's intent is likely but not certain, for example a menu or a tooltip with rich content.
<a href="/profile" #profileLink>Profile</a>
@defer (on hover(profileLink)) {
<app-profile-card />
} @placeholder {
<span></span>
}Hover cannot fire on a touch screen, because there is no pointer to enter the element. Choose viewport or interaction for features that touch users must reach.
Choosing and combining
You can list several triggers, separated by semicolons. The block loads when any one of them fires.
@defer (on interaction(showChart); on timer(10s)) {
<app-chart />
} @placeholder {
<p>Press the button to see the chart.</p>
}Pair a user trigger with prefetch to hide latency. In the next example, hovering downloads the code, and clicking renders it:
<button type="button" #open>Open editor</button>
@defer (on interaction(open); prefetch on hover(open)) {
<app-editor />
} @placeholder {
<p>Editor is not loaded yet.</p>
}Common errors
- Missing placeholder.
on viewportoron interactionwith no argument and no@placeholderfails to compile. - Text-only placeholder. The placeholder needs a single root element, not bare text.
- Trigger inside the deferred content. The trigger element must exist before the block renders. Referencing a button that lives inside the
@deferblock does not work. - Zero-height placeholder. An empty placeholder can sit permanently in the viewport. It can also jump the layout when the real content replaces it.
Exercise
A page has a "Show map" button with the reference #mapBtn. The map component is large, and you want to download it when users hover over the button but render it only when they click. Write the @defer line.
Show answer
@defer (on interaction(mapBtn); prefetch on hover(mapBtn)) { <app-map /> } @placeholder { <p>Map not loaded.</p> }. The prefetch on hover part downloads the chunk early. The on interaction part renders it on click.
Exercise
Why does @defer (on viewport) { <app-reviews /> } with no @placeholder fail?
Show answer
With no argument, on viewport observes the placeholder's root element. Without a placeholder there is nothing to observe, so you must add a placeholder or pass a reference, as in on viewport(ref).