Space 间距布局
排列一组元素,支持横纵间距、换行、对齐和撑满。基线 iOS 13 使用外边距方案,每个子项必须加 hui-space__item。推荐用独立 view 包裹子项,尤其是嵌套 Space 或带自己宽度的组件;不要依赖小程序的跨组件选择器。
引入
uni-app 配置 easycom 后使用 hui-space;手动引入路径为 @nscode/handy-uni/components/hui-space/hui-space.vue。普通 Vue 3 使用 @nscode/handy-uni/web 预览插件,或从该入口具名导入 HuiSpace。
示例
尺寸档位
small、default、large 分别读取主题间距变量,缺省为 8、16、24px。
vue
<script setup lang="ts">
import type { ComponentSize } from '@nscode/handy-uni'
const sizes: ComponentSize[] = ['small', 'default', 'large']
</script>
<template>
<view>
<view v-for="size in sizes" :key="size" style="margin-bottom: 20px">
<text>{{ size }}</text>
<view>
<hui-space :size="size">
<view class="hui-space__item"><button>第一项</button></view>
<view class="hui-space__item"><button>第二项</button></view>
</hui-space>
</view>
</view>
</view>
</template>
数值、双向间距与换行
数组顺序为 [横向, 纵向]。点击按钮切换等间距和双向间距、换行开关。子项隐藏后无需保留占位元素。
vue
<script setup lang="ts">
import { ref } from 'vue'
const paired = ref(true)
const wrap = ref(true)
</script>
<template>
<view>
<button @click="paired = !paired">{{ paired ? '[12, 24]' : '8px' }}</button>
<button @click="wrap = !wrap">wrap:{{ wrap }}</button>
<hui-space :size="paired ? [12, 24] : 8" :wrap="wrap" justify="start">
<view v-for="index in 5" :key="index" class="hui-space__item" style="width: 100px">
<button>项目 {{ index }}</button>
</view>
</hui-space>
</view>
</template>
方向与撑满
fill 横向平分剩余宽度,纵向使子项宽度为 100%。嵌套 Space 时,将内层放到独立的 hui-space__item 包裹中。
vue
<script setup lang="ts">
import { ref } from 'vue'
const vertical = ref(true)
const fill = ref(true)
</script>
<template>
<view>
<button @click="vertical = !vertical">{{ vertical ? 'vertical' : 'horizontal' }}</button>
<button @click="fill = !fill">fill:{{ fill }}</button>
<hui-space :direction="vertical ? 'vertical' : 'horizontal'" :fill="fill" :size="12">
<view class="hui-space__item"><button>短项</button></view>
<view class="hui-space__item"><button>较长的一项</button></view>
</hui-space>
</view>
</template>
对齐与分布
对比全部交叉轴对齐与主轴分布取值。justify 使容器占满父宽;纵向的主轴分布需要使用方设置高度。
vue
<script setup lang="ts">
import type { SpaceAlign, SpaceJustify } from '@nscode/handy-uni'
const aligns: SpaceAlign[] = ['start', 'center', 'end', 'baseline', 'stretch']
const justifies: SpaceJustify[] = ['start', 'center', 'end', 'between', 'around']
</script>
<template>
<view>
<view v-for="align in aligns" :key="align" style="margin-bottom: 20px">
<text>align:{{ align }}</text>
<view>
<hui-space :align="align">
<view class="hui-space__item" style="font-size: 12px">小字</view>
<view class="hui-space__item" style="font-size: 24px">大字</view>
</hui-space>
</view>
</view>
<view v-for="justify in justifies" :key="justify" style="margin-bottom: 20px">
<text>justify:{{ justify }}</text>
<hui-space :justify="justify">
<view class="hui-space__item">甲</view>
<view class="hui-space__item">乙</view>
<view class="hui-space__item">丙</view>
</hui-space>
</view>
</view>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
direction | 'horizontal' | 'vertical' | 'horizontal' | 排列方向 |
size | ComponentSize | number | [number, number] | 'small' | 间距档位、非负像素数、[横向, 纵向];数组长度错误时两个方向均取首项并警告,非法数值回退 small |
wrap | boolean | false | 横向换行 |
align | 'start' | 'center' | 'end' | 'baseline' | 'stretch' | — | 交叉轴对齐;横向默认 center,纵向默认 start |
justify | 'start' | 'center' | 'end' | 'between' | 'around' | — | 主轴分布,指定后容器占满父宽 |
fill | boolean | false | 横向平分子项,纵向子项宽度撑满 |
根节点支持通用属性。
Events
无。
Slots
| 插槽 | 说明 |
|---|---|
default | 子项;每项加 hui-space__item,组件不遍历或重建子项 |
方法
无。
类型
从 @nscode/handy-uni 导入 SpaceProps、SpaceDirection、SpaceSize、SpaceAlign、SpaceJustify、ComponentSize。
CSS 变量
| 变量 | 说明 | 默认值 |
|---|---|---|
--hui-space-column-gap | 横向间距;纵向排列不使用 | 由 size 决定 |
--hui-space-row-gap | 纵向间距 | 由 size 决定 |
可在组件自身或祖先覆盖公开变量。组件不重定义公开变量;嵌套布局也继承公开覆盖。
平台说明
| 行为 | uni-app H5 | 微信小程序 | App-vue | 普通 Vue 3 Web(预览) |
|---|---|---|---|---|
| 间距 | 外边距 | 外边距,无跨组件通配选择器 | 外边距 | 外边距 |
| 子项 | 显式加 hui-space__item | 同左,组件节点建议使用独立 view 包裹 | 同左 | 同左 |
| 嵌套 | 内层用独立子项包裹 | 同左 | 同左 | 同左 |
操作步骤见Space 冒烟清单。