Loading
Cover a region or the entire window with an overlay while loading data. The overlay displays a spinner and optional text. Use it as a component, call HuiLoading.service(), or add the v-loading directive to an element.
Import
import { HuiLoading, vLoading } from '@nscode/handy-ui'
HuiLoadingis a component with aHuiLoading.service(options)method. The same service function is also exported separately asHuiLoadingService.vLoadingis the directive.app.use(HandyUI)registers it asv-loading; see the directive section below for on-demand registration.
Basic Usage
Place HuiLoading inside the region to cover and control it with visible. It can be opened and closed repeatedly. Closing uses a 0.3-second fade-out.
The overlay uses position: absolute with all four offsets set to zero. It fills the nearest positioned ancestor. The component does not change its parent's styles, so set position: relative on that parent; otherwise, the overlay may cover a more distant positioned ancestor.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
const loading = ref(false)
</script>
<template>
<div style="margin-bottom: 12px">
<HuiButton @click="loading = !loading">{{ loading ? 'Close' : 'Open' }}</HuiButton>
</div>
<div style="position: relative; height: 120px">
<div style="padding: 16px">Content area</div>
<HuiLoading :visible="loading" />
</div>
</template>
Text, Spinner and Background
textappears below the spinner.spinneris an icon name from@nscode/icons, defaulting to'loading'. The icon rotates continuously. An invalid name falls back to the default loading icon.backgroundsets the overlay background. When omitted, it follows the theme's page background at 90% opacity, including a dark background in dark mode.
<script setup lang="ts">
import { HuiLoading } from '@nscode/handy-ui'
</script>
<template>
<div class="grid">
<div>
<div class="box"><HuiLoading visible /></div>
<p class="caption">Default</p>
</div>
<div>
<div class="box"><HuiLoading visible text="Loading" /></div>
<p class="caption">text</p>
</div>
<div>
<div class="box"><HuiLoading visible text="Refreshing" spinner="refresh" /></div>
<p class="caption">spinner="refresh"</p>
</div>
<div>
<div class="box"><HuiLoading visible text="Configuring" spinner="setting" /></div>
<p class="caption">spinner="setting"</p>
</div>
<div>
<div class="box"><HuiLoading visible text="Invalid icon name" spinner="not-exist" /></div>
<p class="caption">Invalid names use the default icon</p>
</div>
<div>
<div class="box">
<HuiLoading
visible
text="Dark background"
spinner="refresh"
background="rgba(0, 0, 0, 0.5)"
/>
</div>
<p class="caption">background</p>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
gap: var(--hui-spacing-3);
}
.box {
position: relative;
height: 120px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
overflow: hidden;
}
.caption {
margin: var(--hui-spacing-2) 0 0;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
text-align: center;
}
</style>
Fullscreen
With fullscreen, the overlay uses position: fixed and covers the window regardless of where the component appears in the template. This example closes automatically after two seconds.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
const loading = ref(false)
const open = () => {
loading.value = true
window.setTimeout(() => (loading.value = false), 2000)
}
</script>
<template>
<HuiButton @click="open">Fullscreen loading for 2 seconds</HuiButton>
<HuiLoading :visible="loading" fullscreen text="Loading..." />
</template>
Component Methods
Call show() and close() through the component ref to open and close the overlay. The exposed visible value indicates whether the overlay is still on the page; it remains true during the 0.3-second fade-out. These methods do not change the visible prop. When controlling the overlay only through methods, the prop can be omitted and retains its default of false, as below.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading, HuiSpace } from '@nscode/handy-ui'
interface LoadingExpose {
show: () => void
close: () => void
visible: boolean
}
const loadingRef = ref<LoadingExpose>()
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton @click="loadingRef?.show()">show()</HuiButton>
<HuiButton @click="loadingRef?.close()">close()</HuiButton>
<span>visible: {{ loadingRef?.visible ?? false }}</span>
</HuiSpace>
<div style="position: relative; height: 120px">
<div style="padding: 16px">Content area</div>
<HuiLoading ref="loadingRef" text="Opened through a method" />
</div>
</template>
Service API
HuiLoading.service(options) opens an overlay from code and returns an instance with a close() method. No component is needed in the template, making this suitable for request functions and route guards.
Fullscreen Loading
Without target, the overlay is attached to document.body. Set fullscreen: true to keep it fixed over the window while scrolling. lock: true locks page scrolling during loading and restores it on close.
The following timer simulates a two-second request. The finally block closes the overlay when the request finishes.
<script setup lang="ts">
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
// Simulate a two-second request
const fetchData = () => new Promise((resolve) => setTimeout(resolve, 2000))
const load = async () => {
const loading = HuiLoading.service({ fullscreen: true, lock: true, text: 'Loading...' })
try {
await fetchData()
} finally {
loading.close()
}
}
</script>
<template>
<HuiButton @click="load">Fullscreen loading for 2 seconds</HuiButton>
</template>
Loading a Region
Pass a DOM element or CSS selector as target to attach the overlay inside that element and cover it. If the target has static positioning, the service temporarily changes it to relative and restores it after the last overlay on that target closes. It also adds hui-loading-parent while an overlay is open. A selector that finds no element produces a console warning and falls back to document.body.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading, HuiSpace } from '@nscode/handy-ui'
const box = ref<HTMLElement>()
const byElement = () => {
const loading = HuiLoading.service({ target: box.value, text: 'Element target' })
setTimeout(() => loading.close(), 2000)
}
const bySelector = () => {
const loading = HuiLoading.service({
target: '#loading-service-target',
text: 'Selector target'
})
setTimeout(() => loading.close(), 2000)
}
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton @click="byElement">Element target: load for 2 seconds</HuiButton>
<HuiButton @click="bySelector">Selector target: load for 2 seconds</HuiButton>
</HuiSpace>
<div id="loading-service-target" ref="box" class="box">Table region</div>
</template>
<style scoped>
.box {
height: 120px;
padding: 16px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
</style>
Updating Text
The instance's update() method changes the text, spinner and background while loading.
<script setup lang="ts">
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
const run = async () => {
const loading = HuiLoading.service({ fullscreen: true, text: 'Step 1: Read data' })
await wait(1000)
loading.update({ text: 'Step 2: Save', spinner: 'save' })
await wait(1000)
loading.update({ text: 'Step 3: Complete', spinner: 'check', background: 'rgba(0, 0, 0, 0.6)' })
await wait(800)
loading.close()
}
</script>
<template>
<HuiButton @click="run">Load in 3 steps</HuiButton>
</template>
Fullscreen Singleton
Only one fullscreen overlay exists at a time. This includes fullscreen: true and overlays attached to document.body because no target was provided. Calling HuiLoading.service() while one is open, including during fade-out, returns that same instance: the new text, spinner and background replace its settings, and a pending close is canceled. Calling close() from any holder closes it.
Overlays attached to other elements are independent. Each call creates a new instance that must be closed separately.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLoading } from '@nscode/handy-ui'
const result = ref('')
const run = () => {
const first = HuiLoading.service({ fullscreen: true, text: 'First call' })
setTimeout(() => {
const second = HuiLoading.service({ fullscreen: true, text: 'Second call: the same overlay' })
result.value = `Both calls returned the same instance: ${first === second}`
setTimeout(() => second.close(), 1500)
}, 1000)
}
</script>
<template>
<HuiButton @click="run">Call twice in succession</HuiButton>
<p style="margin-top: 12px">{{ result || 'Click the button to try it' }}</p>
</template>
v-loading Directive
The directive accepts a boolean. true adds an overlay inside the element; false removes it after fading out. An element with static positioning is temporarily changed to relative and restored on close.
The directive is already registered by app.use(HandyUI). For on-demand use, choose either approach:
- Register globally with
app.directive('loading', vLoading). - Import
vLoadingfrom@nscode/handy-uiinside<script setup>and usev-loadingdirectly in the template.
Set text, spinner and background through element attributes. Omitting them uses the component defaults.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, vLoading } from '@nscode/handy-ui'
const loading = ref(false)
</script>
<template>
<div style="margin-bottom: 12px">
<HuiButton @click="loading = !loading">{{ loading ? 'Close' : 'Open' }}</HuiButton>
</div>
<div class="grid">
<div v-loading="loading" class="box">No custom attributes</div>
<div
v-loading="loading"
element-loading-text="Loading in progress"
element-loading-spinner="refresh"
element-loading-background="rgba(0, 0, 0, 0.6)"
class="box"
>
Custom text, spinner and background
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: var(--hui-spacing-3);
}
.box {
height: 120px;
padding: 16px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
</style>
element-loading-text, element-loading-spinner and element-loading-background are read again whenever the containing component updates. Changing their bound values updates the overlay. element-loading-fullscreen="true" covers the window; it is read only when the overlay is created.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, vLoading } from '@nscode/handy-ui'
const loading = ref(false)
const percent = ref(0)
const fullscreen = ref(false)
const start = () => {
percent.value = 0
loading.value = true
const timer = setInterval(() => {
percent.value += 20
if (percent.value >= 100) {
clearInterval(timer)
loading.value = false
}
}, 400)
}
const openFullscreen = () => {
fullscreen.value = true
setTimeout(() => (fullscreen.value = false), 2000)
}
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton :disabled="loading" @click="start">Start loading</HuiButton>
<HuiButton @click="openFullscreen">Fullscreen loading for 2 seconds</HuiButton>
</HuiSpace>
<div v-loading="loading" :element-loading-text="`Loaded ${percent}%`" class="box">
Content area
</div>
<div
v-loading="fullscreen"
element-loading-fullscreen="true"
element-loading-text="Fullscreen loading; closes after 2 seconds"
/>
</template>
<style scoped>
.box {
height: 120px;
padding: 16px;
border: var(--hui-border-width) var(--hui-border-style) var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
</style>
Props
Props for component usage.
| Prop | Type | Default | Description |
|---|---|---|---|
| visible | boolean | false | Shows the overlay. Repeated toggling is supported; closing fades out for 0.3 seconds. Omit when using only show() and close() |
| text | string | — | Text below the spinner |
| spinner | string | 'loading' | Icon name from @nscode/icons; an invalid name uses the default loading icon |
| background | string | — | Overlay background; follows the theme when omitted |
| fullscreen | boolean | false | true uses fixed positioning over the window; false uses absolute positioning over the nearest positioned ancestor |
Exposed Component Methods
Call these through the component ref.
| Name | Type | Description |
|---|---|---|
| show | () => void | Shows the overlay and cancels a pending fade-out |
| close | () => void | Hides it after fading out. Does nothing when already hidden or fading out |
| visible | boolean | Read-only indication that the overlay is still on the page, including during fade-out |
show() and close() do not change the visible prop. A subsequent prop change takes precedence.
HuiLoading.service Options
| Option | Type | Default | Description |
|---|---|---|---|
| target | HTMLElement | string | document.body | Element to attach to. Strings are CSS selectors; a missing match warns and falls back to document.body |
| text | string | — | Text below the spinner |
| spinner | string | 'loading' | Icon name; invalid names use the default loading icon |
| background | string | — | Overlay background; follows the theme when omitted |
| fullscreen | boolean | false | Uses fixed positioning to cover the window when true |
| lock | boolean | false | Locks scrolling during loading and restores it on close. Applies only to fullscreen overlays, either fullscreen: true or attached to body |
HuiLoading.service Return Value
Returns a LoadingInstance.
| Method | Type | Description |
|---|---|---|
| close | () => void | Removes the overlay after a 0.3-second fade-out and restores target positioning and scrolling. Repeated calls close it only once |
| update | (options: Partial<LoadingProps>) => void | Updates text, spinner, background and fullscreen. Passing visible: true cancels a pending close |
v-loading Attributes
| Attribute | Description |
|---|---|
| element-loading-text | Text below the spinner; read again on each containing-component update |
| element-loading-spinner | Icon name, defaulting to 'loading'; read again on each update |
| element-loading-background | Overlay background, following the theme by default; read again on each update |
| element-loading-fullscreen | "true" uses fixed positioning over the window. Read only when the overlay is created; later changes have no effect |
Types
import type {
LoadingOptions,
LoadingInstance,
LoadingProps,
LoadingDirectiveBinding,
LoadingServiceFn
} from '@nscode/handy-ui'
interface LoadingOptions {
target?: HTMLElement | string
text?: string
spinner?: string
background?: string
fullscreen?: boolean
lock?: boolean
}
interface LoadingInstance {
close: () => void
update: (options: Partial<LoadingProps>) => void
}
interface LoadingProps {
text?: string
spinner?: string
background?: string
fullscreen?: boolean
visible: boolean
}
interface LoadingDirectiveBinding {
value: boolean
oldValue?: boolean
arg?: string
modifiers?: Record<string, boolean>
}
type LoadingServiceFn = (options: LoadingOptions) => LoadingInstance
HuiLoadingComponent is also exported and refers to the same component as HuiLoading.