Toast Toast
Show transient messages or persistent loading feedback through a page host.
Import
Use <hui-toast> with uni-app easycom. Import public types from @nscode/handy-uni. For ordinary Vue 3, install the preview plugin from @nscode/handy-uni/web and register its components.
Examples
Interact with the controls to see values and event results on the page.
<template>
<view>
<hui-page-host
><template #toast><hui-toast /></template></hui-page-host
><hui-button @click="notify">显示提示 / Notify</hui-button
><hui-button @click="loading">持续加载 / Loading</hui-button
><hui-button @click="stop">关闭 / Close</hui-button>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { showToast, showLoadingToast } from '@nscode/handy-uni'
const lastEvent = ref('No event yet')
let handle: ReturnType<typeof showToast> | undefined
const notify = () => {
handle = showToast({ message: '保存成功 / Saved', type: 'success', duration: 1500 })
}
const loading = () => {
handle = showLoadingToast({ message: '处理中 / Working', forbidClick: true })
}
const stop = () => handle?.close()
</script>
Place one hui-page-host on each page. Its live region remains mounted. The close/update handle can change text without restarting its timer; setting duration restarts it. Use duration=0 for a persistent message.
Props
| Prop | Type | Default | Description |
|---|
Root attributes: common props. A Boolean default of undefined supports inheritance; explicit false overrides it. Other Boolean props default to false unless listed otherwise.
Events
None.
Slots
None.
Methods
None.
Function API
| Function | Description |
|---|---|
showToast(options | message) | Shows a message for 2000ms by default. |
showLoadingToast(options | message) | Shows persistent loading feedback and blocks background clicks by default. |
showSuccessToast(options | message) | Shows success feedback. |
showFailToast(options | message) | Shows failure feedback. |
closeToast() | Closes the current function message. |
Each show function returns a ToastHandle. A new message replaces the previous one; stale handles cannot modify the new message. Toast uses the displayed page's highest layer + 1 and a persistent announcement region in PageHost. Style its background with --hui-toast-bg.
import { closeToast, showLoadingToast, showSuccessToast } from '@nscode/handy-uni'
const task = showLoadingToast({ message: 'Saving' })
task.update({ message: 'Saving: 50%' }) // Text changes keep the existing timer.
task.close()
showSuccessToast({ message: 'Saved', duration: 1500 })
closeToast()
Types
ToastProps、ToastType、ToastPosition、ToastOptions、ToastHandle are available from @nscode/handy-uni.
export type ToastType = 'text' | 'success' | 'fail' | 'warning' | 'loading'
export type ToastPosition = 'top' | 'middle' | 'bottom'
export interface ToastOptions {
message?: string
type?: ToastType
icon?: string
duration?: number
position?: ToastPosition
forbidClick?: boolean
onClose?: () => void
}
export interface ToastHandle {
close(): void
update(patch: Partial<ToastOptions>): void
}
Platform notes
| Behavior | uni-app H5 | WeChat Mini Program | App-vue | Ordinary Vue 3 Web (preview) |
|---|---|---|---|---|
| Basic interaction | Supported; device checks pending | Supported; device checks pending | Supported; device checks pending | Supported by the web platform adapter |
| Layer and announcements | Highest page layer + 1; persistent live region | Same as H5; screen reader verification pending | Verify native stacking and announcements | Supports an automatic host and a persistent role=status/aria-live region |
Follow the smoke checklist. Device evidence is tracked separately; unit tests do not establish device acceptance.