kitzo 2.0.13 → 2.0.15
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.d.ts +6 -1
- package/dist/react.esm.js +68 -55
- 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.15/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.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export interface ToastOptions {
|
|
2
2
|
duration?: number;
|
|
3
|
+
style?: React.CSSProperties;
|
|
4
|
+
showIcon?: true | false;
|
|
3
5
|
}
|
|
4
6
|
|
|
5
7
|
export interface ToastAPI {
|
|
@@ -17,4 +19,7 @@ export interface ToastAPI {
|
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export declare const toast: ToastAPI;
|
|
20
|
-
export declare function ToastContainer(props: {
|
|
22
|
+
export declare function ToastContainer(props: {
|
|
23
|
+
position?: 'top-left' | 'top-center' | 'top-right' | 'bottom-left' | 'bottom-center' | 'bottom-right';
|
|
24
|
+
gap?: number;
|
|
25
|
+
}): JSX.Element;
|
package/dist/react.esm.js
CHANGED
|
@@ -10,7 +10,7 @@ function toastStyles() {
|
|
|
10
10
|
.toast-content,
|
|
11
11
|
.toast-content-bottom {
|
|
12
12
|
pointer-events: all;
|
|
13
|
-
padding-inline: 0.
|
|
13
|
+
padding-inline: 0.825rem;
|
|
14
14
|
padding-block: 0.625rem;
|
|
15
15
|
display: flex;
|
|
16
16
|
align-items: center;
|
|
@@ -202,7 +202,9 @@ function success(text = 'Toast success', options = {}) {
|
|
|
202
202
|
const id = Date.now();
|
|
203
203
|
options = Object.assign({
|
|
204
204
|
duration: 2000,
|
|
205
|
-
id
|
|
205
|
+
id,
|
|
206
|
+
style: {},
|
|
207
|
+
showIcon: true
|
|
206
208
|
}, options);
|
|
207
209
|
addToast({
|
|
208
210
|
type: 'success',
|
|
@@ -214,7 +216,9 @@ function error(text = 'Toast denied', options = {}) {
|
|
|
214
216
|
const id = Date.now();
|
|
215
217
|
options = Object.assign({
|
|
216
218
|
duration: 2000,
|
|
217
|
-
id
|
|
219
|
+
id,
|
|
220
|
+
style: {},
|
|
221
|
+
showIcon: true
|
|
218
222
|
}, options);
|
|
219
223
|
addToast({
|
|
220
224
|
type: 'error',
|
|
@@ -226,7 +230,9 @@ function promise(callback, msgs = {}, options = {}) {
|
|
|
226
230
|
const id = Date.now();
|
|
227
231
|
options = Object.assign({
|
|
228
232
|
duration: 2000,
|
|
229
|
-
id
|
|
233
|
+
id,
|
|
234
|
+
style: {},
|
|
235
|
+
showIcon: true
|
|
230
236
|
}, options);
|
|
231
237
|
msgs = Object.assign({
|
|
232
238
|
loading: 'Saving...',
|
|
@@ -316,34 +322,34 @@ function LoadingSvg() {
|
|
|
316
322
|
})));
|
|
317
323
|
}
|
|
318
324
|
|
|
319
|
-
const GAP = 10;
|
|
320
325
|
function Toast({
|
|
321
326
|
toast,
|
|
322
327
|
setToasts,
|
|
323
|
-
position
|
|
328
|
+
position,
|
|
329
|
+
gap
|
|
324
330
|
}) {
|
|
331
|
+
const {
|
|
332
|
+
id,
|
|
333
|
+
leaving,
|
|
334
|
+
offset,
|
|
335
|
+
text,
|
|
336
|
+
type,
|
|
337
|
+
options
|
|
338
|
+
} = toast;
|
|
339
|
+
const {
|
|
340
|
+
style,
|
|
341
|
+
showIcon
|
|
342
|
+
} = options;
|
|
325
343
|
const ref = useRef(null);
|
|
344
|
+
console.log(toast);
|
|
326
345
|
useLayoutEffect(() => {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
newToasts[index] = {
|
|
335
|
-
...newToasts[index],
|
|
336
|
-
height
|
|
337
|
-
};
|
|
338
|
-
let offset = 0;
|
|
339
|
-
for (let t of newToasts) {
|
|
340
|
-
t.offset = offset;
|
|
341
|
-
offset += (t.height || 0) + GAP;
|
|
342
|
-
}
|
|
343
|
-
return newToasts;
|
|
344
|
-
});
|
|
345
|
-
}, [setToasts, toast.options.id]);
|
|
346
|
-
const transformY = position.includes('bottom') ? `translateY(-${toast.offset || 0}px)` : `translateY(${toast.offset || 0}px)`;
|
|
346
|
+
const height = ref.current.getBoundingClientRect().height + gap;
|
|
347
|
+
setToasts(prev => prev.map(t => t.id == id ? {
|
|
348
|
+
...t,
|
|
349
|
+
height
|
|
350
|
+
} : t));
|
|
351
|
+
}, []);
|
|
352
|
+
const transformY = position.includes('bottom') ? `translateY(-${offset || 0}px)` : `translateY(${offset || 0}px)`;
|
|
347
353
|
return /*#__PURE__*/React.createElement("div", {
|
|
348
354
|
ref: ref,
|
|
349
355
|
style: {
|
|
@@ -352,25 +358,28 @@ function Toast({
|
|
|
352
358
|
...getToastPosition(position)
|
|
353
359
|
}
|
|
354
360
|
}, /*#__PURE__*/React.createElement("div", {
|
|
355
|
-
|
|
356
|
-
|
|
361
|
+
style: {
|
|
362
|
+
...style
|
|
363
|
+
},
|
|
364
|
+
className: `toast-content${position.includes('bottom') ? '-bottom' : ''} ${leaving ? 'exit' : ''}`
|
|
365
|
+
}, 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)));
|
|
357
366
|
}
|
|
358
367
|
function getToastPosition(position) {
|
|
359
368
|
const isLeft = position.includes('left');
|
|
360
369
|
const isRight = position.includes('right');
|
|
361
370
|
const styles = {
|
|
362
371
|
position: 'absolute',
|
|
363
|
-
width: '
|
|
372
|
+
width: '100%',
|
|
364
373
|
pointerEvents: 'none',
|
|
365
374
|
transition: 'transform 230ms',
|
|
366
375
|
display: 'flex'
|
|
367
376
|
};
|
|
368
377
|
if (position.includes('top')) {
|
|
369
|
-
styles.top = '
|
|
378
|
+
styles.top = '0';
|
|
370
379
|
styles.justifyContent = isLeft ? 'flex-start' : isRight ? 'flex-end' : 'center';
|
|
371
380
|
}
|
|
372
381
|
if (position.includes('bottom')) {
|
|
373
|
-
styles.bottom = '
|
|
382
|
+
styles.bottom = '0';
|
|
374
383
|
styles.justifyContent = isLeft ? 'flex-start' : isRight ? 'flex-end' : 'center';
|
|
375
384
|
}
|
|
376
385
|
return styles;
|
|
@@ -378,18 +387,18 @@ function getToastPosition(position) {
|
|
|
378
387
|
|
|
379
388
|
function ToastContainer(props) {
|
|
380
389
|
props = Object.assign({
|
|
381
|
-
position: 'top-center'
|
|
390
|
+
position: 'top-center',
|
|
391
|
+
gap: 8
|
|
382
392
|
}, props);
|
|
383
393
|
const {
|
|
384
|
-
position
|
|
394
|
+
position,
|
|
395
|
+
gap
|
|
385
396
|
} = props;
|
|
386
397
|
const [toasts, setToasts] = useState([]);
|
|
387
|
-
const timeouts = useRef(new Set());
|
|
388
398
|
useEffect(() => {
|
|
389
399
|
const unsub = subscribe(toast => {
|
|
390
400
|
const duration = toast.options.duration;
|
|
391
401
|
const id = toast.options.id;
|
|
392
|
-
console.log(toast);
|
|
393
402
|
setToasts(prev => {
|
|
394
403
|
const exists = prev.some(t => t.options.id === id);
|
|
395
404
|
if (exists) {
|
|
@@ -402,40 +411,37 @@ function ToastContainer(props) {
|
|
|
402
411
|
id,
|
|
403
412
|
...toast,
|
|
404
413
|
offset: 0,
|
|
414
|
+
height: 0,
|
|
405
415
|
leaving: false
|
|
406
416
|
}, ...prev];
|
|
407
417
|
});
|
|
408
418
|
if (toast.type !== 'loading') {
|
|
409
|
-
|
|
419
|
+
setTimeout(() => {
|
|
410
420
|
setToasts(prev => prev.map(t => t.options.id === id ? {
|
|
411
421
|
...t,
|
|
412
422
|
leaving: true
|
|
413
423
|
} : t));
|
|
414
424
|
}, Math.max(0, duration - 300));
|
|
415
|
-
|
|
425
|
+
setTimeout(() => {
|
|
416
426
|
setToasts(prev => prev.filter(t => t.options.id !== id));
|
|
417
427
|
}, duration);
|
|
418
|
-
timeouts.current.add(exit);
|
|
419
|
-
timeouts.current.add(remove);
|
|
420
428
|
}
|
|
421
429
|
});
|
|
422
|
-
return () =>
|
|
423
|
-
unsub();
|
|
424
|
-
timeouts.current.forEach(clearTimeout);
|
|
425
|
-
timeouts.current.clear();
|
|
426
|
-
};
|
|
430
|
+
return () => unsub();
|
|
427
431
|
}, []);
|
|
428
432
|
useEffect(() => {
|
|
429
433
|
let offset = 0;
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
+
const updated = toasts.map(toast => {
|
|
435
|
+
const top = offset;
|
|
436
|
+
offset += toast.height;
|
|
437
|
+
return {
|
|
438
|
+
...toast,
|
|
439
|
+
offset: top
|
|
434
440
|
};
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
}, [toasts
|
|
441
|
+
});
|
|
442
|
+
const changed = updated.some((u, i) => u.offset != toasts[i].offset);
|
|
443
|
+
if (changed) setToasts(updated);
|
|
444
|
+
}, [toasts]);
|
|
439
445
|
return /*#__PURE__*/React.createElement("div", {
|
|
440
446
|
style: {
|
|
441
447
|
position: 'fixed',
|
|
@@ -443,14 +449,21 @@ function ToastContainer(props) {
|
|
|
443
449
|
zIndex: 100000000,
|
|
444
450
|
pointerEvents: 'none',
|
|
445
451
|
fontFamily: 'inherit',
|
|
446
|
-
|
|
452
|
+
display: 'grid',
|
|
453
|
+
padding: '1rem',
|
|
454
|
+
boxSizing: 'border-box'
|
|
455
|
+
}
|
|
456
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
457
|
+
style: {
|
|
458
|
+
position: 'relative'
|
|
447
459
|
}
|
|
448
460
|
}, toasts.map(toast => /*#__PURE__*/React.createElement(Toast, {
|
|
449
461
|
key: toast.options.id,
|
|
450
462
|
toast: toast,
|
|
451
463
|
setToasts: setToasts,
|
|
452
|
-
position: position
|
|
453
|
-
|
|
464
|
+
position: position,
|
|
465
|
+
gap: typeof gap === 'string' ? isNaN(+gap) ? 8 : +gap : gap
|
|
466
|
+
}))));
|
|
454
467
|
}
|
|
455
468
|
|
|
456
469
|
const toast = {
|