Skeleton
Show gray placeholder shapes where content will appear while data loads, then replace them with the real content.
Import
import { HuiSkeleton, HuiSkeletonItem } from '@nscode/handy-ui'
Basic Usage
Without slots, the default layout contains a circular avatar, one title line, and paragraph lines.
<template>
<HuiSkeleton />
</template>
Wrapping Real Content
Place real content in the default slot and control loading: the skeleton appears while loading and the content appears afterward. “Reload” simulates a 2-second request; the switch also changes the state directly.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSkeleton, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const loading = ref(true)
const reload = () => {
loading.value = true
// Simulate a request with a timer.
setTimeout(() => {
loading.value = false
}, 2000)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap align="center">
<HuiSwitch v-model="loading" />
<span>loading:{{ loading }}</span>
<HuiButton size="small" :disabled="loading" @click="reload">Reload</HuiButton>
</HuiSpace>
<HuiSkeleton :loading="loading">
<div>
<h4>Article title</h4>
<p>Article content…</p>
</div>
</HuiSkeleton>
</HuiSpace>
</template>
Paragraph Lines and Animation
rows sets the paragraph-line count in the default layout (3 by default), with a shorter final line. animated enables the shimmer by default; false produces static gray blocks.
<template>
<HuiSpace direction="vertical" fill>
<HuiSkeleton :rows="1" />
<HuiSkeleton :rows="5" />
<HuiSkeleton :animated="false" />
</HuiSpace>
</template>
Use these controls to adjust the line count and toggle the animation:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiCheckbox, HuiInputNumber, HuiSkeleton, HuiSpace } from '@nscode/handy-ui'
const rows = ref(3)
const animated = ref(true)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap align="center">
<span>rows</span>
<HuiInputNumber v-model="rows" :min="1" :max="8" size="small" />
<HuiCheckbox v-model="animated">animated</HuiCheckbox>
</HuiSpace>
<HuiSkeleton :rows="rows" :animated="animated" />
</HuiSpace>
</template>
Custom Template
Replace the default layout with the template slot. Combine HuiSkeletonItem shapes to approximate the real content.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSkeleton, HuiSkeletonItem, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const loading = ref(true)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace align="center">
<HuiSwitch v-model="loading" />
<span>Loading</span>
</HuiSpace>
<HuiSkeleton :loading="loading">
<template #template>
<div style="display: flex; gap: 16px">
<HuiSkeletonItem variant="circle" :size="48" />
<div style="flex: 1">
<HuiSkeletonItem variant="text" width="40%" />
<HuiSkeletonItem variant="text" />
</div>
</div>
</template>
<div>Real content</div>
</HuiSkeleton>
</HuiSpace>
</template>
Multiple Items
count repeats the default layout or the template slot, which is useful for lists.
<template>
<HuiSkeleton :count="3" />
</template>
With a template slot, each repetition contains that slot's content. style and class on HuiSkeleton apply to the skeleton container and can arrange the items, such as three cards in a row:
<template>
<HuiSkeleton
:count="3"
style="display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 16px"
>
<template #template>
<div>
<HuiSkeletonItem variant="image" :height="100" />
<HuiSkeletonItem variant="text" width="70%" style="margin-top: 12px" />
<HuiSkeletonItem variant="text" width="40%" style="margin-top: 8px" />
</div>
</template>
</HuiSkeleton>
</template>
Delayed Display
throttle delays the skeleton by the given number of milliseconds. The default slot remains visible during the delay, preventing a brief skeleton flash for fast requests.
Here throttle is 500ms. “Fast load” takes only 200ms, so the skeleton never appears. “Slow load” takes 2 seconds, showing the skeleton after 500ms.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSkeleton, HuiSpace } from '@nscode/handy-ui'
const loading = ref(false)
// Set loading to true for the request and false when it finishes; a timer simulates it here.
const load = (ms: number) => {
loading.value = true
setTimeout(() => {
loading.value = false
}, ms)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton :disabled="loading" @click="load(200)">Fast load</HuiButton>
<HuiButton :disabled="loading" @click="load(2000)">Slow load</HuiButton>
</HuiSpace>
<HuiSkeleton :loading="loading" :throttle="500">
<div>Real content</div>
</HuiSkeleton>
</HuiSpace>
</template>
SkeletonItem Shapes
HuiSkeletonItem also works on its own. variant accepts text (default), circle, rect, and image. Default dimensions when no size is specified:
| variant | Default dimensions |
|---|---|
| text | 100% wide, 16px high, 4px corner radius |
| circle | 40px × 40px circle |
| rect | 100% wide, 16px high, 6px corner radius |
| image | 100% wide, 120px high, 6px corner radius, centered icon |
Each row below shows one shape at its default size:
<template>
<HuiSpace direction="vertical" fill>
<div class="row">
<span class="row-label">text</span>
<HuiSkeletonItem variant="text" />
</div>
<div class="row">
<span class="row-label">circle</span>
<HuiSkeletonItem variant="circle" />
</div>
<div class="row">
<span class="row-label">rect</span>
<HuiSkeletonItem variant="rect" />
</div>
<div class="row">
<span class="row-label">image</span>
<HuiSkeletonItem variant="image" />
</div>
</HuiSpace>
</template>
<style scoped>
.row {
display: flex;
align-items: center;
gap: var(--hui-spacing-3);
}
.row-label {
flex-shrink: 0;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
A standalone HuiSkeletonItem has no shimmer animation. Inside the template slot of HuiSkeleton, it follows the parent's animated setting.
SkeletonItem Dimensions
width and height set the dimensions separately. size sets both and takes precedence over them. Numbers use pixels; strings pass through unchanged.
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap align="center">
<HuiSkeletonItem variant="text" :width="200" />
<HuiSkeletonItem variant="circle" :size="64" />
<HuiSkeletonItem variant="circle" size="2em" />
<HuiSkeletonItem variant="rect" :width="120" :height="60" />
<!-- size takes precedence: renders at 48px × 48px; width and height are ignored. -->
<HuiSkeletonItem variant="rect" :size="48" :width="200" :height="20" />
</HuiSpace>
<HuiSkeletonItem variant="image" width="100%" :height="160" />
</HuiSpace>
</template>
Configuring the Default Layout
Control the avatar, title, and paragraph separately. The avatar supports two shapes, and widths accept numbers or CSS lengths.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSkeleton, HuiCheckbox, HuiSpace } from '@nscode/handy-ui'
const avatar = ref(true)
const title = ref(true)
const paragraph = ref(true)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><HuiCheckbox v-model="avatar">avatar</HuiCheckbox
><HuiCheckbox v-model="title">title</HuiCheckbox
><HuiCheckbox v-model="paragraph">paragraph</HuiCheckbox></HuiSpace
>
<HuiSkeleton
:avatar="avatar"
:title="title"
:paragraph="paragraph"
:avatar-size="48"
avatar-shape="circle"
title-width="60%"
:rows="2"
/>
<HuiSkeleton
:avatar="avatar"
:title="title"
:paragraph="paragraph"
avatar-size="3em"
avatar-shape="square"
:title-width="160"
:rows="2"
/>
</HuiSpace>
</template>
HuiSkeleton Props
| Prop | Type | Default | Description |
|---|---|---|---|
| loading | boolean | true | Whether loading is active. Shows default-slot content when false. |
| animated | boolean | true | Whether to show the shimmer animation. |
| count | number | 1 | Number of repetitions of the default layout or template slot. |
| rows | number | 3 | Paragraph-line count in the default layout. |
| throttle | number | 0 | Delay before showing the skeleton, in milliseconds. Displays default-slot content during the delay. |
| avatar | boolean | true | Whether the default layout shows an avatar. |
| avatarSize | number | string | 40 | Avatar size in the default layout. |
| avatarShape | 'circle' | 'square' | 'circle' | Avatar shape in the default layout. |
| title | boolean | true | Whether the default layout shows a title. |
| titleWidth | number | string | '38%' | Title width in the default layout. |
| paragraph | boolean | true | Whether the default layout shows a paragraph; rows controls its lines. |
HuiSkeletonItem Props
| Prop | Type | Default | Description |
|---|---|---|---|
| variant | 'text' | 'circle' | 'rect' | 'image' | 'text' | Shape. |
| width | string | number | - | Width. Numbers use pixels. |
| height | string | number | - | Height. Numbers use pixels. |
| size | string | number | - | Sets width and height, taking precedence over width and height. |
HuiSkeleton Slots
| Slot | Description |
|---|---|
| default | Real content, shown when loading is false and during the delay. |
| template | Replaces the loading layout and repeats according to count. |
Types
import type { SkeletonProps, SkeletonItemProps, SkeletonItemVariant } from '@nscode/handy-ui'
type SkeletonItemVariant = 'text' | 'circle' | 'rect' | 'image'
interface SkeletonProps extends SkeletonDefaultLayoutProps {
loading?: boolean
animated?: boolean
count?: number
rows?: number
throttle?: number
}
interface SkeletonItemProps {
variant?: SkeletonItemVariant
width?: string | number
height?: string | number
size?: string | number
}
interface SkeletonDefaultLayoutProps {
avatar?: boolean
avatarSize?: string | number
avatarShape?: 'circle' | 'square'
title?: boolean
titleWidth?: string | number
paragraph?: boolean
}