Masonry 瀑布流
把不等高内容按顺序放入当前最矮的列,适合卡片墙、图片墙和动态内容。所有项目保留数据顺序与 DOM 顺序,换列不会重建内部组件。
引入
import { HuiMasonry, useBreakpoint, resolveResponsive } from '@nscode/handy-ui'
import type {
MasonryProps,
MasonryEmits,
MasonrySlots,
Breakpoint,
ResponsiveValue
} from '@nscode/handy-ui'
示例
不等高卡片与排版事件
item 插槽提供数据、原始下标和排版后的列号(从 0 开始)。layout 在实际排版变化后返回各列累积高度;非空列包含最后一项之后的垂直间距,容器高度会扣除这段间距。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMasonry, HuiSpace } from '@nscode/handy-ui'
const heights = ref<number[]>([])
const cards = [
{ id: 1, title: '项目计划', height: 80 },
{ id: 2, title: '设计稿', height: 130 },
{ id: 3, title: '测试记录', height: 100 },
{ id: 4, title: '上线清单', height: 160 },
{ id: 5, title: '操作说明', height: 90 }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiMasonry :items="cards" :columns="3" item-key="id" @layout="heights = $event">
<template #item="{ item, index, column }">
<article class="card" :style="{ minHeight: `${item.height}px` }">
<strong>{{ item.title }}</strong>
<p>第 {{ index + 1 }} 项,位于第 {{ column + 1 }} 列</p>
</article>
</template>
</HuiMasonry>
<span>各列累积高度:{{ heights.join(' / ') || '等待测量' }}</span>
</HuiSpace>
</template>
<style scoped>
.card {
padding: var(--hui-spacing-4);
background: var(--hui-fill-color-light);
border-radius: var(--hui-border-radius);
}
.card p {
margin-bottom: 0;
}
</style>
响应式列数
默认列数是 { xs: 1, sm: 2, md: 3, lg: 4 },xl 继续沿用 lg。自定义时优先取当前及更小的最近断点;只有更大的配置时取最小的已设断点。调整浏览器宽度可查看变化,断点与视口宽度关联。
<script setup lang="ts">
import { HuiMasonry } from '@nscode/handy-ui'
const cards = Array.from({ length: 9 }, (_, index) => ({
id: index,
height: 72 + (index % 4) * 28
}))
</script>
<template>
<HuiMasonry :items="cards" :columns="{ xs: 1, sm: 2, lg: 4 }" item-key="id">
<template #item="{ item }">
<div class="card" :style="{ height: `${item.height}px` }">卡片 {{ item.id + 1 }}</div>
</template>
</HuiMasonry>
</template>
<style scoped>
.card {
padding: var(--hui-spacing-3);
background: var(--hui-color-primary-bg);
border-radius: var(--hui-border-radius);
}
</style>
横纵间距
数字 gap 同时设置两个方向;数组为 [水平, 垂直],单位 px。下面对比 8 和 [24, 12]。
<script setup lang="ts">
import { HuiMasonry, HuiSpace } from '@nscode/handy-ui'
const cards = [80, 120, 60, 100]
const gaps: Array<number | [number, number]> = [8, [24, 12]]
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="(gap, index) in gaps" :key="index">
<p>gap:{{ JSON.stringify(gap) }}</p>
<HuiMasonry :items="cards" :columns="2" :gap="gap">
<template #item="{ item }"
><div class="card" :style="{ height: `${item}px` }">{{ item }}px</div></template
>
</HuiMasonry>
</div>
</HuiSpace>
</template>
<style scoped>
.card {
padding: var(--hui-spacing-3);
background: var(--hui-fill-color-light);
border-radius: var(--hui-border-radius);
}
</style>
追加项目与稳定 key
itemKey 支持字段名或函数,返回稳定唯一标识。按 key 保留内部组件状态;追加、重排时建议显式设置。未设置时使用下标。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiMasonry, HuiSpace } from '@nscode/handy-ui'
interface Card {
id: number
text: string
note: string
height: number
}
const cards = ref<Card[]>([
{ id: 1, text: '第一项', note: '', height: 90 },
{ id: 2, text: '第二项', note: '', height: 130 }
])
let seed = 2
const key = (item: Card) => item.id
const append = () => {
seed += 1
cards.value.push({ id: seed, text: `新增 ${seed}`, note: '', height: 80 + (seed % 4) * 24 })
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="append">追加卡片</HuiButton>
<HuiMasonry :items="cards" :columns="2" :item-key="key">
<template #item="{ item }">
<article class="card" :style="{ minHeight: `${item.height}px` }">
<p>{{ item.text }}</p>
<HuiInput v-model="item.note" placeholder="输入后追加卡片,内容保留" />
</article>
</template>
</HuiMasonry>
</HuiSpace>
</template>
<style scoped>
.card {
padding: var(--hui-spacing-3);
background: var(--hui-fill-color-light);
border-radius: var(--hui-border-radius);
}
</style>
图片墙与内容增高
色块代替外部图片,演示不同比例的图片墙。点击按钮改变一项高度,组件通过 ResizeObserver 自动重排;实际图片加载后尺寸变化也使用同一逻辑。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiMasonry, HuiSpace } from '@nscode/handy-ui'
const pictures = ref(
Array.from({ length: 8 }, (_, index) => ({ id: index, height: 80 + (index % 3) * 40 }))
)
const enlarge = () => {
pictures.value[0].height = pictures.value[0].height === 80 ? 200 : 80
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="enlarge">切换第一张高度</HuiButton>
<HuiMasonry :items="pictures" :columns="3" :gap="[12, 20]" item-key="id">
<template #item="{ item }">
<figure class="picture" :style="{ height: `${item.height}px` }">
<figcaption>图片 {{ item.id + 1 }}</figcaption>
</figure>
</template>
</HuiMasonry>
</HuiSpace>
</template>
<style scoped>
.picture {
margin: 0;
display: flex;
align-items: end;
padding: var(--hui-spacing-3);
background: var(--hui-color-primary-bg);
color: var(--hui-text-color);
border-radius: var(--hui-border-radius);
}
</style>
空状态
empty 插槽占据正常文档流,不被固定为零高度;未提供时不显示额外文字。
<script setup lang="ts">
import { HuiMasonry } from '@nscode/handy-ui'
const cards: string[] = []
</script>
<template>
<HuiMasonry :items="cards"
><template #empty><p class="empty">暂无卡片,稍后再来看看。</p></template></HuiMasonry
>
</template>
<style scoped>
.empty {
margin: 0;
padding: var(--hui-spacing-6);
text-align: center;
color: var(--hui-text-color-secondary);
background: var(--hui-fill-color-light);
}
</style>
断点工具
useBreakpoint() 返回只读的响应式断点。resolveResponsive() 可供其他布局按同一规则取值。所有调用共享四个媒体查询监听,最后一个实例卸载后清理。
<script setup lang="ts">
import { computed } from 'vue'
import { HuiMasonry, HuiSpace, useBreakpoint, resolveResponsive } from '@nscode/handy-ui'
const breakpoint = useBreakpoint()
const columns = computed(() => resolveResponsive({ xs: 1, md: 3, xl: 5 }, breakpoint.value, 1))
const cards = [90, 130, 80, 110, 70]
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>当前断点:{{ breakpoint }},工具解析列数:{{ columns }}</span>
<HuiMasonry :items="cards" :columns="columns">
<template #item="{ item, index }"
><div class="card" :style="{ height: `${item}px` }">第 {{ index + 1 }} 项</div></template
>
</HuiMasonry>
</HuiSpace>
</template>
<style scoped>
.card {
padding: var(--hui-spacing-3);
background: var(--hui-fill-color-light);
border-radius: var(--hui-border-radius);
}
</style>
Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| items | 按原始顺序排列的数据 | T[] | 必填 |
| columns | 列数或响应式配置;小数向下取整,非有限值或小于 1 用 1 | ResponsiveValue<number> | { xs: 1, sm: 2, md: 3, lg: 4 } |
| gap | 横纵间距,数组为 [水平, 垂直];负数或非有限值用 0 | number | [number, number] | 16 |
| itemKey | 稳定唯一字段名或函数;字段无效时使用下标 | string | ((item: T, index: number) => string | number) | 下标 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
| layout | columnHeights: number[] | 实际排版变化后触发,非空列含末尾垂直间距;空数据为 [] |
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
| item | { item: T, index: number, column: number } | 数据、原始下标、从 0 开始的列号;首次测量前列号为 0。未提供时显示数据文字 |
| empty | — | 空数据时的内容 |
方法
无组件实例方法,尺寸变化自动重新排版。
工具函数
useBreakpoint
useBreakpoint(): Readonly<Ref<Breakpoint>>
无参数,返回当前视口断点的只读 ref。必须在组件 setup 或 <script setup> 中同步调用,以便登记挂载和卸载清理。setup 中同步读取 breakpoint.value 总是 lg,SSR 也使用 lg;挂载后才更新为真实断点。请在模板、computed 或 watch 中响应式读取,不要在 setup 中取一次后当成固定值。没有 matchMedia 时保持 lg。
所有已挂载实例共享四个媒体查询监听;新实例只初始化自己,媒体查询变化才通知现有实例,最后一个实例卸载后移除监听。
resolveResponsive
resolveResponsive<T>(value: ResponsiveValue<T>, breakpoint: Breakpoint, fallback: T): T
| 参数 | 说明 |
|---|---|
| value | 单个值,或按断点配置的对象;非对象原样返回 |
| breakpoint | 当前断点,通常取 useBreakpoint().value |
| fallback | 对象没有任何已配置断点时的返回值 |
返回不大于当前断点的最近已配置值;全部断点都更大时返回最小已配置断点的值。0 等显式值不会被当作未配置。
类型
import type { Ref } from 'vue'
type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl'
type ResponsiveValue<T> = T | Partial<Record<Breakpoint, T>>
interface MasonryProps<T = unknown> {
items: T[]
columns?: ResponsiveValue<number>
gap?: number | [number, number]
itemKey?: string | ((item: T, index: number) => string | number)
}
interface MasonryEmits {
layout: [columnHeights: number[]]
}
interface MasonrySlots<T> {
item?: (props: { item: T; index: number; column: number }) => unknown
empty?: () => unknown
}
const BREAKPOINT_MIN_WIDTH: Record<Breakpoint, number> = {
xs: 0,
sm: 640,
md: 768,
lg: 1024,
xl: 1280
}
function useBreakpoint(): Readonly<Ref<Breakpoint>>
function resolveResponsive<T>(value: ResponsiveValue<T>, breakpoint: Breakpoint, fallback: T): T