kitzo 2.0.0 → 2.0.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.
- package/README.md +1 -1
- package/dist/react.esm.js +239 -156
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ npm i kitzo
|
|
|
25
25
|
or
|
|
26
26
|
|
|
27
27
|
```javascript
|
|
28
|
-
<script src="https://cdn.jsdelivr.net/npm/kitzo@2.0.
|
|
28
|
+
<script src="https://cdn.jsdelivr.net/npm/kitzo@2.0.1/dist/kitzo.umd.min.js"></script>
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
> Attach this script tag in the html head tag and you are good to go.
|
package/dist/react.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useRef, useLayoutEffect, useState, useEffect } from 'react';
|
|
1
|
+
import React, { useRef, useLayoutEffect, useState, useEffect } from 'react';
|
|
2
2
|
|
|
3
3
|
const listeners = new Set();
|
|
4
4
|
let isStyleAdded = false;
|
|
@@ -187,7 +187,7 @@ function toastStyles() {
|
|
|
187
187
|
`;
|
|
188
188
|
}
|
|
189
189
|
function addToast(toast) {
|
|
190
|
-
listeners.forEach(callback => {
|
|
190
|
+
listeners.forEach((callback) => {
|
|
191
191
|
callback(toast);
|
|
192
192
|
});
|
|
193
193
|
}
|
|
@@ -203,159 +203,222 @@ function subscribe(callback) {
|
|
|
203
203
|
}
|
|
204
204
|
function success(text = 'Toast success', options = {}) {
|
|
205
205
|
const id = Date.now();
|
|
206
|
-
options = Object.assign(
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
206
|
+
options = Object.assign(
|
|
207
|
+
{
|
|
208
|
+
duration: 2000,
|
|
209
|
+
id,
|
|
210
|
+
},
|
|
211
|
+
options
|
|
212
|
+
);
|
|
210
213
|
addToast({
|
|
211
214
|
type: 'success',
|
|
212
215
|
text,
|
|
213
|
-
options
|
|
216
|
+
options,
|
|
214
217
|
});
|
|
215
218
|
}
|
|
216
219
|
function error(text = 'Toast denied', options = {}) {
|
|
217
220
|
const id = Date.now();
|
|
218
|
-
options = Object.assign(
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
221
|
+
options = Object.assign(
|
|
222
|
+
{
|
|
223
|
+
duration: 2000,
|
|
224
|
+
id,
|
|
225
|
+
},
|
|
226
|
+
options
|
|
227
|
+
);
|
|
222
228
|
addToast({
|
|
223
229
|
type: 'error',
|
|
224
230
|
text,
|
|
225
|
-
options
|
|
231
|
+
options,
|
|
226
232
|
});
|
|
227
233
|
}
|
|
228
234
|
function promise(callback, msgs = {}, options = {}) {
|
|
229
235
|
const id = Date.now();
|
|
230
|
-
options = Object.assign(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
236
|
+
options = Object.assign(
|
|
237
|
+
{
|
|
238
|
+
duration: 2000,
|
|
239
|
+
id,
|
|
240
|
+
},
|
|
241
|
+
options
|
|
242
|
+
);
|
|
243
|
+
msgs = Object.assign(
|
|
244
|
+
{
|
|
245
|
+
loading: 'Saving...',
|
|
246
|
+
success: 'Success',
|
|
247
|
+
error: 'Something went wrong',
|
|
248
|
+
},
|
|
249
|
+
msgs
|
|
250
|
+
);
|
|
239
251
|
addToast({
|
|
240
252
|
type: 'loading',
|
|
241
253
|
text: msgs.loading,
|
|
242
254
|
options: {
|
|
243
255
|
...options,
|
|
244
256
|
duration: Infinity,
|
|
245
|
-
id
|
|
246
|
-
}
|
|
257
|
+
id,
|
|
258
|
+
},
|
|
247
259
|
});
|
|
248
|
-
callback
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
260
|
+
callback
|
|
261
|
+
.then((res) => {
|
|
262
|
+
const successMsg = typeof msgs.success === 'function' ? msgs.success(res) : msgs.success;
|
|
263
|
+
addToast({
|
|
264
|
+
type: 'success',
|
|
265
|
+
text: successMsg,
|
|
266
|
+
options,
|
|
267
|
+
});
|
|
268
|
+
return res;
|
|
269
|
+
})
|
|
270
|
+
.catch((err) => {
|
|
271
|
+
const normalizedError = err instanceof Error ? err : new Error(String(err));
|
|
272
|
+
const errorMsg = typeof msgs.error === 'function' ? msgs.error(normalizedError) : msgs.error;
|
|
273
|
+
addToast({
|
|
274
|
+
type: 'error',
|
|
275
|
+
text: errorMsg,
|
|
276
|
+
options,
|
|
277
|
+
});
|
|
278
|
+
return Promise.reject(normalizedError);
|
|
263
279
|
});
|
|
264
|
-
return Promise.reject(normalizedError);
|
|
265
|
-
});
|
|
266
280
|
}
|
|
267
281
|
|
|
268
282
|
function SuccessSvg() {
|
|
269
|
-
return /*#__PURE__*/React.createElement(
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
283
|
+
return /*#__PURE__*/ React.createElement(
|
|
284
|
+
'div',
|
|
285
|
+
{
|
|
286
|
+
className: 'svg-container success',
|
|
287
|
+
},
|
|
288
|
+
/*#__PURE__*/ React.createElement(
|
|
289
|
+
'svg',
|
|
290
|
+
{
|
|
291
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
292
|
+
width: '24',
|
|
293
|
+
height: '24',
|
|
294
|
+
viewBox: '0 0 24 24',
|
|
295
|
+
fill: 'none',
|
|
296
|
+
stroke: 'currentColor',
|
|
297
|
+
strokeWidth: '2',
|
|
298
|
+
strokeLinecap: 'round',
|
|
299
|
+
strokeLinejoin: 'round',
|
|
300
|
+
},
|
|
301
|
+
/*#__PURE__*/ React.createElement('path', {
|
|
302
|
+
d: 'M20 6 9 17l-5-5',
|
|
303
|
+
})
|
|
304
|
+
)
|
|
305
|
+
);
|
|
284
306
|
}
|
|
285
307
|
function ErrorSvg() {
|
|
286
|
-
return /*#__PURE__*/React.createElement(
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
308
|
+
return /*#__PURE__*/ React.createElement(
|
|
309
|
+
'div',
|
|
310
|
+
{
|
|
311
|
+
className: 'svg-container error',
|
|
312
|
+
},
|
|
313
|
+
/*#__PURE__*/ React.createElement(
|
|
314
|
+
'svg',
|
|
315
|
+
{
|
|
316
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
317
|
+
width: '24',
|
|
318
|
+
height: '24',
|
|
319
|
+
viewBox: '0 0 24 24',
|
|
320
|
+
fill: 'none',
|
|
321
|
+
stroke: 'currentColor',
|
|
322
|
+
strokeWidth: '2',
|
|
323
|
+
strokeLinecap: 'round',
|
|
324
|
+
strokeLinejoin: 'round',
|
|
325
|
+
},
|
|
326
|
+
/*#__PURE__*/ React.createElement('path', {
|
|
327
|
+
d: 'M18 6 6 18',
|
|
328
|
+
}),
|
|
329
|
+
/*#__PURE__*/ React.createElement('path', {
|
|
330
|
+
d: 'm6 6 12 12',
|
|
331
|
+
})
|
|
332
|
+
)
|
|
333
|
+
);
|
|
303
334
|
}
|
|
304
335
|
function LoadingSvg() {
|
|
305
|
-
return /*#__PURE__*/React.createElement(
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
336
|
+
return /*#__PURE__*/ React.createElement(
|
|
337
|
+
'div',
|
|
338
|
+
{
|
|
339
|
+
className: 'promise-svg-container',
|
|
340
|
+
},
|
|
341
|
+
/*#__PURE__*/ React.createElement(
|
|
342
|
+
'svg',
|
|
343
|
+
{
|
|
344
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
345
|
+
width: '24',
|
|
346
|
+
height: '24',
|
|
347
|
+
viewBox: '0 0 24 24',
|
|
348
|
+
fill: 'none',
|
|
349
|
+
stroke: 'currentColor',
|
|
350
|
+
strokeWidth: '2',
|
|
351
|
+
strokeLinecap: 'round',
|
|
352
|
+
strokeLinejoin: 'round',
|
|
353
|
+
},
|
|
354
|
+
/*#__PURE__*/ React.createElement('path', {
|
|
355
|
+
d: 'M21 12a9 9 0 1 1-6.219-8.56',
|
|
356
|
+
})
|
|
357
|
+
)
|
|
358
|
+
);
|
|
320
359
|
}
|
|
321
360
|
|
|
322
361
|
const GAP = 10;
|
|
323
|
-
function Toast({
|
|
324
|
-
toast,
|
|
325
|
-
setToasts,
|
|
326
|
-
position
|
|
327
|
-
}) {
|
|
362
|
+
function Toast({ toast, setToasts, position }) {
|
|
328
363
|
const ref = useRef(null);
|
|
329
364
|
useLayoutEffect(() => {
|
|
330
365
|
if (!ref.current) return;
|
|
331
366
|
const h = ref.current.getBoundingClientRect().height;
|
|
332
|
-
setToasts(prev =>
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
367
|
+
setToasts((prev) =>
|
|
368
|
+
prev.map((t) =>
|
|
369
|
+
t.options.id === toast.id
|
|
370
|
+
? t
|
|
371
|
+
: {
|
|
372
|
+
...t,
|
|
373
|
+
offset: t.offset + h + GAP,
|
|
374
|
+
}
|
|
375
|
+
)
|
|
376
|
+
);
|
|
336
377
|
}, [setToasts, toast.id]);
|
|
337
378
|
if (position.includes('bottom')) {
|
|
338
|
-
return /*#__PURE__*/React.createElement(
|
|
379
|
+
return /*#__PURE__*/ React.createElement(
|
|
380
|
+
'div',
|
|
381
|
+
{
|
|
382
|
+
ref: ref,
|
|
383
|
+
className: 'toast',
|
|
384
|
+
style: {
|
|
385
|
+
transform: `translateY(-${toast.offset}px)`,
|
|
386
|
+
...getToastPosition(position),
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
/*#__PURE__*/ React.createElement(
|
|
390
|
+
'div',
|
|
391
|
+
{
|
|
392
|
+
className: `toast-content-bottom ${toast.leaving ? 'exit' : ''}`,
|
|
393
|
+
},
|
|
394
|
+
toast.type === 'loading' && /*#__PURE__*/ React.createElement(LoadingSvg, null),
|
|
395
|
+
toast.type === 'success' && /*#__PURE__*/ React.createElement(SuccessSvg, null),
|
|
396
|
+
toast.type === 'error' && /*#__PURE__*/ React.createElement(ErrorSvg, null),
|
|
397
|
+
/*#__PURE__*/ React.createElement('span', null, toast.text)
|
|
398
|
+
)
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
return /*#__PURE__*/ React.createElement(
|
|
402
|
+
'div',
|
|
403
|
+
{
|
|
339
404
|
ref: ref,
|
|
340
|
-
className:
|
|
405
|
+
className: 'toast',
|
|
341
406
|
style: {
|
|
342
|
-
transform: `translateY(
|
|
343
|
-
...getToastPosition(position)
|
|
344
|
-
}
|
|
345
|
-
},
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
className: `toast-content ${toast.leaving ? 'exit' : ''}`
|
|
358
|
-
}, toast.type === 'loading' && /*#__PURE__*/React.createElement(LoadingSvg, null), toast.type === 'success' && /*#__PURE__*/React.createElement(SuccessSvg, null), toast.type === 'error' && /*#__PURE__*/React.createElement(ErrorSvg, null), /*#__PURE__*/React.createElement("span", null, toast.text)));
|
|
407
|
+
transform: `translateY(${toast.offset}px)`,
|
|
408
|
+
...getToastPosition(position),
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
/*#__PURE__*/ React.createElement(
|
|
412
|
+
'div',
|
|
413
|
+
{
|
|
414
|
+
className: `toast-content ${toast.leaving ? 'exit' : ''}`,
|
|
415
|
+
},
|
|
416
|
+
toast.type === 'loading' && /*#__PURE__*/ React.createElement(LoadingSvg, null),
|
|
417
|
+
toast.type === 'success' && /*#__PURE__*/ React.createElement(SuccessSvg, null),
|
|
418
|
+
toast.type === 'error' && /*#__PURE__*/ React.createElement(ErrorSvg, null),
|
|
419
|
+
/*#__PURE__*/ React.createElement('span', null, toast.text)
|
|
420
|
+
)
|
|
421
|
+
);
|
|
359
422
|
}
|
|
360
423
|
function getToastPosition(position) {
|
|
361
424
|
const isLeft = position.includes('left');
|
|
@@ -365,7 +428,7 @@ function getToastPosition(position) {
|
|
|
365
428
|
width: 'calc(100% - 2rem)',
|
|
366
429
|
pointerEvents: 'none',
|
|
367
430
|
transition: 'transform 230ms',
|
|
368
|
-
display: 'flex'
|
|
431
|
+
display: 'flex',
|
|
369
432
|
};
|
|
370
433
|
if (position.includes('top')) {
|
|
371
434
|
styles.top = '1rem';
|
|
@@ -380,67 +443,87 @@ function getToastPosition(position) {
|
|
|
380
443
|
|
|
381
444
|
const EXIT_ANIM_MS = 300;
|
|
382
445
|
function ToastContainer(props) {
|
|
383
|
-
props = Object.assign(
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
446
|
+
props = Object.assign(
|
|
447
|
+
{
|
|
448
|
+
position: 'top-center',
|
|
449
|
+
},
|
|
450
|
+
props
|
|
451
|
+
);
|
|
452
|
+
const { position } = props;
|
|
389
453
|
const [toasts, setToasts] = useState([]);
|
|
390
454
|
useEffect(() => {
|
|
391
|
-
const unsub = subscribe(toast => {
|
|
455
|
+
const unsub = subscribe((toast) => {
|
|
392
456
|
const duration = toast.options.duration;
|
|
393
457
|
const id = toast.options.id;
|
|
394
|
-
setToasts(prev => {
|
|
395
|
-
const exists = prev.some(t => t.options.id === id);
|
|
458
|
+
setToasts((prev) => {
|
|
459
|
+
const exists = prev.some((t) => t.options.id === id);
|
|
396
460
|
if (exists) {
|
|
397
|
-
return prev.map(t =>
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
461
|
+
return prev.map((t) =>
|
|
462
|
+
t.options.id === id
|
|
463
|
+
? {
|
|
464
|
+
...t,
|
|
465
|
+
...toast,
|
|
466
|
+
}
|
|
467
|
+
: t
|
|
468
|
+
);
|
|
401
469
|
}
|
|
402
|
-
return [
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
470
|
+
return [
|
|
471
|
+
{
|
|
472
|
+
id,
|
|
473
|
+
...toast,
|
|
474
|
+
offset: 0,
|
|
475
|
+
leaving: false,
|
|
476
|
+
},
|
|
477
|
+
...prev,
|
|
478
|
+
];
|
|
408
479
|
});
|
|
409
480
|
if (toast.type !== 'loading') {
|
|
410
481
|
setTimeout(() => {
|
|
411
|
-
setToasts(prev =>
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
482
|
+
setToasts((prev) =>
|
|
483
|
+
prev.map((t) =>
|
|
484
|
+
t.options.id === id
|
|
485
|
+
? {
|
|
486
|
+
...t,
|
|
487
|
+
leaving: true,
|
|
488
|
+
}
|
|
489
|
+
: t
|
|
490
|
+
)
|
|
491
|
+
);
|
|
415
492
|
}, Math.max(0, duration - EXIT_ANIM_MS));
|
|
416
493
|
setTimeout(() => {
|
|
417
|
-
setToasts(prev => prev.filter(t => t.options.id !== id));
|
|
494
|
+
setToasts((prev) => prev.filter((t) => t.options.id !== id));
|
|
418
495
|
}, duration);
|
|
419
496
|
}
|
|
420
497
|
});
|
|
421
498
|
return () => unsub();
|
|
422
499
|
}, []);
|
|
423
|
-
return /*#__PURE__*/React.createElement(
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
500
|
+
return /*#__PURE__*/ React.createElement(
|
|
501
|
+
'div',
|
|
502
|
+
{
|
|
503
|
+
style: {
|
|
504
|
+
position: 'fixed',
|
|
505
|
+
inset: 0,
|
|
506
|
+
zIndex: 100000000,
|
|
507
|
+
pointerEvents: 'none',
|
|
508
|
+
fontFamily: 'inherit',
|
|
509
|
+
padding: '1rem',
|
|
510
|
+
},
|
|
511
|
+
},
|
|
512
|
+
toasts.map((t) =>
|
|
513
|
+
/*#__PURE__*/ React.createElement(Toast, {
|
|
514
|
+
key: t.options.id,
|
|
515
|
+
toast: t,
|
|
516
|
+
setToasts: setToasts,
|
|
517
|
+
position: position,
|
|
518
|
+
})
|
|
519
|
+
)
|
|
520
|
+
);
|
|
438
521
|
}
|
|
439
522
|
|
|
440
523
|
const toast = {
|
|
441
524
|
success,
|
|
442
525
|
error,
|
|
443
|
-
promise
|
|
526
|
+
promise,
|
|
444
527
|
};
|
|
445
528
|
|
|
446
529
|
export { ToastContainer, toast };
|