Waterfall
Places items into the shortest estimated column; appends preserve prior positions, while listKey resets layout.
Import
Use <hui-waterfall> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-space direction="vertical" fill>
<hui-waterfall
custom-class="hui-space__item"
:items="items"
item-key="id"
:list-key="version"
:estimate-height="estimateHeight"
@layout="(value) => (lastEvent = JSON.stringify(value))"
>
<template #item="{ index, column }"
><hui-card :title="items[index].title"
><text>{{ column }} / {{ items[index].height }}</text></hui-card
></template
>
</hui-waterfall>
<hui-button custom-class="hui-space__item" @click="append">追加 / Append</hui-button
><hui-button custom-class="hui-space__item" @click="reset"
>更换列表 / Replace list</hui-button
>
<text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const version = ref(0)
const items = ref([
{ id: 1, title: '甲 / A', height: 140 },
{ id: 2, title: '乙 / B', height: 90 }
])
const estimateHeight = (item: unknown) => (item as { height: number }).height
const append = () => {
const id = items.value.length + 1
items.value.push({ id, title: '条目 / Item ' + id, height: 110 })
}
const reset = () => {
version.value++
items.value = [{ id: 1, title: '新列表 / New list', height: 180 }]
}
</script>
Complete content feed
Combines carousel preview, image fallback and retry, stable waterfall appends, filter resets, full text, article content, a bottom action bar and platform sharing. Call append from the page bottom handler in the application.
<template>
<view>
<hui-space direction="vertical" fill>
<hui-page-host custom-class="hui-space__item" />
<hui-swiper
custom-class="hui-space__item"
:items="banners"
:height="160"
indicator-type="fraction"
preview
:autoplay="false"
/>
<hui-button custom-class="hui-space__item" @click="filter"
>切换筛选 / Change filter</hui-button
>
<hui-waterfall
custom-class="hui-space__item"
:items="cards"
item-key="id"
:list-key="listKey"
:estimate-height="estimateHeight"
>
<template #item="{ index }">
<hui-card :title="cards[index].title">
<hui-image
:src="cards[index].failed ? '/missing-cover.png' : cards[index].src"
:fallback-src="cards[index].src"
width="100%"
:ratio="cards[index].ratio"
>
<template #error="{ reload }"
><hui-button @click.stop="reload">重试 / Retry</hui-button></template
>
</hui-image>
<hui-show-more :text="cards[index].text" :rows="2" />
</hui-card>
</template>
</hui-waterfall>
<hui-button custom-class="hui-space__item" @click="append"
>触底后追加 / Append next page</hui-button
>
<hui-rich-text
custom-class="hui-space__item"
content="<p><strong>文章正文 / Article</strong></p><p>图片按宽高预估布局,追加不移动旧项。 / Estimate from image ratios; appending keeps existing cards in place.</p>"
/>
<hui-action-bar
custom-class="hui-space__item"
:fixed="false"
:icons="[{ icon: 'share', text: '分享 / Share' }]"
@icon-click="shareVisible = true"
/>
<hui-share-sheet
v-model:visible="shareVisible"
custom-class="hui-space__item"
:options="shareOptions"
:payload="payload"
/>
<text class="hui-space__item">{{ lastEvent }}</text>
</hui-space>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const banners = [
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB/0lEQVR4nM3QVUIUABAA0DmEgCDYgoCBqEgoiIiBSkp3d9d2dy/d3d3d3SWooBJ2YAL/XmPeER4ccyaoupKOu1M0PGma3oyTvqzT/pyzgbzzwQLtUNHFcIlepOxStOJKbJpBfIZhYtaN5Byj1DxjYoEpueg2tcScXnaXWXGPXXWfW/OAX/dI2GAjbnoqbbGVt9krOxzTu55n9rhk97nlDnjkD3kVjvgUj/mVTgSUTwVVzoRUz4XVLkTUL0U1rsQ0r8W1rie0vwQVF6KaG1ndg3rCi67lwzzlxz4TwD0XxL8QItQJE+tGSPWj5JdjlFfj0q8lZF5Pyr6ZknuLkG9CKjSjFN+hlVowyi1ZlVacamte7UNB/WNR4xNJ8zNZq52i3SGt0ymj2zmr1zWn3z1v0LNg2Lto1Ldk3L9sMrBiOrhqNrRmPrxuMbJhObppNbblRXzbRmLHK0DVk9T5GlD1JHdtAqqelO4tQNWT2vMGUPUQet8Cqh5i3ztA1UPq3wZUPeSBHUDVQxncBVQ91KE9QNVDG34PqHroIx8AVQ9j9COg6mGOfQJUPazxz4Cqhz3xBVD1cCa/Aqoe7tQ3QNXDm/4OqHr4Mz8AVY9gdh9Q9QjnfgKqHtH8L0DVI174Dah6JIt/AFWPdOkvoOqRLf8DVD3ylQNA1aNYPQRUPcq1o/83VquIElJ47QAAAABJRU5ErkJggg==',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB30lEQVR4nM3QA2IcABAAwP1Qbdt2G+Ns27Zt5+5i27aTGj/pN3aeMFBzglB3itRwhtJ0jtZygUG4xCJd4VCu8Wg3BIxbItYdCeeejPdAIXikEj3RSJ7pZC8Milcm1RuL5p1N98Fh+OQyffFYan22+oCjMeRqjnhaYz5iIkBOhaiZCD0XYxYS7FKKW87wqzlhR0HcVZL2lOV9VeVAh3qoSzvSox/rM04MmKeGrDMj9jmoPUmsP01uPEttPk9vvcgkXmaTr3Kp1/n0m0LmbTH7rpR7X85/qBQ+VoufaqXP9fKXRuVrs/qtVfverv/oNH52m2u81jq/vSHobAq7W6JeQtxPSgYp6TAtG2Xk46xiktOW5lWygva8qLMo6W6T9VYU/e2qwU7NcLdutNcw3m+aHLRMD9tmRx3zgKpnzLkAqHrGXYuAqmfCvQSoeiY9y4CqZ8q7Aqh6pn2rgKpnxr8GqHpmA+uAqmcuuAGoeuZDm4CqZyG8Bah6FiPbgKpnKboDqHqWY7uAqmclvgeoelYT+4CqZy15AKh61lOHgKpnI30EqHo2M8eAqmcr+xVQ9WznvgGqnp38d0DVs1v4Aah69oo/AVXPfukXoOo5aPsNqHoOy38AVc9R5S+g6jmu/vsPM1TniEVFeWsAAAAASUVORK5CYII='
]
const listKey = ref(0)
const shareVisible = ref(false)
const shareOptions: ('wechat' | 'timeline' | 'link')[] = ['wechat', 'timeline', 'link']
const payload = {
title: '发现 / Discover',
url: 'https://example.com/discover',
path: '/pages/discover/index'
}
const make = (id: number) => ({
id,
title: '卡片 / Card ' + id,
src: banners[id % 2],
ratio: id % 2 ? 1.5 : 1,
failed: id === 2,
text: '完整长文本 / Full text. '.repeat(12)
})
const cards = ref([make(1), make(2), make(3)])
const estimateHeight = (item: unknown, context: { columnWidth: number; index: number }) =>
context.columnWidth / (item as { ratio: number }).ratio + 130
const append = () => {
const next = cards.value.length + 1
cards.value.push(make(next), make(next + 1))
lastEvent.value = '追加两项 / Two appended'
}
const filter = () => {
listKey.value++
cards.value = [make(1), make(2)]
lastEvent.value = '重新估高 / Layout reset'
}
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
items | unknown[] | [] | Data items rendered in source order. |
columns | number | 2 | Positive number of columns. |
gap | number | [number, number] | 8 | Spacing in px; Waterfall accepts horizontal and vertical values. |
itemKey | string | ((item: unknown, index: number) => string | number) | — | Stable object field or key getter; falls back to id, then index. |
listKey | string | number | — | List-generation key; change it when replacing or filtering data. |
estimateHeight | (item: unknown, context: WaterfallEstimateContext) => number | — | Returns an estimated item height from the item, columnWidth and index. |
layout | WaterfallLayout | 'estimate' | Initial implementation supports estimate only. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
layout | (columnHeights: number[]) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
item | { item, index, column } |
empty | — |
Methods
| Method | Signature | Description |
|---|---|---|
relayout | (): void | Re-estimate and place every item. |
Types
WaterfallLayout、WaterfallEstimateContext、WaterfallProps、WaterfallEmits、WaterfallExpose.
Platform notes
Only estimate layout is implemented. Appending with the same listKey and key prefix preserves old columns. Change listKey for filtering, replacement or reordering; call relayout after changing existing heights or estimation rules. The estimator is read during layout; replacing an inline function does not trigger layout. It receives measured column width; resizing recomputes placement. Invalid or throwing estimates use a fallback.
Device smoke checklist.