vant/packages/toast/index.js
neverland 257654ff03
[new feature] toast add mask option (#296)
* [bugfix] CouponList always show empty info

* [bugfix] add click feedback of buttons in components

* [Doc] add custom theme document

* [new feature] Notice bar support more props

* [bugfix] PullRefresh test cases

* [bugfix] unused NoticeBar style

* [bugfix] Swipe width calc error

* [Doc] english document of all action components

* [Doc] change document site path to /zanui/vant

* [Doc] fix

* [bugfix] uploader style error

* [bugfix] tabs document demo

* [new feature] Cell support vue-router target route

* [bugfix] add cell test cases

* update yarn.lock

* [bugfix] Tabbar cann't display info when use icon slot

* [Doc] update document title

* [bugfix] Dialog should reset button text when showed

* [new feature] CouponList add showCloseButton prop

* [new feature] Swipe add 'initialSwipe' prop

* [bugfix] NoticeBar text disappeared when page back

* [new feature] ImagePreview support startPosition

* fix: improve imagePreview test cases

* [bugfix] Steps style error when has more than 4 items

* [new feature] normalize size of all icons

* [new feature] Stepper add plus & minus event

* fix: yarn.lock

* [bugfix] addressEdit icon render failed

* [new feature] toast add mask option
2017-11-09 01:08:46 -06:00

60 lines
1.2 KiB
JavaScript

import Vue from 'vue';
import VueToast from './toast';
let instance;
const defaultOptions = {
visible: true,
type: 'text',
position: 'middle',
duration: 3000,
mask: false,
forbidClick: false,
clear: () => {
instance.visible = false;
}
};
const createInstance = () => {
if (!instance) {
const ToastConstructor = Vue.extend(VueToast);
instance = new ToastConstructor({
el: document.createElement('div')
});
document.body.appendChild(instance.$el);
}
};
const Toast = (options = {}) => {
createInstance();
options = typeof options === 'string' ? { message: options } : options;
options = { ...defaultOptions, ...options };
Object.assign(instance, options);
clearTimeout(instance.timer);
if (options.duration !== 0) {
instance.timer = setTimeout(() => {
instance.clear();
}, options.duration);
}
return instance;
};
const createMethod = type => (options = {}) => Toast({
type,
message: typeof options === 'string' ? options : options.message,
...options
});
Toast.loading = createMethod('loading');
Toast.success = createMethod('success');
Toast.fail = createMethod('fail');
Toast.clear = () => {
instance && instance.clear();
};
export default Toast;