Grid 網格
將一列分成 24 欄,以 HuiRow 包住一列、HuiCol 佔用指定欄數,建立依比例分欄及隨螢幕寬度變化的版面。
引入
import { HuiRow, HuiCol } from '@nscode/handy-ui'
基礎用法
HuiCol 的 span 是該欄佔用的網格欄數,每列合計 24。總和超過 24 時,多出的欄會排至下一列。
<script setup lang="ts">
import { HuiRow, HuiCol, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol :span="24"><div class="cell">24</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
<HuiCol :span="12"><div class="cell cell--light">12</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="8"><div class="cell">8</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
<HuiCol :span="8"><div class="cell">8</div></HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
.cell 僅提供背景色,方便辨識各欄位置,與網格行為無關。後續範例皆使用相同樣式。
span 可以是 0~24 之間的任意整數,同一列內的各欄可採不同寬度:
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol v-for="i in 4" :key="i" :span="6">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">6</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol v-for="i in 6" :key="i" :span="4">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">4</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol v-for="i in 8" :key="i" :span="3">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">3</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="16"><div class="cell">16</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
<HuiCol :span="16"><div class="cell cell--light">16</div></HuiCol>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="16"><div class="cell">16</div></HuiCol>
<HuiCol :span="16"
><div class="cell cell--light">16(加起來超過 24,換到下一行)</div></HuiCol
>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
欄間距
HuiRow 的 gutter 指定欄間距,單位 px。傳入 [水平, 垂直] 時,亦加入列與列之間的間距,適合換列排列。
<template>
<HuiSpace direction="vertical" fill>
<HuiRow :gutter="16">
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
</HuiRow>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 8" :key="i" :span="6"
><div class="cell">{{ i }}</div></HuiCol
>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
間距由各 HuiCol 左右的 gutter / 2 內距,以及 HuiRow 左右的 -gutter / 2 外距共同形成,因此首末欄內容仍與列邊緣對齊。背景色請設於 HuiCol 內部元素;直接設在 HuiCol 會連間距一起上色。
不同的間距放在一起對比:
<template>
<HuiSpace direction="vertical" fill>
<div v-for="gutter in [0, 8, 16, 32]" :key="gutter">
<div class="label">gutter {{ gutter }}px</div>
<HuiRow :gutter="gutter">
<HuiCol v-for="i in 4" :key="i" :span="6"><div class="cell">6</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
執行中修改 gutter,各欄的間距跟著變。拖曳滑塊分別調整水平和垂直間距:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCol, HuiRow, HuiSlider, HuiSpace } from '@nscode/handy-ui'
const horizontal = ref(16)
const vertical = ref(16)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap size="large">
<span>水平 {{ horizontal }}px</span>
<HuiSlider
v-model="horizontal"
:min="0"
:max="48"
:step="8"
show-stops
style="width: 200px"
/>
<span>垂直 {{ vertical }}px</span>
<HuiSlider v-model="vertical" :min="0" :max="48" :step="8" show-stops style="width: 200px" />
</HuiSpace>
<HuiRow :gutter="[horizontal, vertical]">
<HuiCol v-for="i in 8" :key="i" :span="6">
<div class="cell">{{ i }}</div>
</HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
欄偏移
offset 在欄左側留出指定欄數,使其向右偏移。與 span 相同,接受 0~24。
<template>
<HuiSpace direction="vertical" fill>
<HuiRow>
<HuiCol :span="6"><div class="cell">6</div></HuiCol>
<HuiCol :span="6" :offset="6"><div class="cell">6,偏移 6</div></HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="12" :offset="6"><div class="cell">12,偏移 6(居中)</div></HuiCol>
</HuiRow>
<HuiRow v-for="offset in [0, 4, 8, 12, 16, 20]" :key="offset">
<HuiCol :span="4" :offset="offset">
<div class="cell cell--light">偏移 {{ offset }}</div>
</HuiCol>
</HuiRow>
<HuiRow>
<HuiCol :span="6" :offset="2"><div class="cell">6,偏移 2</div></HuiCol>
<HuiCol :span="6" :offset="2"><div class="cell">6,偏移 2</div></HuiCol>
<HuiCol :span="6" :offset="2"><div class="cell">6,偏移 2</div></HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
調換順序
push 向右、pull 向左移動,單位為網格欄數。只改變顯示位置,不影響其他欄,可在不更動範本順序的情況下交換位置。
<template>
<HuiRow>
<HuiCol :span="18" :push="6"><div class="cell">在範本裡排第一,顯示在右邊</div></HuiCol>
<HuiCol :span="6" :pull="18"
><div class="cell cell--light">在範本裡排第二,顯示在左邊</div></HuiCol
>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
水平排列
HuiRow 的 justify 控制同列各欄的水平分布。可選 start(預設)、end、center、space-around、space-between、space-evenly;合計欄數小於 24、仍有剩餘空間時才能看出差異。
<script setup lang="ts">
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
import type { RowJustify } from '@nscode/handy-ui'
const justifies: RowJustify[] = [
'start',
'end',
'center',
'space-around',
'space-between',
'space-evenly'
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="item in justifies" :key="item">
<div class="label">{{ item }}</div>
<HuiRow :justify="item" class="row">
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
<HuiCol :span="4"><div class="cell cell--light">4</div></HuiCol>
<HuiCol :span="4"><div class="cell">4</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.row {
outline: 1px dashed var(--hui-border-color);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-dark);
}
</style>
垂直對齊
HuiRow 的 align 控制垂直對齊,可選 top(預設)、middle、bottom、stretch。stretch 會使各欄拉伸至該列最高欄的高度。
<template>
<HuiRow align="middle" :gutter="16">
<HuiCol :span="8"><div class="cell" style="height: 80px">高 80px</div></HuiCol>
<HuiCol :span="8"><div class="cell">垂直居中</div></HuiCol>
<HuiCol :span="8"><div class="cell" style="height: 50px">高 50px</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
四個值放在一起對比。每列左欄高 80px,中欄未指定高度(拉伸時隨列高改變),右欄高 50px:
<script setup lang="ts">
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
import type { RowAlign } from '@nscode/handy-ui'
const aligns: RowAlign[] = ['top', 'middle', 'bottom', 'stretch']
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="item in aligns" :key="item">
<div class="label">{{ item }}</div>
<HuiRow :align="item" :gutter="16">
<HuiCol :span="8"><div class="cell" style="height: 80px">高 80px</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--fit">不設高度</div></HuiCol>
<HuiCol :span="8"><div class="cell" style="height: 50px">高 50px</div></HuiCol>
</HuiRow>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
display: flex;
align-items: center;
justify-content: center;
min-height: 36px;
box-sizing: border-box;
background: var(--hui-fill-color);
}
/* 列被拉伸時,高度 100% 才能跟著列變高 */
.cell--fit {
height: 100%;
background: var(--hui-fill-color-dark);
}
</style>
不換行
HuiRow 預設允許換列。wrap=false 時,所有欄保持在同一列且不縮窄;span 合計超過 24 的部分會溢出。
<template>
<HuiRow :wrap="false">
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
<HuiCol :span="12"><div class="cell cell--light">12</div></HuiCol>
<HuiCol :span="12"><div class="cell">12</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
相同的三個 span=12 欄位,預設換行和不換行對比(下面給行外面套了水平捲動的容器,看得到超出的部分):
<template>
<HuiSpace direction="vertical" fill>
<div>
<div class="label">wrap(預設):第三列換到下一行</div>
<HuiRow>
<HuiCol v-for="i in 3" :key="i" :span="12">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">12</div>
</HuiCol>
</HuiRow>
</div>
<div>
<div class="label">:wrap="false":第三列超出此列</div>
<div style="overflow-x: auto">
<HuiRow :wrap="false">
<HuiCol v-for="i in 3" :key="i" :span="12">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">12</div>
</HuiCol>
</HuiRow>
</div>
</div>
</HuiSpace>
</template>
<style scoped>
.label {
margin-bottom: 4px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
寬度隨內容與隱藏欄
未指定 span 時,欄寬隨內容;指定 span=0 時隱藏該欄。
<template>
<HuiRow :gutter="8">
<HuiCol><div class="cell" style="padding: 0 12px">寬度隨內容</div></HuiCol>
<HuiCol :span="0"><div class="cell">不顯示</div></HuiCol>
<HuiCol :span="8"><div class="cell cell--light">8</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
響應式版面配置
xs、sm、md、lg、xl 依螢幕寬度設定欄寬。可傳數值(等同 span),或 { span, offset, push, pull } 物件。各斷點的有效範圍如下:
| 斷點 | 生效範圍 |
|---|---|
| xs | <640px,只在這個範圍內 |
| sm | ≥640px 起生效,向上繼承 |
| md | ≥768px 起生效,向上繼承 |
| lg | ≥1024px 起生效,向上繼承 |
| xl | ≥1280px 起生效,向上繼承 |
「向上繼承」表示斷點設定在較寬螢幕持續有效,直到由更大的斷點覆蓋。不帶斷點的 span、offset 等屬性作用於所有寬度,再由斷點值覆蓋。
下例在小於 768px 時每欄佔滿一列;768px 起每列兩欄;1280px 起每列四欄。
<template>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 4" :key="i" :span="24" :md="12" :xl="6">
<div class="cell">{{ i }}</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
五個斷點都寫上時,每個寬度範圍各有自己的欄寬。下面顯示目前視窗寬度和生效的斷點,拖曳瀏覽器視窗改變寬度看效果:
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { HuiCol, HuiRow, HuiSpace } from '@nscode/handy-ui'
const width = ref(0)
const updateWidth = () => {
width.value = window.innerWidth
}
onMounted(() => {
updateWidth()
window.addEventListener('resize', updateWidth)
})
onBeforeUnmount(() => window.removeEventListener('resize', updateWidth))
const current = computed(() => {
if (width.value < 640) return 'xs(每欄 24,一行 1 列)'
if (width.value < 768) return 'sm(每欄 12,一行 2 列)'
if (width.value < 1024) return 'md(每欄 8,一行 3 列)'
if (width.value < 1280) return 'lg(每欄 6,一行 4 列)'
return 'xl(每欄 4,一行 6 列)'
})
</script>
<template>
<HuiSpace direction="vertical" fill>
<span>視窗寬度 {{ width }}px,生效的斷點是 {{ current }}</span>
<HuiRow :gutter="[16, 16]">
<HuiCol v-for="i in 6" :key="i" :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
<div :class="['cell', { 'cell--light': i % 2 === 0 }]">{{ i }}</div>
</HuiCol>
</HuiRow>
</HuiSpace>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
物件形式可同時調整偏移。下例在 1024px 以下佔滿一列,1024px 起占 12 欄並置中。
<template>
<HuiRow>
<HuiCol :span="24" :lg="{ span: 12, offset: 6 }">
<div class="cell">響應式偏移</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
物件內的 push、pull 可只在寬螢幕改變順序。下例在 1024px 以下主內容在上、側欄在下,1024px 起將側欄移至左側。
<template>
<HuiRow :gutter="[16, 16]">
<HuiCol :span="24" :lg="{ span: 16, push: 8 }">
<div class="cell">主內容(範本裡排第一)</div>
</HuiCol>
<HuiCol :span="24" :lg="{ span: 8, pull: 16 }">
<div class="cell cell--light">側欄(範本裡排第二)</div>
</HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
某個斷點設成 0 可以在這個寬度及以上隱藏此欄,更大的斷點設成非零值又會顯示出來:
<template>
<HuiRow :gutter="16">
<HuiCol :span="6" :md="0" :xl="6"><div class="cell">768~1279px 隱藏</div></HuiCol>
<HuiCol :span="18" :md="24" :xl="18"><div class="cell cell--light">主內容</div></HuiCol>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
.cell--light {
background: var(--hui-fill-color-light);
}
</style>
自訂標籤
HuiRow、HuiCol 預設渲染成 div,tag 可以換成別的元素,例如清單結構用 ul 和 li。
<template>
<HuiRow tag="ul" :gutter="16" style="padding: 0; list-style: none">
<HuiCol v-for="i in 3" :key="i" tag="li" :span="8"
><div class="cell">{{ i }}</div></HuiCol
>
</HuiRow>
</template>
<style scoped>
.cell {
height: 36px;
line-height: 36px;
text-align: center;
background: var(--hui-fill-color);
}
</style>
HuiRow Props
| 屬性 | 型別 | 預設值 | 說明 |
|---|---|---|---|
| gutter | number | [number, number] | 0 | 欄間距,單位 px;陣列為 [水平, 垂直],垂直間距作用於列與列之間 |
| justify | 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly' | 'start' | 各欄的水平分布 |
| align | 'top' | 'middle' | 'bottom' | 'stretch' | 'top' | 各欄的垂直對齊 |
| wrap | boolean | true | 放不下時是否換行 |
| tag | string | 'div' | 渲染成的元素 |
HuiCol Props
| 屬性 | 型別 | 預設值 | 說明 |
|---|---|---|---|
| span | number | - | 占用的欄數,0~24。不填時寬度等於內容寬度;為 0 時隱藏 |
| offset | number | - | 左側空出的欄數 |
| push | number | - | 向右移動的欄數,不影響其他欄的位置 |
| pull | number | - | 向左移動的欄數,不影響其他欄的位置 |
| xs | number | ColResponsiveProps | - | 螢幕寬度 <640px 時的設定,只在這個範圍內生效。數字等同於 span |
| sm | number | ColResponsiveProps | - | ≥640px 起生效,向上繼承。數字等同於 span |
| md | number | ColResponsiveProps | - | ≥768px 起生效,向上繼承。數字等同於 span |
| lg | number | ColResponsiveProps | - | ≥1024px 起生效,向上繼承。數字等同於 span |
| xl | number | ColResponsiveProps | - | ≥1280px 起生效,向上繼承。數字等同於 span |
| tag | string | 'div' | 渲染成的元素 |
HuiRow Slots
| 插槽 | 作用域參數 | 說明 |
|---|---|---|
| default | - | 放 HuiCol |
HuiCol Slots
| 插槽 | 作用域參數 | 說明 |
|---|---|---|
| default | - | 欄的內容 |
型別
import type { RowProps, ColProps, ColResponsiveProps, RowJustify, RowAlign } from '@nscode/handy-ui'
type RowJustify = 'start' | 'end' | 'center' | 'space-around' | 'space-between' | 'space-evenly'
type RowAlign = 'top' | 'middle' | 'bottom' | 'stretch'
interface RowProps {
gutter?: number | [number, number]
justify?: RowJustify
align?: RowAlign
wrap?: boolean
tag?: string
}
/** 斷點屬性寫成物件時的格式 */
interface ColResponsiveProps {
span?: number
offset?: number
push?: number
pull?: number
}
interface ColProps extends ColResponsiveProps {
xs?: number | ColResponsiveProps
sm?: number | ColResponsiveProps
md?: number | ColResponsiveProps
lg?: number | ColResponsiveProps
xl?: number | ColResponsiveProps
tag?: string
}
另外還匯出了 RowContext 和注入用的 ROW_INJECTION_KEY,HuiCol 通過它拿到所在列的 gutter,一般用不到。