Empty
Use an illustration and a short message when a list, table, or search has no data. Optional actions can appear below the message.
Import
import { HuiEmpty } from '@nscode/handy-ui'
Basic Usage
Without props, the component shows its built-in illustration and the locale's “No data” message.
<script setup lang="ts">
import { HuiEmpty } from '@nscode/handy-ui'
</script>
<template>
<HuiEmpty />
</template>
Custom Description
Set description to change the message. An empty string hides the description without leaving space below the image.
<script setup lang="ts">
import { HuiEmpty } from '@nscode/handy-ui'
</script>
<template>
<div class="grid">
<HuiEmpty description="No matching content" />
<HuiEmpty description="" />
<HuiEmpty
description="No records match these filters. Try a wider time range or clear the keywords."
/>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: var(--hui-spacing-4);
}
</style>
Image Size
The image area defaults to 160×100 pixels. A numeric image-size sets equal width and height; { width, height } sets them separately, in pixels. Both the built-in illustration and an image URL scale proportionally to fit this area.
<script setup lang="ts">
import { HuiEmpty } from '@nscode/handy-ui'
</script>
<template>
<div class="grid">
<HuiEmpty :image-size="60" description="Small (60)" />
<HuiEmpty description="Default (160×100)" />
<HuiEmpty :image-size="{ width: 240, height: 80 }" description="Custom dimensions (240×80)" />
<HuiEmpty
image="/images/empty.png"
:image-size="80"
description="Also applies to custom images"
/>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
gap: var(--hui-spacing-4);
}
</style>
Custom Image
Pass an image URL to image to replace the built-in illustration.
<script setup lang="ts">
import { HuiEmpty } from '@nscode/handy-ui'
</script>
<template>
<HuiEmpty image="/images/empty.png" description="Using a custom image" />
</template>
Image Slot
Use the #image slot for an icon or custom content. It replaces the entire image area and takes precedence over image.
<script setup lang="ts">
import { HuiEmpty, HuiIcon } from '@nscode/handy-ui'
</script>
<template>
<HuiEmpty description="No search results">
<template #image>
<HuiIcon name="search" style="font-size: 64px" />
</template>
</HuiEmpty>
</template>
Choose different icons and colors for different situations:
<script setup lang="ts">
import { HuiEmpty, HuiIcon } from '@nscode/handy-ui'
</script>
<template>
<div class="grid">
<HuiEmpty description="This folder is empty">
<template #image>
<HuiIcon name="folder" style="font-size: 56px; color: var(--hui-color-warning)" />
</template>
</HuiEmpty>
<HuiEmpty description="No new messages">
<template #image>
<HuiIcon name="mail" style="font-size: 56px; color: var(--hui-color-primary)" />
</template>
</HuiEmpty>
<HuiEmpty description="No pending notifications">
<template #image>
<HuiIcon name="bell" style="font-size: 56px; color: var(--hui-text-color-placeholder)" />
</template>
</HuiEmpty>
<HuiEmpty description="All done">
<template #image>
<HuiIcon name="success" style="font-size: 56px; color: var(--hui-color-success)" />
</template>
</HuiEmpty>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: var(--hui-spacing-4);
}
</style>
Bottom Actions
The default slot appears below the description, usually with one or two action buttons.
<script setup lang="ts">
import { HuiEmpty, HuiButton, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<div class="grid">
<HuiEmpty description="No data">
<HuiButton type="primary">Add data</HuiButton>
</HuiEmpty>
<HuiEmpty description="No projects yet">
<HuiSpace>
<HuiButton type="primary" icon="plus">New project</HuiButton>
<HuiButton icon="upload">Import</HuiButton>
</HuiSpace>
</HuiEmpty>
<HuiEmpty description="Unable to load. Check your connection.">
<HuiButton variant="link" type="primary" icon="refresh">Reload</HuiButton>
</HuiEmpty>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: var(--hui-spacing-4);
}
</style>
Replacing an Empty List
Replace the list with HuiEmpty when no tasks remain. “Clear” first confirms the total task count and clears the list only after confirmation. Cancel, close, or Escape preserves it. Reset restores the initial tasks while retaining added tasks; it never silently removes current data. Resetting, adding or editing tasks, changing read-only mode, or leaving the example invalidates a pending confirmation.
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import { HuiButton, HuiCard, HuiEmpty, HuiMessageBox, HuiSpace } from '@nscode/handy-ui'
// Clear the example task list only after confirmation.
const all = ['需求评审', '接口联调', '发布上线']
const todos = ref([...all])
const readonly = ref(false)
const pending = ref(false)
let alive = true
let version = 0
let nextId = 1
watch([todos, readonly], () => version++, { deep: true, flush: 'sync' })
onBeforeUnmount(() => {
alive = false
version++
})
const clearTodos = async () => {
if (!alive || readonly.value || pending.value || !todos.value.length) return
const collection = todos.value
const snapshot = collection.slice()
const request = version
pending.value = true
try {
await HuiMessageBox.confirm(
`Clear all ${snapshot.length} tasks (${snapshot.join('、')})? This only clears this example's list; other data is unchanged.`,
'Confirm clearing tasks',
{ type: 'warning' }
)
if (
!alive ||
readonly.value ||
version !== request ||
todos.value !== collection ||
collection.length !== snapshot.length ||
collection.some((item, index) => item !== snapshot[index])
)
return
todos.value = []
} catch {
// Cancel, close, and Escape preserve the original list.
} finally {
if (alive) pending.value = false
}
}
const reset = () => {
if (!alive || readonly.value) return
todos.value = [...all, ...todos.value.filter((item) => !all.includes(item))]
}
const add = () => {
if (!alive || readonly.value) return
todos.value = [...todos.value, `新增待办${nextId++}`]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<label><input v-model="readonly" type="checkbox" /> Read-only</label>
<HuiButton size="small" :disabled="readonly" @click="add">Add task</HuiButton>
<HuiButton size="small" :disabled="readonly" @click="reset">Reset example</HuiButton>
</HuiSpace>
<HuiCard title="Tasks" style="max-width: 360px">
<template #header-extra>
<HuiButton size="small" :disabled="readonly || pending || !todos.length" @click="clearTodos"
>Clear</HuiButton
>
</template>
<ul v-if="todos.length" style="margin: 0; padding-left: 20px">
<li v-for="item in todos" :key="item">{{ item }}</li>
</ul>
<HuiEmpty v-else :image-size="60" description="No tasks">
<HuiButton size="small" type="primary" :disabled="readonly" @click="reset"
>Restore sample data</HuiButton
>
</HuiEmpty>
</HuiCard>
<span>{{
pending
? 'Waiting for confirmation; tasks have not been cleared'
: `Current tasks: ${todos.length}`
}}</span>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| image | string | — | Custom image URL. Shows the built-in illustration when omitted. |
| imageSize | number | { width: number; height: number } | — | Image area size in pixels. A number sets equal width and height; an object sets each dimension separately. Defaults to 160×100 when omitted. |
| description | string | Follows the locale (“No data”) | Description text. An empty string hides it. |
Slots
| Slot | Description |
|---|---|
| image | Custom content for the image area, replacing both image and the built-in illustration. |
| default | Bottom content below the description, usually action buttons. |
Types
import type { EmptyImageSize, EmptyProps, EmptySlots } from '@nscode/handy-ui'
type EmptyImageSize = number | { width: number; height: number }
interface EmptyProps {
image?: string
imageSize?: EmptyImageSize
description?: string
}
interface EmptySlots {
image?: () => unknown
default?: () => unknown
}