Skip to content

Toast

Lightweight prompt component for message notifications, loading prompts, and operation result feedback. Supports component mounting point with useToast() for functional calls.

Tip

Toast supports controlling default styles through component props since 1.7.0. When calling functionally, the passed options take priority over component props.

Global calling solution can refer to wot-starter, suitable for use in route guards or request interceptors.

Component Types

Basic Usage

First place a wd-toast in the page as a mounting point, then call the prompt through useToast().

html
<wd-toast />
<wd-button @click="showToast">toast</wd-button>
ts
import { useToast } from '@/uni_modules/wot-ui'

const toast = useToast()

function showToast() {
  toast.show('Prompt message')
}

Type Prompt

Supports four quick prompts: success, error, warning, and info.

ts
toast.success('Operation successful')
toast.error('Mobile verification code input error, please re-enter')
toast.warning('Prompt message')
toast.info('Regular prompt message')

Component States

Loading Prompt

loading() does not auto-close by default, suitable for waiting for async requests to complete before manually calling close().

ts
toast.loading('Auto-close after 3s')

setTimeout(() => {
  toast.close()
}, 3000)

Loading Type

Switch different loading styles through loadingType, supports circular, spinner, dots.

ts
toast.loading({
  msg: 'Auto-close after 3s',
  loadingType: 'spinner',
  loadingColor: '#fff'
})

Component Styles

Use Icon

You can use built-in icons through iconClass, or use custom icons with classPrefix.

ts
toast.show({
  iconClass: 'star',
  msg: 'Using component library internal icon'
})
ts
toast.show({
  iconClass: 'kehuishouwu',
  classPrefix: 'fish',
  msg: 'Using custom icon'
})

Prompt Position

Adjust prompt position through position, supports top, middle-top, middle, bottom.

ts
toast.show({
  position: 'top',
  msg: 'Prompt message'
})

toast.show({
  position: 'middle',
  msg: 'Prompt message'
})

toast.show({
  position: 'bottom',
  msg: 'Prompt message'
})

Layout Direction

Control horizontal or vertical layout through direction, often used for long text or loading prompts.

ts
toast.success({
  msg: 'Vertical layout',
  direction: 'vertical'
})

Toast Attributes

ParameterDescriptionTypeDefault Value
selectorUnique mounting identifier, used to distinguish different toasts in multi-instance scenariosstring''
msg v1.7.0Default prompt textstring''
direction v1.7.0Default layout direction, optional values are horizontal, verticalToastDirectionhorizontal
icon-name v1.7.0Default icon type, optional values are success, error, warning, loading, infoToastIconType''
icon-size v1.7.0Default icon sizenumber-
loading-type v1.7.0Default loading icon type, optional values are circular, spinner, dotsToastLoadingTypecircular
loading-color v1.7.0Default loading icon colorstring#ffffff
loading-size v1.7.0Default loading icon sizenumber-
icon-color v1.7.0Default icon colorstring-
position v1.7.0Default prompt position, optional values are top, middle-top, middle, bottomToastPositionTypemiddle
z-index v1.7.0Default z-index levelnumber100
cover v1.7.0Whether to show transparent mask layerbooleanfalse
icon-class v1.7.0Default icon class namestring''
class-prefix v1.7.0Icon class name prefixstringwd-icon
opened v1.7.0Callback after fully displayed() => void-
closed v1.7.0Callback after fully closed() => void-
custom-classRoot node custom class namestring''
custom-styleRoot node custom stylestring''

Toast Options

ParameterDescriptionTypeDefault Value
msgPrompt textstring''
durationDuration in milliseconds, 0 means no auto-closenumber2000
directionLayout direction, optional values are horizontal, verticalToastDirectionhorizontal
iconNameIcon type, optional values are success, error, warning, loading, infoToastIconType-
iconSizeIcon sizenumber-
loadingTypeLoading icon type, optional values are circular, spinner, dotsToastLoadingType-
loadingColorLoading icon colorstring-
loadingSizeLoading icon sizenumber-
iconColorIcon colorstring-
positionPrompt position, optional values are top, middle-top, middle, bottomToastPositionTypemiddle
showWhether to show, for internal state use onlyboolean-
zIndexZ-index levelnumber100
coverWhether to show transparent mask layerbooleanfalse
iconClassCustom icon class namestring''
classPrefixCustom icon class name prefixstringwd-icon
openedCallback after fully displayed() => void-
closedCallback after fully closed() => void-

Toast Methods

Method NameDescriptionParametersReturn Value
showShow regular prompt(options: ToastOptions | string)-
successShow success prompt(options: ToastOptions | string)-
errorShow error prompt(options: ToastOptions | string)-
warningShow warning prompt(options: ToastOptions | string)-
infoShow regular prompt(options: ToastOptions | string)-
loadingShow loading prompt(options: ToastOptions | string)-
closeManually close current prompt--

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-toast-color$text-white文字颜色
--wot-toast-padding$padding-extra-loose内边距
--wot-toast-max-width300px最大宽度
--wot-toast-border-radius$radius-large圆角大小
--wot-toast-bg$opacfilled-tooltip-toast-cover背景色
--wot-toast-font-size$typography-body-size-extra-large字号
--wot-toast-line-height$typography-body-line--height-size-extra-large行高
--wot-toast-icon-size$n-24图标大小
--wot-toast-msg-margin-left$spacing-tight消息内容左边距
--wot-toast-icon-margin-bottom$spacing-extra-tight图标下边距
--wot-toast-loading-padding$padding-mainloading 状态下的 padding
--wot-toast-loading-margin-bottom$spacing-extra-looseloading 动画的 margin-bottom

Source Code

Documentation
Component

Released under the MIT License.

Released under the MIT License.