kitzo 2.1.30 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,31 @@ function _arrayWithHoles(r) {
11
11
  function _arrayWithoutHoles(r) {
12
12
  if (Array.isArray(r)) return _arrayLikeToArray(r);
13
13
  }
14
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
15
+ try {
16
+ var i = n[a](c),
17
+ u = i.value;
18
+ } catch (n) {
19
+ return void e(n);
20
+ }
21
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
22
+ }
23
+ function _asyncToGenerator(n) {
24
+ return function () {
25
+ var t = this,
26
+ e = arguments;
27
+ return new Promise(function (r, o) {
28
+ var a = n.apply(t, e);
29
+ function _next(n) {
30
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
31
+ }
32
+ function _throw(n) {
33
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
34
+ }
35
+ _next(void 0);
36
+ });
37
+ };
38
+ }
14
39
  function _defineProperty(e, r, t) {
15
40
  return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
16
41
  value: t,
@@ -73,6 +98,114 @@ function _objectSpread2(e) {
73
98
  }
74
99
  return e;
75
100
  }
101
+ function _regenerator() {
102
+ /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
103
+ var e,
104
+ t,
105
+ r = "function" == typeof Symbol ? Symbol : {},
106
+ n = r.iterator || "@@iterator",
107
+ o = r.toStringTag || "@@toStringTag";
108
+ function i(r, n, o, i) {
109
+ var c = n && n.prototype instanceof Generator ? n : Generator,
110
+ u = Object.create(c.prototype);
111
+ return _regeneratorDefine(u, "_invoke", function (r, n, o) {
112
+ var i,
113
+ c,
114
+ u,
115
+ f = 0,
116
+ p = o || [],
117
+ y = false,
118
+ G = {
119
+ p: 0,
120
+ n: 0,
121
+ v: e,
122
+ a: d,
123
+ f: d.bind(e, 4),
124
+ d: function (t, r) {
125
+ return i = t, c = 0, u = e, G.n = r, a;
126
+ }
127
+ };
128
+ function d(r, n) {
129
+ for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) {
130
+ var o,
131
+ i = p[t],
132
+ d = G.p,
133
+ l = i[2];
134
+ r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0));
135
+ }
136
+ if (o || r > 1) return a;
137
+ throw y = true, n;
138
+ }
139
+ return function (o, p, l) {
140
+ if (f > 1) throw TypeError("Generator is already running");
141
+ for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) {
142
+ i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u);
143
+ try {
144
+ if (f = 2, i) {
145
+ if (c || (o = "next"), t = i[o]) {
146
+ if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object");
147
+ if (!t.done) return t;
148
+ u = t.value, c < 2 && (c = 0);
149
+ } else 1 === c && (t = i.return) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1);
150
+ i = e;
151
+ } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break;
152
+ } catch (t) {
153
+ i = e, c = 1, u = t;
154
+ } finally {
155
+ f = 1;
156
+ }
157
+ }
158
+ return {
159
+ value: t,
160
+ done: y
161
+ };
162
+ };
163
+ }(r, o, i), true), u;
164
+ }
165
+ var a = {};
166
+ function Generator() {}
167
+ function GeneratorFunction() {}
168
+ function GeneratorFunctionPrototype() {}
169
+ t = Object.getPrototypeOf;
170
+ var c = [][n] ? t(t([][n]())) : (_regeneratorDefine(t = {}, n, function () {
171
+ return this;
172
+ }), t),
173
+ u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c);
174
+ function f(e) {
175
+ return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e;
176
+ }
177
+ return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine(u), _regeneratorDefine(u, o, "Generator"), _regeneratorDefine(u, n, function () {
178
+ return this;
179
+ }), _regeneratorDefine(u, "toString", function () {
180
+ return "[object Generator]";
181
+ }), (_regenerator = function () {
182
+ return {
183
+ w: i,
184
+ m: f
185
+ };
186
+ })();
187
+ }
188
+ function _regeneratorDefine(e, r, n, t) {
189
+ var i = Object.defineProperty;
190
+ try {
191
+ i({}, "", {});
192
+ } catch (e) {
193
+ i = 0;
194
+ }
195
+ _regeneratorDefine = function (e, r, n, t) {
196
+ function o(r, n) {
197
+ _regeneratorDefine(e, r, function (e) {
198
+ return this._invoke(r, n, e);
199
+ });
200
+ }
201
+ r ? i ? i(e, r, {
202
+ value: n,
203
+ enumerable: !t,
204
+ configurable: !t,
205
+ writable: !t
206
+ }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2));
207
+ }, _regeneratorDefine(e, r, n, t);
208
+ }
76
209
  function _slicedToArray(r, e) {
77
210
  return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
78
211
  }
@@ -93,6 +226,15 @@ function _toPropertyKey(t) {
93
226
  var i = _toPrimitive(t, "string");
94
227
  return "symbol" == typeof i ? i : i + "";
95
228
  }
