Space
Arrange items with horizontal and vertical spacing, wrapping and alignment. The iOS 13 baseline uses margins. Add hui-space__item to every item. Use a separate view wrapper around nested Space components and components with their own width.
Import
Use hui-space with uni-app easycom. The manual import path is @nscode/handy-uni/components/hui-space/hui-space.vue. In plain Vue 3, install the @nscode/handy-uni/web preview plugin or import HuiSpace from that entry.
Examples
Preset sizes
The small, default and large presets use theme spacing tokens, falling back to 8, 16 and 24px.
<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>First</button></view>
<view class="hui-space__item"><button>Second</button></view>
</hui-space>
</view>
</view>
</view>
</template>
Numeric spacing and wrapping
The tuple order is [horizontal, vertical]. Toggle uniform or paired spacing and wrapping.
<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>Item {{ index }}</button>
</view>
</hui-space>
</view>
</template>
Direction and fill
Horizontal fill shares the available width between items. Vertical fill gives every item full width. Wrap nested Space components in separate item elements.
<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>Short</button></view>
<view class="hui-space__item"><button>A longer item</button></view>
</hui-space>
</view>
</template>
Alignment and distribution
Compare every alignment and distribution option. Setting justify makes the container full width. Vertical distribution requires an explicit height.
<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">Small</view>
<view class="hui-space__item" style="font-size: 24px">Large</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">A</view>
<view class="hui-space__item">B</view>
<view class="hui-space__item">C</view>
</hui-space>
</view>
</view>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
direction | 'horizontal' | 'vertical' | 'horizontal' | Layout direction |
size | ComponentSize | number | [number, number] | 'small' | Preset, nonnegative pixels or [horizontal, vertical]. Invalid tuple lengths use the first value for both axes and warn. Invalid numbers fall back to small |
wrap | boolean | false | Wrap horizontal items |
align | 'start' | 'center' | 'end' | 'baseline' | 'stretch' | — | Cross-axis alignment; defaults to center horizontally and start vertically |
justify | 'start' | 'center' | 'end' | 'between' | 'around' | — | Main-axis distribution; makes the container full width |
fill | boolean | false | Share horizontal width or stretch vertical items |
Supports common props.
Events
None.
Slots
| Slot | Description |
|---|---|
default | Items with hui-space__item; item nodes are not traversed or rebuilt |
Methods
None.
Types
Import SpaceProps, SpaceDirection, SpaceSize, SpaceAlign, SpaceJustify and ComponentSize from @nscode/handy-uni.
CSS variables
| Variable | Description | Default |
|---|---|---|
--hui-space-column-gap | Horizontal spacing; unused for vertical layouts | Derived from size |
--hui-space-row-gap | Vertical spacing | Derived from size |
Override variables on the component or an ancestor. Nested layouts inherit public overrides.
Platform notes
| Behavior | uni-app H5 | WeChat Mini Programs | App-vue | Plain Vue 3 Web (preview) |
|---|---|---|---|---|
| Spacing | Margins | Margins, no cross-component wildcard selectors | Margins | Margins |
| Items | Explicit hui-space__item | Same; use separate view wrappers for components | Same | Same |
| Nested layouts | Separate item wrapper | Same | Same | Same |
See the Space smoke checklist.