vant/packages/toast/index.js
neverland ffa3fddfae
[bugfix] remove unnecessary props (#323)
* fix: Tabbar icon line-height

* [new feature] progress add showPivot prop

* [new feature] TabItem support vue-router

* [new feature] update document header style

* [Doc] add toast english ducoment

* [bugfix] Search box-sizing wrong

* [Doc] update vant-demo respo

* [Doc] translate theme & demo pages

* [Doc] add Internationalization document

* [bugfix] remove unnecessary props
2017-11-16 03:12:13 -06:00

60 lines
1.2 KiB
JavaScript

import Vue from 'vue';
import VueToast from './toast';
let instance;
const defaultOptions = {
visible: true,
type: 'text',
mask: false,
position: 'middle',
duration: 3000,
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;