229
+ function _typeof(o) {
230
+ "@babel/helpers - typeof";
231
+
232
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
233
+ return typeof o;
234
+ } : function (o) {
235
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
236
+ }, _typeof(o);
237
+ }
96
238
  function _unsupportedIterableToArray(r, a) {
97
239
  if (r) {
98
240
  if ("string" == typeof r) return _arrayLikeToArray(r, a);
@@ -101,136 +243,280 @@ function _unsupportedIterableToArray(r, a) {
101
243
  }
102
244
  }
103
245
 
104
- var toastStyles = ".toast-content,\n.toast-content-bottom {\n font-size: 0.925rem;\n}\n.toast-content,\n.toast-content-bottom {\n pointer-events: all;\n}\n \n.pre-styled {\n padding-inline: 0.825rem;\n padding-block: 0.625rem;\n display: flex;\n align-items: center;\n gap: 0.5rem;\n background-color: white;\n border-radius: 0.5525rem;\n box-shadow: 0 2px 8px -3px rgba(0, 0, 0, 0.3);\n}\n\n.toast-content {\n animation: slide-in 230ms forwards;\n}\n.toast-content.exit {\n animation: slide-out 110ms forwards;\n}\n\n.toast-content-bottom {\n animation: bottom-slide-in 230ms forwards;\n}\n.toast-content-bottom.exit {\n animation: bottom-slide-out 110ms forwards;\n}\n\n@keyframes slide-in {\n from {\n opacity: 0;\n translate: 0 -100%;\n scale: 0.7;\n }\n to {\n opacity: 1;\n translate: 0 0;\n scale: 1;\n }\n}\n\n@keyframes slide-out {\n from {\n opacity: 1;\n translate: 0 0;\n scale: 1;\n }\n to {\n opacity: 0;\n translate: 0 -100%;\n scale: 0.7;\n }\n}\n\n@keyframes bottom-slide-in {\n from {\n opacity: 0;\n translate: 0 100%;\n scale: 0.7;\n }\n to {\n opacity: 1;\n translate: 0 0;\n scale: 1;\n }\n}\n\n@keyframes bottom-slide-out {\n from {\n opacity: 1;\n translate: 0 0;\n scale: 1;\n }\n to {\n opacity: 0;\n translate: 0 100%;\n scale: 0.7;\n }\n}\n\n.svg-container {\n display: grid;\n place-items: center;\n border-radius: 10rem;\n background-color: #61d345;\n color: white;\n height: 20px;\n width: 20px;\n position: relative;\n overflow: hidden;\n\n scale: 1.1;\n animation: svg-container-animation 400ms ease-in-out forwards;\n\n svg {\n width: 14px;\n height: 14px;\n stroke-width: 4px;\n display: inline-block;\n }\n}\n\n.svg-container.success {\n background-color: #61d345;\n}\n.svg-container.error {\n background-color: #ff4b4b;\n\n svg {\n scale: 0;\n animation: error-svg-zoom-in 170ms 130ms forwards;\n }\n}\n\n.svg-container.success::before {\n content: '';\n position: absolute;\n inset: 0;\n z-index: 5;\n border-radius: 10rem;\n background-color: #61d345;\n animation: success-container-before-animation 250ms 150ms forwards;\n}\n\n@keyframes svg-container-animation {\n 0% {\n scale: 1;\n }\n 50% {\n scale: 1.15;\n }\n 100% {\n scale: 1;\n }\n}\n\n@keyframes success-container-before-animation {\n from {\n translate: 0 0;\n }\n to {\n translate: 100% -50%;\n }\n}\n\n@keyframes error-svg-zoom-in {\n from {\n scale: 0;\n }\n to {\n scale: 1;\n }\n}\n\n.promise-svg-container {\n width: 20px;\n height: 20px;\n display: grid;\n place-items: center;\n color: #474747;\n\n svg {\n width: 14px;\n height: 14px;\n stroke-width: 3px;\n display: inline-block;\n animation: rotate-infinity 1000ms infinite linear;\n }\n}\n\n@keyframes rotate-infinity {\n to {\n rotate: 360deg;\n }\n}\n";
246
+ var toastStyles = ".kitzo-toast-container {\n --normal-bg: hsl(0, 0%, 100%);\n --success-bg: hsl(152, 65%, 95%);\n --error-bg: hsl(0, 65%, 95%);\n --info-bg: hsl(210, 65%, 95%);\n\n --normal-text: hsl(0, 0%, 10%);\n --success-text: hsl(142, 98%, 30%);\n --error-text: hsl(6, 98%, 40%);\n --info-text: hsl(210, 100%, 20%);\n\n --normal-border: hsl(0, 0%, 94%);\n --success-border: hsl(123, 100%, 93%);\n --error-border: hsl(0, 100%, 94%);\n --info-border: hsl(210, 100%, 94%);\n\n --success-svg-bg: hsl(142, 98%, 40%);\n --error-svg-bg: hsl(6, 98%, 50%);\n --info-svg-bg: hsl(210, 100%, 40%);\n\n --loader-stroke: hsl(0, 0%, 50%);\n --loader-bg: hsl(0, 0%, 80%);\n}\n\n.kitzo-toast-container.kitzo-toast-dark {\n --normal-bg: hsl(0, 0%, 15%);\n --success-bg: hsl(152, 65%, 15%);\n --error-bg: hsl(0, 65%, 15%);\n --info-bg: hsl(210, 65%, 15%);\n\n --normal-text: hsl(0, 0%, 95%);\n --success-text: hsl(142, 98%, 70%);\n --error-text: hsl(6, 98%, 70%);\n --info-text: hsl(210, 100%, 70%);\n\n --normal-border: hsl(0, 0%, 17%);\n --success-border: hsl(123, 100%, 15%);\n --error-border: hsl(0, 100%, 15%);\n --info-border: hsl(210, 100%, 16%);\n\n --success-svg-bg: hsl(142, 98%, 40%);\n --error-svg-bg: hsl(6, 98%, 50%);\n --info-svg-bg: hsl(210, 100%, 40%);\n\n --loader-stroke: hsl(0, 0%, 80%);\n --loader-bg: hsl(0, 0%, 50%);\n}\n\n.kitzo-toast {\n font-family: inherit;\n font-size: 0.875rem;\n transition:\n opacity 160ms,\n transform 210ms;\n}\n\n/*! toast transition styles */\n.kitzo-toast.pos-y-top {\n transform-origin: top;\n}\n.kitzo-toast.pos-y-bottom {\n transform-origin: bottom;\n}\n\n.kitzo-toast.type-normal {\n background-color: var(--normal-bg);\n color: var(--normal-text);\n border: 1px solid var(--normal-border);\n}\n.kitzo-toast.type-loading {\n background-color: var(--normal-bg);\n color: var(--normal-text);\n border: 1px solid var(--normal-border);\n}\n\n.kitzo-toast.type-success {\n background-color: var(--success-bg);\n color: var(--success-text);\n border: 1px solid var(--success-border);\n}\n\n.kitzo-toast.type-error {\n background-color: var(--error-bg);\n color: var(--error-text);\n border: 1px solid var(--error-border);\n}\n\n.kitzo-toast.type-info {\n background-color: var(--info-bg);\n color: var(--info-text);\n border: 1px solid var(--info-border);\n}\n\n.kitzo-toast.type-normal,\n.kitzo-toast.type-loading,\n.kitzo-toast.type-success,\n.kitzo-toast.type-error,\n.kitzo-toast.type-info {\n border-radius: 0.425rem;\n padding: 0.2525rem 0.5rem;\n box-shadow: 0 3px 8px -3px hsl(0, 0%, 0%, 0.15);\n}\n\n.kitzo-toast.status-entering.pos-y-top {\n opacity: 0;\n transform: translateY(-120%) scale(0.5);\n}\n\n.kitzo-toast.status-visible.pos-y-top {\n opacity: 1;\n transform: translateY(0) scale(1);\n}\n\n.kitzo-toast.status-leaving.pos-y-top {\n opacity: 0;\n transform: translateY(-120%) scale(0.5);\n}\n\n.kitzo-toast.status-entering.pos-y-bottom {\n opacity: 0;\n transform: translateY(120%) scale(0.5);\n}\n\n.kitzo-toast.status-visible.pos-y-bottom {\n opacity: 1;\n transform: translateY(0) scale(1);\n}\n\n.kitzo-toast.status-leaving.pos-y-bottom {\n opacity: 0;\n transform: translateY(120%) scale(0.5);\n}\n\n.action-update {\n animation: update 150ms;\n}\n\n@keyframes update {\n 0% {\n opacity: 0.5;\n }\n 100% {\n opacity: 1;\n }\n}\n\n/*! svg stylings */\n.svg-container {\n display: grid;\n place-items: center;\n border-radius: 10rem;\n color: white;\n height: 18px;\n width: 18px;\n position: relative;\n overflow: hidden;\n\n scale: 1.1;\n animation: svg-container-animation 400ms ease-in-out forwards;\n\n svg {\n width: 12px;\n height: 12px;\n stroke-width: 4px;\n display: inline-block;\n }\n}\n\n.svg-container.success {\n background-color: var(--success-svg-bg);\n}\n.svg-container.error {\n background-color: var(--error-svg-bg);\n\n svg {\n scale: 0;\n animation: error-svg-zoom-in 170ms 130ms forwards;\n }\n}\n\n.svg-container.info {\n background-color: var(--info-svg-bg);\n animation: info-svg-bell 400ms 200ms;\n\n svg {\n width: 14px;\n height: 14px;\n color: white;\n }\n}\n\n.svg-container.success::before {\n content: '';\n position: absolute;\n inset: 0;\n z-index: 5;\n border-radius: 10rem;\n background-color: var(--success-svg-bg);\n animation: success-container-before-animation 250ms 150ms forwards;\n}\n\n@keyframes svg-container-animation {\n 0% {\n scale: 1;\n }\n 50% {\n scale: 1.15;\n }\n 100% {\n scale: 1;\n }\n}\n\n@keyframes success-container-before-animation {\n from {\n translate: 0 0;\n }\n to {\n translate: 100% -50%;\n }\n}\n\n@keyframes error-svg-zoom-in {\n from {\n scale: 0;\n }\n to {\n scale: 1;\n }\n}\n\n@keyframes info-svg-bell {\n 0% {\n transform: rotate(0);\n }\n 25% {\n transform: rotate(15deg);\n }\n 75% {\n transform: rotate(-15deg);\n }\n 100% {\n transform: rotate(0);\n }\n}\n\n.promise-svg-container {\n width: 20px;\n height: 20px;\n display: grid;\n place-items: center;\n\n .loader {\n width: 14px;\n height: 14px;\n background-image: conic-gradient(\n var(--loader-stroke) 0 25%,\n var(--loader-bg) 0 100%\n );\n border-radius: 10rem;\n position: relative;\n animation: rotate-infinity 1000ms linear infinite;\n }\n .loader::before {\n content: '';\n position: absolute;\n inset: 2px;\n border-radius: inherit;\n background-color: var(--normal-bg);\n }\n}\n\n@keyframes rotate-infinity {\n to {\n rotate: 360deg;\n }\n}\n";
105
247
  var listeners = new Set();
106
- function addToast(toast) {
107
- listeners.forEach(function (callback) {
108
- callback(toast);
109
- });
110
- }
111
- function subscribe(callback) {
112
- if (!document.getElementById('kitzo-react-toast-styles')) {
248
+ function subscribe(fn) {
249
+ if (!document.getElementById('kitzo-toast-styles')) {
113
250
  var styleTag = document.createElement('style');
114
- styleTag.id = 'kitzo-react-toast-styles';
251
+ styleTag.id = 'kitzo-toast-styles';
115
252
  styleTag.textContent = toastStyles;
116
253
  document.head.appendChild(styleTag);
117
254
  }
118
- listeners.add(callback);
255
+ listeners.add(fn);
119
256
  return function () {
120
- return listeners["delete"](callback);
257
+ return listeners["delete"](fn);
121
258
  };
122
259
  }
123
- function toast() {
124
- var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Toast message';
125
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
126
- var id = Date.now();
127
- options = Object.assign({
128
- duration: 2800,
129
- id: id,
130
- style: {},
131
- showIcon: false
132
- }, options);
133
- addToast({
134
- type: 'success',
135
- text: text,
136
- options: options
260
+ function notify(toast) {
261
+ listeners.forEach(function (fn) {
262
+ return fn(toast);
137
263
  });
138
264
  }
139
- toast.success = function () {
140
- var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Toast success';
141
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
142
- var id = Date.now();
143
- options = Object.assign({
144
- duration: 2800,
145
- id: id,
146
- style: {},
147
- showIcon: true
148
- }, options);
149
- addToast({
150
- type: 'success',
151
- text: text,
152
- options: options
265
+ var DEFAULTS = {
266
+ duration: 2800,
267
+ showIcon: true
268
+ };
269
+ var uid = 0;
270
+ var genId = function genId() {
271
+ var _crypto$randomUUID, _crypto$randomUUID2, _crypto;
272
+ return (_crypto$randomUUID = (_crypto$randomUUID2 = (_crypto = crypto).randomUUID) === null || _crypto$randomUUID2 === void 0 ? void 0 : _crypto$randomUUID2.call(_crypto)) !== null && _crypto$randomUUID !== void 0 ? _crypto$randomUUID : "kitzo_toast_id_".concat(++uid);
273
+ };
274
+ var zIndex = 1;
275
+ function createToast(type, content) {
276
+ var _opts$id, _opts$duration, _opts$showIcon;
277
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
278
+ var opts = _typeof(options) === 'object' && options !== null ? options : {};
279
+ return _objectSpread2({
280
+ id: (_opts$id = opts.id) !== null && _opts$id !== void 0 ? _opts$id : genId(),
281
+ type: type,
282
+ zIndex: ++zIndex,
283
+ render: content,
284
+ duration: (_opts$duration = opts.duration) !== null && _opts$duration !== void 0 ? _opts$duration : DEFAULTS.duration,
285
+ showIcon: (_opts$showIcon = opts.showIcon) !== null && _opts$showIcon !== void 0 ? _opts$showIcon : DEFAULTS.showIcon,
286
+ action: 'add'
287
+ }, opts);
288
+ }
289
+ function toast(content, options) {
290
+ if (content == null) return;
291
+ notify(createToast('normal', content, options));
292
+ }
293
+ toast.success = function (content, options) {
294
+ if (content == null) return;
295
+ notify(createToast('success', content, options));
296
+ };
297
+ toast.error = function (content, options) {
298
+ if (content == null) return;
299
+ notify(createToast('error', content, options));
300
+ };
301
+ toast.info = function (content, options) {
302
+ if (content == null) return;
303
+ notify(createToast('info', content, options));
304
+ };
305
+ toast.custom = function (content, options) {
306
+ if (content == null) return;
307
+ notify(createToast('custom', content, options));
308
+ };
309
+ toast.dismiss = function (id) {
310
+ notify({
311
+ action: 'dismiss',
312
+ id: id
153
313
  });
154
314
  };
155
- toast.error = function () {
156
- var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'Toast denied';
157
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
158
- var id = Date.now();
159
- options = Object.assign({
160
- duration: 2800,
315
+ toast.update = function (id, content) {
316
+ var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
317
+ if (!id) return;
318
+ notify(_objectSpread2(_objectSpread2({}, options), {}, {
161
319
  id: id,
162
- style: {},
163
- showIcon: true
164
- }, options);
165
- addToast({
166
- type: 'error',
167
- text: text,
168
- options: options
169
- });
320
+ render: content,
321
+ action: 'update'
322
+ }));
170
323
  };
171
- toast.promise = function (callback) {
172
- var msgs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
324
+ var loading = function loading(content) {
325
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
326
+ notify(createToast('loading', content, _objectSpread2(_objectSpread2({}, options), {}, {
327
+ duration: Infinity
328
+ })));
329
+ };
330
+ toast.promise = function (promise, handlers) {
331
+ var _options$id;
173
332
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
174
- var id = Date.now();
175
- options = Object.assign({
176
- duration: 2800,
333
+ if (!promise) return;
334
+ if (!handlers) return;
335
+ if (!handlers.loading) return;
336
+ if (!handlers.success) return;
337
+ if (!handlers.error) return;
338
+ var id = (_options$id = options.id) !== null && _options$id !== void 0 ? _options$id : genId();
339
+ loading(handlers.loading, _objectSpread2(_objectSpread2({}, options), {}, {
177
340
  id: id,
178
- style: {},
179
- showIcon: true
180
- }, options);
181
- msgs = Object.assign({
182
- loading: 'Saving...',
183
- success: 'Success',
184
- error: 'Something went wrong'
185
- }, msgs);
186
- addToast({
187
- type: 'loading',
188
- text: msgs.loading,
189
- options: _objectSpread2(_objectSpread2({}, options), {}, {
190
- duration: Infinity,
191
- id: id
192
- })
341
+ duration: Infinity
342
+ }));
343
+ promise.then(/*#__PURE__*/function () {
344
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(result) {
345
+ var _options$duration;
346
+ var _t, _t2;
347
+ return _regenerator().w(function (_context) {
348
+ while (1) switch (_context.n) {
349
+ case 0:
350
+ _t = toast;
351
+ _t2 = id;
352
+ _context.n = 1;
353
+ return handlers.success(result);
354
+ case 1:
355
+ _t.update.call(_t, _t2, _context.v, {
356
+ type: 'success',
357
+ duration: (_options$duration = options.duration) !== null && _options$duration !== void 0 ? _options$duration : 2800
358
+ });
359
+ return _context.a(2, result);
360
+ }
361
+ }, _callee);
362
+ }));
363
+ return function (_x) {
364
+ return _ref.apply(this, arguments);
365
+ };
366
+ }())["catch"](/*#__PURE__*/function () {
367
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(error) {
368
+ var _options$duration2;
369
+ var _t3, _t4;
370
+ return _regenerator().w(function (_context2) {
371
+ while (1) switch (_context2.n) {
372
+ case 0:
373
+ _t3 = toast;
374
+ _t4 = id;
375
+ _context2.n = 1;
376
+ return handlers.error(error);
377
+ case 1:
378
+ _t3.update.call(_t3, _t4, _context2.v, {
379
+ type: 'error',
380
+ duration: (_options$duration2 = options.duration) !== null && _options$duration2 !== void 0 ? _options$duration2 : 3800
381
+ });
382
+ throw error;
383
+ case 2:
384
+ return _context2.a(2);
385
+ }
386
+ }, _callee2);
387
+ }));
388
+ return function (_x2) {
389
+ return _ref2.apply(this, arguments);
390
+ };
391
+ }());
392
+ return promise;
393
+ };
394
+
395
+ var timeouts = new Map();
396
+ var LEAVE_DELAY = 600;
397
+ var MIN_VISIBLE = 700;
398
+ function clearToastTimers(id) {
399
+ var t = timeouts.get(id);
400
+ if (!t) return;
401
+ clearTimeout(t.leaving);
402
+ clearTimeout(t.left);
403
+ timeouts["delete"](id);
404
+ }
405
+ function getDurations(duration) {
406
+ var d = Number(duration);
407
+ if (!isFinite(d)) return null;
408
+ var visible = Math.max(MIN_VISIBLE, d);
409
+ return {
410
+ leaving: visible,
411
+ left: visible + LEAVE_DELAY
412
+ };
413
+ }
414
+ function armToastTimers(toast, setToasts) {
415
+ var durations = getDurations(toast.duration);
416
+ if (!durations) return;
417
+ clearToastTimers(toast.id);
418
+ var leaving = setTimeout(function () {
419
+ setToasts(function (prev) {
420
+ return prev.map(function (t) {
421
+ return t.id === toast.id ? _objectSpread2(_objectSpread2({}, t), {}, {
422
+ status: 'leaving'
423
+ }) : t;
424
+ });
425
+ });
426
+ }, durations.leaving);
427
+ var left = setTimeout(function () {
428
+ setToasts(function (prev) {
429
+ return prev.filter(function (t) {
430
+ return t.id !== toast.id;
431
+ });
432
+ });
433
+ timeouts["delete"](toast.id);
434
+ }, durations.left);
435
+ timeouts.set(toast.id, {
436
+ leaving: leaving,
437
+ left: left
193
438
  });
194
- callback.then(function (res) {
195
- var successMsg = typeof msgs.success === 'function' ? msgs.success(res) : msgs.success;
196
- addToast({
197
- type: 'success',
198
- text: successMsg,
199
- options: options
439
+ }
440
+ function manageToasts(toast, setToasts) {
441
+ // dismiss
442
+ if (toast.action === 'dismiss') {
443
+ if (!toast.id) {
444
+ setToasts(function (prev) {
445
+ return prev.map(function (t) {
446
+ return _objectSpread2(_objectSpread2({}, t), {}, {
447
+ status: 'leaving'
448
+ });
449
+ });
450
+ });
451
+ setTimeout(function () {
452
+ setToasts([]);
453
+ }, LEAVE_DELAY);
454
+ timeouts.forEach(function (_ref) {
455
+ var leaving = _ref.leaving,
456
+ left = _ref.left;
457
+ clearTimeout(leaving);
458
+ clearTimeout(left);
459
+ });
460
+ timeouts.clear();
461
+ return;
462
+ }
463
+ clearToastTimers(toast.id);
464
+ setToasts(function (prev) {
465
+ return prev.map(function (t) {
466
+ return t.id === toast.id ? _objectSpread2(_objectSpread2({}, t), {}, {
467
+ status: 'leaving'
468
+ }) : t;
469
+ });
200
470
  });
201
- return res;
202
- })["catch"](function (err) {
203
- var normalizedError = err instanceof Error ? err : new Error(String(err));
204
- var errorMsg = typeof msgs.error === 'function' ? msgs.error(normalizedError) : msgs.error;
205
- addToast({
206
- type: 'error',
207
- text: errorMsg,
208
- options: options
471
+ setTimeout(function () {
472
+ setToasts(function (prev) {
473
+ return prev.filter(function (t) {
474
+ return t.id !== toast.id;
475
+ });
476
+ });
477
+ }, LEAVE_DELAY);
478
+ return;
479
+ }
480
+
481
+ // update
482
+ if (toast.action === 'update') {
483
+ setToasts(function (prev) {
484
+ var prevToast = prev.find(function (t) {
485
+ return t.id === toast.id;
486
+ });
487
+ if (!prevToast) return prev;
488
+ if (prevToast.type !== 'custom' && prevToast.type !== 'loading') return prev;
489
+ return prev.map(function (t) {
490
+ return t.id === toast.id ? _objectSpread2(_objectSpread2(_objectSpread2({}, t), toast), {}, {
491
+ status: 'visible'
492
+ }) : t;
493
+ });
209
494
  });
210
- return Promise.reject(normalizedError);
495
+ armToastTimers(toast, setToasts);
496
+ return;
497
+ }
498
+
499
+ // add
500
+ var enteringToast = _objectSpread2(_objectSpread2({}, toast), {}, {
501
+ status: 'entering'
211
502
  });
212
- };
213
- toast.custom = function (render) {
214
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
215
- var id = Date.now();
216
- options = Object.assign({
217
- duration: 3000,
218
- id: id,
219
- exitDelay: 50
220
- }, options);
221
- addToast({
222
- type: 'custom',
223
- render: render,
224
- options: options
503
+ setToasts(function (prev) {
504
+ var prevToast = prev.find(function (t) {
505
+ return t.id === enteringToast.id;
506
+ });
507
+ if (prevToast) return prev;
508
+ return [enteringToast].concat(_toConsumableArray(prev));
225
509
  });
226
- return id;
227
- };
228
- function dismiss(id, exitDelay) {
229
- addToast({
230
- type: 'dismiss',
231
- id: id,
232
- exitDelay: exitDelay
510
+ requestAnimationFrame(function () {
511
+ setToasts(function (prev) {
512
+ return prev.map(function (t) {
513
+ return t.id === toast.id ? _objectSpread2(_objectSpread2({}, t), {}, {
514
+ status: 'visible'
515
+ }) : t;
516
+ });
517
+ });
233
518
  });
519
+ armToastTimers(enteringToast, setToasts);
234
520
  }
235
521
 
236
522
  function SuccessSvg() {
@@ -272,237 +558,169 @@ function ErrorSvg() {
272
558
  function LoadingSvg() {
273
559
  return /*#__PURE__*/React.createElement("div", {
274
560
  className: "promise-svg-container"
561
+ }, /*#__PURE__*/React.createElement("div", {
562
+ className: "loader"
563
+ }));
564
+ }
565
+ function InfoSvg() {
566
+ return /*#__PURE__*/React.createElement("div", {
567
+ className: "svg-container info"
275
568
  }, /*#__PURE__*/React.createElement("svg", {
276
569
  xmlns: "http://www.w3.org/2000/svg",
277
- width: "24",
278
- height: "24",
279
570
  viewBox: "0 0 24 24",
280
- fill: "none",
281
- stroke: "currentColor",
282
- strokeWidth: "2",
283
- strokeLinecap: "round",
284
- strokeLinejoin: "round"
571
+ width: "16",
572
+ height: "16",
573
+ fill: "currentColor"
285
574
  }, /*#__PURE__*/React.createElement("path", {
286
- d: "M21 12a9 9 0 1 1-6.219-8.56"
575
+ d: "M12 6C12.8284 6 13.5 5.32843 13.5 4.5C13.5 3.67157 12.8284 3 12 3C11.1716 3 10.5 3.67157 10.5 4.5C10.5 5.32843 11.1716 6 12 6ZM9 10H11V18H9V20H15V18H13V8H9V10Z"
287
576
  })));
288
577
  }
289
578
 
290
579
  function Toast(_ref) {
291
- var toast = _ref.toast,
292
- setToasts = _ref.setToasts,
293
- position = _ref.position,
294
- gap = _ref.gap;
295
- var id = toast.id,
296
- leaving = toast.leaving,
297
- offset = toast.offset,
298
- text = toast.text,
299
- type = toast.type,
300
- options = toast.options;
301
- var style = options.style,
302
- showIcon = options.showIcon;
580
+ var t = _ref.t,
581
+ containerPosition = _ref.containerPosition,
582
+ gap = _ref.gap,
583
+ setToasts = _ref.setToasts;
584
+ var id = t.id,
585
+ type = t.type,
586
+ render = t.render,
587
+ action = t.action,
588
+ status = t.status;
303
589
  var ref = useRef(null);
304
590
  useLayoutEffect(function () {
305
591
  if (!ref.current) return;
306
- var height = ref.current.getBoundingClientRect().height;
307
- var totalHeight = height + gap;
308
- setToasts(function (prev) {
309
- return prev.map(function (t) {
310
- return t.id === id ? _objectSpread2(_objectSpread2({}, t), {}, {
311
- height: totalHeight
312
- }) : t;
592
+ var g = typeof gap === 'number' ? gap : Number.isFinite(Number(gap)) ? Number(gap) : 8;
593
+ var el = ref.current;
594
+ var observer = new ResizeObserver(function () {
595
+ var height = el.offsetHeight + g;
596
+ setToasts(function (prev) {
597
+ return prev.map(function (t) {
598
+ return t.id === id ? _objectSpread2(_objectSpread2({}, t), {}, {
599
+ height: height
600
+ }) : t;
601
+ });
313
602
  });
314
603
  });
604
+ observer.observe(el);
605
+ return function () {
606
+ return observer.disconnect();
607
+ };
315
608
  }, []);
316
- var transformY = position.includes('bottom') ? "translateY(-".concat(offset || 0, "px)") : "translateY(".concat(offset || 0, "px)");
317
- function renderCustomContent() {
318
- var render = toast.render;
319
- if (typeof render === 'function') {
320
- return render(function () {
321
- return dismiss(toast.id, toast.options.exitDelay);
322
- });
323
- }
324
- if (typeof render === 'string') {
325
- return /*#__PURE__*/React.createElement("span", null, render);
326
- }
327
- return render;
328
- }
609
+ var allowedPositions = ['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'];
610
+ var userPosition = t.position || containerPosition;
611
+ var position = allowedPositions.includes(userPosition) ? userPosition : 'top-center';
329
612
  return /*#__PURE__*/React.createElement("div", {
330
613
  ref: ref,
331
- style: _objectSpread2({
332
- maxHeight: 'fit-content',
333
- transform: transformY
334
- }, getToastPosition(position))
614
+ style: getToastStyles(t, position)
335
615
  }, /*#__PURE__*/React.createElement("div", {
336
- className: "toast-content".concat(position.includes('bottom') ? '-bottom' : '', " ").concat(leaving ? 'exit' : '')
337
- }, type === 'custom' ? renderCustomContent() : /*#__PURE__*/React.createElement("div", {
338
- style: _objectSpread2({}, style),
339
- className: "pre-styled"
340
- }, showIcon && /*#__PURE__*/React.createElement(React.Fragment, null, type === 'loading' && /*#__PURE__*/React.createElement(LoadingSvg, null), type === 'success' && /*#__PURE__*/React.createElement(SuccessSvg, null), type === 'error' && /*#__PURE__*/React.createElement(ErrorSvg, null)), /*#__PURE__*/React.createElement("span", null, text))));
341
- }
342
- function getToastPosition(position) {
343
- var isLeft = position.includes('left');
344
- var isRight = position.includes('right');
616
+ style: {
617
+ pointerEvents: 'all',
618
+ display: 'flex',
619
+ alignItems: 'center',
620
+ gap: 8
621
+ },
622
+ className: "kitzo-toast type-".concat(type, " action-").concat(action, " status-").concat(status, " pos-y-").concat(position.split('-')[0], " pos-x-").concat(position.split('-')[1], " ")
623
+ }, t.showIcon && type !== 'normal' && type !== 'custom' && /*#__PURE__*/React.createElement("div", {
624
+ style: {
625
+ flexShrink: 0
626
+ }
627
+ }, type === 'loading' && /*#__PURE__*/React.createElement(LoadingSvg, null), type === 'success' && /*#__PURE__*/React.createElement(SuccessSvg, null), type === 'error' && /*#__PURE__*/React.createElement(ErrorSvg, null), type === 'info' && /*#__PURE__*/React.createElement(InfoSvg, null)), /*#__PURE__*/React.createElement("div", {
628
+ style: {
629
+ flex: 1
630
+ }
631
+ }, typeof render === 'function' ? render(function () {
632
+ return toast.dismiss(id);
633
+ }) : render)));
634
+ }
635
+ function getToastStyles(t, position) {
345
636
  var styles = {
346
637
  position: 'absolute',
638
+ zIndex: t.zIndex,
639
+ left: 0,
347
640
  width: '100%',
348
- pointerEvents: 'none',
349
- transition: 'transform 230ms',
641
+ transition: 'transform 260ms',
350
642
  display: 'flex'
351
643
  };
352
- if (position.includes('top')) {
353
- styles.top = '0';
354
- styles.justifyContent = isLeft ? 'flex-start' : isRight ? 'flex-end' : 'center';
355
- }
356
- if (position.includes('bottom')) {
357
- styles.bottom = '0';
358
- styles.justifyContent = isLeft ? 'flex-start' : isRight ? 'flex-end' : 'center';
359
- }
644
+ var transform = position.includes('top') ? "translateY(".concat(t.distance || 0, "px)") : "translateY(-".concat(t.distance || 0, "px)");
645
+ var justifyContent = position.includes('left') ? 'flex-start' : position.includes('center') ? 'center' : 'flex-end';
646
+ styles.transform = transform;
647
+ styles.justifyContent = justifyContent;
648
+ if (position.includes('top')) styles.top = 0;
649
+ if (position.includes('bottom')) styles.bottom = 0;
360
650
  return styles;
361
651
  }
362
652
 
363
- function ToastContainer(props) {
364
- props = Object.assign({
365
- position: 'top-center',
366
- gap: 8
367
- }, props);
368
- var _props = props,
369
- gap = _props.gap;
370
- var position = props.position;
653
+ function ToastContainer(_ref) {
654
+ var _ref$position = _ref.position,
655
+ position = _ref$position === void 0 ? 'top-center' : _ref$position,
656
+ _ref$gap = _ref.gap,
657
+ gap = _ref$gap === void 0 ? 8 : _ref$gap,
658
+ _ref$edgeOffset = _ref.edgeOffset,
659
+ edgeOffset = _ref$edgeOffset === void 0 ? 16 : _ref$edgeOffset,
660
+ _ref$isDark = _ref.isDark,
661
+ isDark = _ref$isDark === void 0 ? window.matchMedia('(prefers-color-scheme: dark)').matches : _ref$isDark;
371
662
  var _useState = useState([]),
372
663
  _useState2 = _slicedToArray(_useState, 2),
373
664
  toasts = _useState2[0],
374
665
  setToasts = _useState2[1];
375
-
376
- // event listener
377
666
  useEffect(function () {
378
667
  var unsub = subscribe(function (toast) {
379
- if (toast.type === 'dismiss') {
380
- setTimeout(function () {
381
- setToasts(function (prev) {
382
- return prev.map(function (t) {
383
- return t.options.id === toast.id ? _objectSpread2(_objectSpread2({}, t), {}, {
384
- leaving: true
385
- }) : t;
386
- });
387
- });
388
- }, Math.max(0, Number(toast.exitDelay)));
389
- setTimeout(function () {
390
- setToasts(function (prev) {
391
- return prev.filter(function (t) {
392
- return t.options.id !== toast.id;
393
- });
394
- });
395
- }, Math.max(0, Number(toast.exitDelay) + 300));
396
- return;
397
- }
398
- var duration = toast.options.duration;
399
- var id = toast.options.id;
400
- setToasts(function (prev) {
401
- var exists = prev.some(function (t) {
402
- return t.options.id === id;
403
- });
404
- if (exists) {
405
- return prev.map(function (t) {
406
- return t.options.id === id ? _objectSpread2(_objectSpread2({}, t), toast) : t;
407
- });
408
- }
409
- return [_objectSpread2(_objectSpread2({
410
- id: id
411
- }, toast), {}, {
412
- offset: 0,
413
- height: 0,
414
- leaving: false
415
- })].concat(_toConsumableArray(prev));
416
- });
417
- if (toast.type !== 'loading' && isFinite(duration)) {
418
- setTimeout(function () {
419
- setToasts(function (prev) {
420
- return prev.map(function (t) {
421
- return t.options.id === id ? _objectSpread2(_objectSpread2({}, t), {}, {
422
- leaving: true
423
- }) : t;
424
- });
425
- });
426
- }, Math.max(0, duration - 300));
427
- setTimeout(function () {
428
- setToasts(function (prev) {
429
- return prev.filter(function (t) {
430
- return t.options.id !== id;
431
- });
432
- });
433
- }, duration);
434
- }
668
+ return manageToasts(toast, setToasts);
435
669
  });
436
- return function () {
437
- return unsub();
438
- };
670
+ return unsub;
439
671
  }, []);
440
-
441
- // measue height and offset for each toast
442
672
  useLayoutEffect(function () {
443
- var grouped = toasts.reduce(function (acc, t) {
444
- var pos = t.options.position || position || 'top-center';
445
- (acc[pos] || (acc[pos] = [])).push(t);
446
- return acc;
673
+ var grouped = toasts.reduce(function (prev, t) {
674
+ var pos = t.position || position || 'top-center';
675
+ (prev[pos] || (prev[pos] = [])).push(t);
676
+ return prev;
447
677
  }, {});
448
678
  var hasChanged = false;
449
679
  var updated = toasts.map(function (toast) {
450
- var pos = toast.options.position || position || 'top-center';
680
+ var pos = toast.position || position || 'top-center';
451
681
  var group = grouped[pos];
452
682
  var index = group.findIndex(function (t) {
453
683
  return t.id === toast.id;
454
684
  });
455
- var offset = group.slice(0, index).reduce(function (acc, t) {
685
+ var distance = group.slice(0, index).reduce(function (acc, t) {
456
686
  return acc + (t.height || 0);
457
687
  }, 0);
458
- if (offset !== toast.offset) {
688
+ if (distance !== toast.distance) {
459
689
  hasChanged = true;
460
690
  }
461
691
  return _objectSpread2(_objectSpread2({}, toast), {}, {
462
- offset: offset
692
+ distance: distance
463
693
  });
464
694
  });
465
695
  if (hasChanged) {
466
- setToasts(updated);
696
+ setTimeout(function () {
697
+ setToasts(updated);
698
+ }, 30);
467
699
  }
468
- }, [toasts]);
469
- var positions = ['top-left', 'top-center', 'top-right', 'bottom-left', 'bottom-center', 'bottom-right'];
700
+ }, [toasts, position]);
470
701
  return /*#__PURE__*/React.createElement("div", {
471
702
  style: {
472
703
  position: 'fixed',
473
704
  inset: 0,
474
- zIndex: 100000000,
705
+ zIndex: 2147483647,
475
706
  pointerEvents: 'none',
476
- fontFamily: 'inherit',
477
- boxSizing: 'border-box'
707
+ display: 'grid',
708
+ padding: edgeOffset != null ? edgeOffset : 16
709
+ },
710
+ className: "kitzo-toast-container ".concat(isDark ? 'kitzo-toast-dark' : '')
711
+ }, /*#__PURE__*/React.createElement("div", {
712
+ style: {
713
+ position: 'relative'
478
714
  }
479
- }, positions.map(function (pos) {
480
- var group = toasts.filter(function (t) {
481
- return (t.options.position || position || 'top-center') === pos;
715
+ }, toasts.map(function (t) {
716
+ return /*#__PURE__*/React.createElement(Toast, {
717
+ key: t.id,
718
+ t: t,
719
+ containerPosition: position,
720
+ gap: gap,
721
+ setToasts: setToasts
482
722
  });
483
- if (!group.length) return null;
484
- return /*#__PURE__*/React.createElement("div", {
485
- key: pos,
486
- style: {
487
- position: 'absolute',
488
- inset: 0,
489
- display: 'grid',
490
- padding: '1rem'
491
- }
492
- }, /*#__PURE__*/React.createElement("div", {
493
- style: {
494
- position: 'relative'
495
- }
496
- }, group.map(function (toast) {
497
- return /*#__PURE__*/React.createElement(Toast, {
498
- key: toast.options.id,
499
- toast: toast,
500
- setToasts: setToasts,
501
- position: pos,
502
- gap: typeof gap === 'string' ? isNaN(+gap) ? 8 : +gap : gap
503
- });
504
- })));
505
- }));
723
+ })));
506
724
  }
507
725
 
508
726
  // import './style.css';