UNPKG

4.56 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var assign = _interopDefault(require('nano-assign'));
6
7var prevToast = null;
8var icons = {
9 warning: "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"feather feather-alert-circle\"><circle cx=\"12\" cy=\"12\" r=\"10\"></circle><line x1=\"12\" y1=\"8\" x2=\"12\" y2=\"12\"></line><line x1=\"12\" y1=\"16\" x2=\"12\" y2=\"16\"></line></svg>",
10 success: "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"feather feather-check-circle\"><path d=\"M22 11.08V12a10 10 0 1 1-5.93-9.14\"></path><polyline points=\"22 4 12 14.01 9 11.01\"></polyline></svg>",
11 info: "<svg viewBox=\"0 0 32 32\" width=\"32\" height=\"32\" fill=\"none\" stroke=\"currentcolor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"6.25%\"><path d=\"M16 14 L16 23 M16 8 L16 10\" /><circle cx=\"16\" cy=\"16\" r=\"14\" /></svg>",
12 error: "<svg width=\"24\" height=\"24\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" class=\"feather feather-alert-triangle\"><path d=\"M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z\"></path><line x1=\"12\" y1=\"9\" x2=\"12\" y2=\"13\"></line><line x1=\"12\" y1=\"17\" x2=\"12\" y2=\"17\"></line></svg>"
13};
14
15var Toast = function Toast(ref) {
16 var this$1 = this;
17 if ( ref === void 0 ) ref = {};
18 var message = ref.message; if ( message === void 0 ) message = '';
19 var position = ref.position; if ( position === void 0 ) position = 'south-east';
20 var timeout = ref.timeout; if ( timeout === void 0 ) timeout = 3000;
21 var el = ref.el; if ( el === void 0 ) el = document.body;
22 var rounded = ref.rounded; if ( rounded === void 0 ) rounded = false;
23 var type = ref.type; if ( type === void 0 ) type = '';
24 var debug = ref.debug; if ( debug === void 0 ) debug = false;
25 var edge = ref.edge; if ( edge === void 0 ) edge = false;
26 var icon = ref.icon; if ( icon === void 0 ) icon = true;
27 var closeOnClick = ref.closeOnClick; if ( closeOnClick === void 0 ) closeOnClick = false;
28 var elements = ref.elements; if ( elements === void 0 ) elements = [];
29
30 if (prevToast) {
31 prevToast.destroy();
32 }
33
34 this.message = message;
35 this.position = position;
36 this.el = el;
37 this.timeout = timeout;
38 this.closeOnClick = closeOnClick;
39 this.toast = document.createElement('div');
40 this.toast.className = "native-toast native-toast-" + (this.position);
41
42 if (type) {
43 this.toast.className += " native-toast-" + type;
44
45 if (icon) {
46 this.message = "<span class=\"native-toast-icon-" + type + "\">" + (icons[type] || '') + "</span>" + (this.message);
47 }
48 }
49
50 var messageElement = document.createElement('div');
51 messageElement.className = 'native-toast-message';
52 messageElement.innerHTML = this.message;
53 [messageElement ].concat( elements).forEach(function (el) {
54 this$1.toast.appendChild(el);
55 });
56 var isMobile = document.body.clientWidth < 768;
57
58 if (edge || isMobile) {
59 this.toast.className += ' native-toast-edge';
60 } else if (rounded) {
61 this.toast.style.borderRadius = '33px';
62 }
63
64 this.el.appendChild(this.toast);
65 prevToast = this;
66 this.show();
67
68 if (!debug && timeout) {
69 this.hide();
70 }
71
72 if (this.closeOnClick) {
73 this.toast.addEventListener('click', function () {
74 this$1.destroy();
75 });
76 }
77};
78
79Toast.prototype.show = function show () {
80 var this$1 = this;
81
82 setTimeout(function () {
83 this$1.toast.classList.add('native-toast-shown');
84 }, 300);
85};
86
87Toast.prototype.hide = function hide () {
88 var this$1 = this;
89
90 setTimeout(function () {
91 this$1.destroy();
92 }, this.timeout);
93};
94
95Toast.prototype.destroy = function destroy () {
96 var this$1 = this;
97
98 if (!this.toast) { return; }
99 this.toast.classList.remove('native-toast-shown');
100 setTimeout(function () {
101 if (this$1.toast) {
102 this$1.el.removeChild(this$1.toast);
103 this$1.toast = null;
104 }
105 }, 300);
106};
107
108function toast(options) {
109 return new Toast(options);
110}
111
112var loop = function () {
113 toast[type] = function (options) { return toast(assign({
114 type: type
115 }, options)); };
116};
117
118for (var type of ['success', 'info', 'warning', 'error']) loop();
119
120module.exports = toast;