import{o as t,a,y as n}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},d=n(`

Toast

Intro

Black semi-transparent pop-up hint in the middle of the page, used for message notification, loading hint, operation result hint and other scenarios.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

import { createApp } from 'vue';
import { Toast } from 'vant';

const app = createApp();
app.use(Toast);

Usage

Text

Toast('Some messages');

Loading

Toast.loading({
  message: 'Loading...',
  forbidClick: true,
});

Success/Fail

Toast.success('Success');
Toast.fail('Fail');

Custom Icon

Toast({
  message: 'Custom Icon',
  icon: 'like-o',
});

Toast({
  message: 'Custom Image',
  icon: 'https://fastly.jsdelivr.net/npm/@vant/assets/logo.png',
});

Toast.loading({
  message: 'Loading...',
  forbidClick: true,
  loadingType: 'spinner',
});

Custom Position

Toast({
  message: 'Top',
  position: 'top',
});

Toast({
  message: 'Bottom',
  position: 'bottom',
});

Update Message

const toast = Toast.loading({
  duration: 0,
  forbidClick: true,
  loadingType: 'spinner',
  message: '3 seconds',
});

let second = 3;
const timer = setInterval(() => {
  second--;
  if (second) {
    toast.message = \`\${second} seconds\`;
  } else {
    clearInterval(timer);
    Toast.clear();
  }
}, 1000);

Global Method

After registering the Toast component through app.use, the $toast method will be automatically mounted on all subcomponents of the app.

export default {
  mounted() {
    this.$toast('Some messages');
  },
};

Singleton

Toast use singleton mode by default, if you need to pop multiple Toast at the same time, you can refer to the following example:

Toast.allowMultiple();

const toast1 = Toast('First Toast');
const toast2 = Toast.success('Second Toast');

toast1.clear();
toast2.clear();

Set Default Options

The Toast default configuration can be globally modified with the Toast.setDefaultOptions function.

Toast.setDefaultOptions({ duration: 2000 });

Toast.setDefaultOptions('loading', { forbidClick: true });

Toast.resetDefaultOptions();

Toast.resetDefaultOptions('loading');

API

Methods

MethodsAttributeReturn valueDescription
Toastoptions | messagetoast instanceShow toast
Toast.loadingoptions | messagetoast instanceShow loading toast
Toast.successoptions | messagetoast instanceShow success toast
Toast.failoptions | messagetoast instanceShow fail toast
Toast.clearclearAll: booleanvoidClose toast
Toast.allowMultiple-voidAllow multiple toast at the same time
Toast.setDefaultOptionstype | optionsvoidSet default options of all toasts
Toast.resetDefaultOptionstypevoidReset default options of all toasts

Options

AttributeDescriptionTypeDefault
typeCan be set to loading success fail htmlToastTypetext
positionCan be set to top middle bottomToastPositionmiddle
messageMessagestring''
iconCustom iconstring-
iconSizeCustom icon sizenumber | string36px
iconPrefixIcon className prefixstringvan-icon
overlayWhether to show overlaybooleanfalse
forbidClickWhether to forbid click backgroundbooleanfalse
closeOnClickWhether to close after clickedbooleanfalse
closeOnClickOverlayWhether to close when overlay is clickedbooleanfalse
loadingTypeLoading icon type, can be set to spinnerstringcircular
durationToast duration(ms), won't disappear if value is 0number2000
classNameCustom classNamestring | Array | object-
overlayClass v3.0.4Custom overlay classstring | Array | object-
overlayStyle v3.0.4Custom overlay styleobject-
onOpenedCallback function after openedFunction-
onCloseCallback function after closeFunction-
transitionTransition, equivalent to name prop of transitionstringvan-fade
teleportSpecifies a target element where Toast will be mountedstring | Elementbody

Types

The component exports the following type definitions:

import type { ToastType, ToastProps, ToastOptions, ToastPosition } from 'vant';

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-toast-max-width70%-
--van-toast-font-sizevar(--van-font-size-md)-
--van-toast-text-colorvar(--van-white)-
--van-toast-loading-icon-colorvar(--van-white)-
--van-toast-line-heightvar(--van-line-height-md)-
--van-toast-border-radiusvar(--van-border-radius-lg)-
--van-toast-background-colorfade(var(--van-black), 70%)-
--van-toast-icon-size36px-
--van-toast-text-min-width96px-
--van-toast-text-paddingvar(--van-padding-xs) var(--van-padding-sm)-
--van-toast-default-paddingvar(--van-padding-md)-
--van-toast-default-width88px-
--van-toast-default-min-height88px-
--van-toast-position-top-distance20%-
--van-toast-position-bottom-distance20%-
`,19),o=[d],h={__name:"README",setup(l,{expose:s}){return s({frontmatter:{}}),(i,p)=>(t(),a("div",e,o))}};export{h as default};