LazyRender
Mount content when it approaches the viewport or a scroll container. Once rendered, it stays mounted and retains its state even after leaving the visible area.
Import
import { HuiLazyRender } from '@nscode/handy-ui'
import type { LazyRenderProps, LazyRenderEmits, LazyRenderExpose } from '@nscode/handy-ui'
Examples
Twenty blocks in a scroll container
The default slot is evaluated only after its block reaches the observation area. Each block displays its first render time; scrolling away and back does not change it.
<script setup lang="ts">
import { defineComponent, h, ref } from 'vue'
import { HuiLazyRender } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const rendered = ref(0)
const Block = defineComponent({
props: { index: { type: Number, required: true } },
setup(props) {
const time = new Date().toLocaleTimeString('en-GB', { hour12: false })
return () => h('div', { class: 'block' }, `Block ${props.index}, rendered at ${time}`)
}
})
</script>
<template>
<p>Rendered {{ rendered }} / 20 blocks. Scroll down:</p>
<div ref="container" class="scroll-area">
<HuiLazyRender
v-for="index in 20"
:key="index"
:root="() => container"
:height="180"
root-margin="0px"
@render="rendered++"
>
<Block :index="index" />
<template #placeholder
><div class="placeholder">Block {{ index }} has not rendered</div></template
>
</HuiLazyRender>
</div>
</template>
<style scoped>
.scroll-area {
height: 320px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.block,
.placeholder {
min-height: 180px;
display: flex;
align-items: center;
justify-content: center;
box-sizing: border-box;
padding: var(--hui-spacing-4);
border-bottom: var(--hui-border-width) solid var(--hui-border-color);
}
.block {
background: var(--hui-color-primary-bg);
}
.placeholder {
color: var(--hui-text-color-secondary);
background: var(--hui-fill-color-light);
}
</style>
Skeleton placeholder and early rendering
The default rootMargin is '200px', starting rendering 200px before the content reaches an edge. Use '0px' to wait for intersection. Choose a placeholder height close to the final content height to reduce scrolling jumps.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiLazyRender, HuiSkeleton, HuiSpace } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const loaded = ref(false)
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>{{ loaded ? 'Content rendered' : 'Scroll down; rendering begins 80px early' }}</span>
<div ref="container" class="scroll-area">
<div class="spacer">Scroll down</div>
<HuiLazyRender
:root="() => container"
:height="180"
root-margin="80px 0px"
@render="loaded = true"
>
<div class="content">Place charts, lists or other expensive content here.</div>
<template #placeholder
><div class="skeleton"><HuiSkeleton :avatar="false" :rows="4" /></div
></template>
</HuiLazyRender>
</div>
</HuiSpace>
</template>
<style scoped>
.scroll-area {
height: 260px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.spacer {
height: 420px;
padding: var(--hui-spacing-3);
box-sizing: border-box;
color: var(--hui-text-color-secondary);
}
.content,
.skeleton {
min-height: 180px;
box-sizing: border-box;
padding: var(--hui-spacing-4);
}
.content {
background: var(--hui-color-primary-bg);
}
</style>
Immediate rendering and print preparation
Call render() without waiting for intersection. Repeated calls do not emit extra events. Before printing, render all required blocks and wait for one nextTick().
<script setup lang="ts">
import { nextTick, ref } from 'vue'
import { HuiButton, HuiLazyRender, HuiMessage, HuiSpace } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const lazy = ref<InstanceType<typeof HuiLazyRender>>()
const count = ref(0)
const prepare = async () => {
lazy.value?.render()
await nextTick()
HuiMessage.success('Content mounted; printing or export can continue')
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiButton @click="lazy?.render()">render()</HuiButton
><HuiButton @click="prepare">Prepare print content</HuiButton
><span>{{ count }} render events</span></HuiSpace
>
<div ref="container" class="scroll-area">
<div class="spacer">
The block below is outside the view, but you can render it immediately.
</div>
<HuiLazyRender
ref="lazy"
:root="() => container"
root-margin="0px"
:height="160"
@render="count++"
>
<div class="content">Mounted print content.</div>
<template #placeholder><span>Waiting for intersection or the method call</span></template>
</HuiLazyRender>
</div>
</HuiSpace>
</template>
<style scoped>
.scroll-area {
height: 240px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.spacer {
height: 400px;
padding: var(--hui-spacing-3);
box-sizing: border-box;
}
.content {
min-height: 160px;
padding: var(--hui-spacing-4);
box-sizing: border-box;
background: var(--hui-color-primary-bg);
}
</style>
Selector, element and getter roots
root accepts a selector, an HTML element or a function returning an element. Getters are useful for containers created in the same template because resolution happens after mounting. Changing the root or margin before rendering recreates the observer; loaded content remains unchanged.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLazyRender, HuiSpace } from '@nscode/handy-ui'
const container = ref<HTMLElement | null>(null)
const mode = ref<'function' | 'element' | 'selector'>('function')
const root = () =>
mode.value === 'selector'
? '#lazy-render-root-demo-en'
: mode.value === 'element'
? container.value
: () => container.value
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap
><HuiButton
v-for="value in ['function', 'element', 'selector']"
:key="value"
@click="mode = value as typeof mode"
>{{ value }}</HuiButton
><span>Current: {{ mode }}</span></HuiSpace
>
<div id="lazy-render-root-demo-en" ref="container" class="scroll-area">
<div class="spacer">Scroll down, or first change how the root is passed.</div>
<HuiLazyRender :root="root() ?? undefined" :height="120" root-margin="0px"
><div class="content">All three root forms point to this container.</div>
<template #placeholder>Waiting to render</template></HuiLazyRender
>
</div>
</HuiSpace>
</template>
<style scoped>
.scroll-area {
height: 220px;
overflow: auto;
border: var(--hui-border-width) solid var(--hui-border-color);
}
.spacer {
height: 360px;
padding: var(--hui-spacing-3);
box-sizing: border-box;
}
.content {
min-height: 120px;
padding: var(--hui-spacing-4);
background: var(--hui-color-primary-bg);
box-sizing: border-box;
}
</style>
Wrapper tag and CSS placeholder height
tag changes only the outer HTML element. height accepts lengths such as '12rem'. Omitting root observes the browser viewport, so nearby content may render immediately.
<script setup lang="ts">
import { HuiLazyRender } from '@nscode/handy-ui'
</script>
<template>
<HuiLazyRender tag="section" height="12rem" root-margin="0px">
<section class="content">
<h3>Article section</h3>
<p>Mounted when it first approaches the viewport, then kept.</p>
</section>
<template #placeholder><div class="placeholder">Article placeholder</div></template>
</HuiLazyRender>
</template>
<style scoped>
.content,
.placeholder {
min-height: 12rem;
padding: var(--hui-spacing-4);
box-sizing: border-box;
background: var(--hui-fill-color-light);
}
</style>
Props
| Prop | Description | Type | Default |
|---|---|---|---|
| height | Placeholder minimum height; numbers use px | number | string | 200 |
| rootMargin | Intersection margin; positive values render earlier | string | '200px' |
| root | Scroll container; omit for the viewport | string | HTMLElement | (() => HTMLElement | null | undefined) | — |
| tag | Outer HTML element | string | 'div' |
Events
| Event | Description | Parameters |
|---|---|---|
| render | Default content has first mounted on the client; emitted once | — |
Slots
| Slot | Description | Parameters |
|---|---|---|
| default | Content evaluated and mounted only near the observation area | — |
| placeholder | Placeholder before rendering, such as a skeleton | — |
Methods
| Method | Description | Parameters |
|---|---|---|
| render | Request immediate rendering; repeat calls do not remount or emit again | — |
Types
interface LazyRenderProps {
height?: number | string
rootMargin?: string
root?: string | HTMLElement | (() => HTMLElement | null | undefined)
tag?: string
}
interface LazyRenderExpose {
render: () => void
}