Spin
Show a spinning loading indicator in a region. Use it alone or wrap content to cover it with a translucent mask while loading.
Import
import { HuiSpin } from '@nscode/handy-ui'
Basic Usage
Without slot content, HuiSpin renders only an inline-block spinning icon. spinning defaults to true; setting it to false removes the entire component.
<script setup lang="ts">
import { HuiSpin } from '@nscode/handy-ui'
</script>
<template>
<HuiSpin />
</template>
Toggle spinning with the switch. When disabled, the icon disappears without reserving space, so the surrounding text moves together.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiSpin, HuiSwitch } from '@nscode/handy-ui'
const spinning = ref(true)
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace align="center">
<HuiSwitch v-model="spinning" />
<span>spinning:{{ spinning }}</span>
</HuiSpace>
<div>Text before<HuiSpin :spinning="spinning" size="small" />Text after</div>
</HuiSpace>
</template>
Sizes
size accepts small, default, and large, with icon sizes of 16, 24, and 32 pixels and text sizes of 12, 14, and 16 pixels. The root receives hui-spin--small, hui-spin--default, or hui-spin--large. Use these classes to style a custom indicator, for example .hui-spin--large .my-indicator { font-size: 32px; } (see Custom Indicator).
<script setup lang="ts">
import { HuiSpin, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start" size="large">
<HuiSpace wrap size="large" align="center">
<HuiSpin size="small" />
<HuiSpin />
<HuiSpin size="large" />
</HuiSpace>
<HuiSpace wrap size="large" align="end">
<HuiSpin size="small" tip="small" />
<HuiSpin tip="default" />
<HuiSpin size="large" tip="large" />
</HuiSpace>
</HuiSpace>
</template>
Loading Text
tip appears below the icon.
<script setup lang="ts">
import { HuiSpin } from '@nscode/handy-ui'
</script>
<template>
<HuiSpin tip="Loading..." />
</template>
Wrapping Content
With content in the default slot, HuiSpin becomes a block element. When spinning is true, a translucent mask covers the content, with the icon and text centered on it. The content remains faintly visible but does not receive pointer clicks. When spinning is false, only the content remains. The mask fades in and out over 0.3 seconds.
Place the loading toggle outside HuiSpin; inside it, the mask would block the button while loading.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiSpin } from '@nscode/handy-ui'
const spinning = ref(true)
</script>
<template>
<HuiSpace direction="vertical" fill>
<div>
<HuiButton @click="spinning = !spinning">Toggle loading</HuiButton>
</div>
<HuiSpin :spinning="spinning" tip="Loading...">
<div style="padding: 24px">Content wrapped by Spin</div>
</HuiSpin>
</HuiSpace>
</template>
size also controls the icon and text when wrapping content:
<template>
<HuiSpace direction="vertical" fill>
<HuiSpin size="small" tip="small">
<div class="block">Content area</div>
</HuiSpin>
<HuiSpin tip="default">
<div class="block">Content area</div>
</HuiSpin>
<HuiSpin size="large" tip="large">
<div class="block">Content area</div>
</HuiSpin>
</HuiSpace>
</template>
<style scoped>
.block {
padding: var(--hui-spacing-6);
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
</style>
Simulating a Data Request
Click “Refresh” to load for 1.5 seconds. A mask covers the content and the button shows its loading state; the displayed data changes when loading finishes.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiSpin } from '@nscode/handy-ui'
const loading = ref(false)
const updatedAt = ref('Not refreshed yet')
const visits = ref(1280)
const refresh = () => {
loading.value = true
// Simulate a request with a timer.
setTimeout(() => {
visits.value += Math.floor(Math.random() * 100)
updatedAt.value = new Date().toLocaleTimeString()
loading.value = false
}, 1500)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<div>
<HuiButton type="primary" icon="refresh" :loading="loading" @click="refresh"
>Refresh</HuiButton
>
</div>
<HuiSpin :spinning="loading" tip="Loading data...">
<div class="panel">
<div class="panel__value">{{ visits }}</div>
<div class="panel__label">Today's visits (updated at {{ updatedAt }})</div>
</div>
</HuiSpin>
</HuiSpace>
</template>
<style scoped>
.panel {
padding: var(--hui-spacing-6);
text-align: center;
background-color: var(--hui-fill-color-light);
border-radius: var(--hui-border-radius);
}
.panel__value {
font-size: var(--hui-font-size-3xl);
font-weight: var(--hui-font-weight-semibold);
color: var(--hui-text-color);
}
.panel__label {
margin-top: var(--hui-spacing-2);
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Delayed Display
delay specifies how many milliseconds to wait after spinning becomes true. If it becomes false before that time, neither the icon nor the mask appears. This avoids flicker for fast requests. Setting spinning to false hides them immediately, regardless of delay.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiSpin } from '@nscode/handy-ui'
const spinning = ref(false)
const load = (ms: number) => {
spinning.value = true
window.setTimeout(() => (spinning.value = false), ms)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<!-- A 200ms request finishes before delay, so the mask never appears. -->
<HuiButton @click="load(200)">Fast load</HuiButton>
<!-- A 2-second request shows the mask after 500ms. -->
<HuiButton @click="load(2000)">Slow load</HuiButton>
</HuiSpace>
<HuiSpin :spinning="spinning" :delay="500">
<div style="padding: 24px">delay is 500ms</div>
</HuiSpin>
</HuiSpace>
</template>
Custom Indicator
The indicator slot replaces the default spinner. Its content does not rotate automatically; add rotation yourself, such as the spin prop on HuiIcon.
<script setup lang="ts">
import { HuiIcon, HuiSpin } from '@nscode/handy-ui'
</script>
<template>
<HuiSpin tip="Refreshing">
<template #indicator>
<HuiIcon name="refresh" spin size="xl" />
</template>
</HuiSpin>
</template>
Custom slot content does not change its size or color with size. Style it through the root size classes (hui-spin--small / default / large) when different sizes are needed:
<template>
<HuiSpace wrap size="large" align="end">
<HuiSpin size="small" tip="small">
<template #indicator>
<HuiIcon name="refresh" spin class="my-indicator" />
</template>
</HuiSpin>
<HuiSpin tip="default">
<template #indicator>
<HuiIcon name="refresh" spin class="my-indicator" />
</template>
</HuiSpin>
<HuiSpin size="large" tip="large">
<template #indicator>
<HuiIcon name="refresh" spin class="my-indicator" />
</template>
</HuiSpin>
</HuiSpace>
</template>
<style scoped>
.my-indicator {
color: var(--hui-color-primary);
}
.hui-spin--small .my-indicator {
font-size: 16px;
}
.hui-spin--default .my-indicator {
font-size: 24px;
}
.hui-spin--large .my-indicator {
font-size: 32px;
}
</style>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| spinning | boolean | true | Whether the loading indicator is active. |
| size | 'large' | 'default' | 'small' | — | Icon and loading-text size. Inherits HuiConfigProvider's size when omitted, then falls back to 'default'. |
| tip | string | — | Loading text below the icon. |
| delay | number | 0 | Delay in milliseconds after spinning becomes true. 0 displays it immediately. |
Slots
| Slot | Description |
|---|---|
| default | Wrapped content. When present, loading uses a translucent mask over it. |
| indicator | Replaces the default spinning icon. |
Types
import type { SpinSize, SpinProps } from '@nscode/handy-ui'
type SpinSize = 'large' | 'default' | 'small'
interface SpinProps {
size?: SpinSize
spinning?: boolean
tip?: string
delay?: number
}