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 亦同;掛載後才更新為實際斷點。請在範本、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