kitzo 2.0.14 → 2.0.16
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 -54
- 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.16/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,13 +387,14 @@ 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;
|
|
@@ -401,40 +411,37 @@ function ToastContainer(props) {
|
|
|
401
411
|
id,
|
|
402
412
|
...toast,
|
|
403
413
|
offset: 0,
|
|
414
|
+
height: 0,
|
|
404
415
|
leaving: false
|
|
405
416
|
}, ...prev];
|
|
406
417
|
});
|
|
407
418
|
if (toast.type !== 'loading') {
|
|
408
|
-
|
|
419
|
+
setTimeout(() => {
|
|
409
420
|
setToasts(prev => prev.map(t => t.options.id === id ? {
|
|
410
421
|
...t,
|
|
411
422
|
leaving: true
|
|
412
423
|
} : t));
|
|
413
424
|
}, Math.max(0, duration - 300));
|
|
414
|
-
|
|
425
|
+
setTimeout(() => {
|
|
415
426
|
setToasts(prev => prev.filter(t => t.options.id !== id));
|
|
416
427
|
}, duration);
|
|
417
|
-
timeouts.current.add(exit);
|
|
418
|
-
timeouts.current.add(remove);
|
|
419
428
|
}
|
|
420
429
|
});
|
|
421
|
-
return () =>
|
|
422
|
-
unsub();
|
|
423
|
-
timeouts.current.forEach(clearTimeout);
|
|
424
|
-
timeouts.current.clear();
|
|
425
|
-
};
|
|
430
|
+
return () => unsub();
|
|
426
431
|
}, []);
|
|
427
432
|
useEffect(() => {
|
|
428
433
|
let offset = 0;
|
|
429
|
-
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
434
|
+
const updated = toasts.map(toast => {
|
|
435
|
+
const top = offset;
|
|
436
|
+
offset += toast.height;
|
|
437
|
+
return {
|
|
438
|
+
...toast,
|
|
439
|
+
offset: top
|
|
433
440
|
};
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
}, [toasts
|
|
441
|
+
});
|
|
442
|
+
const changed = updated.some((u, i) => u.offset != toasts[i].offset);
|
|
443
|
+
if (changed) setToasts(updated);
|
|
444
|
+
}, [toasts]);
|
|
438
445
|
return /*#__PURE__*/React.createElement("div", {
|
|
439
446
|
style: {
|
|
440
447
|
position: 'fixed',
|
|
@@ -442,14 +449,21 @@ function ToastContainer(props) {
|
|
|
442
449
|
zIndex: 100000000,
|
|
443
450
|
pointerEvents: 'none',
|
|
444
451
|
fontFamily: 'inherit',
|
|
445
|
-
|
|
452
|
+
display: 'grid',
|
|
453
|
+
padding: '1rem',
|
|
454
|
+
boxSizing: 'border-box'
|
|
455
|
+
}
|
|
456
|
+
}, /*#__PURE__*/React.createElement("div", {
|
|
457
|
+
style: {
|
|
458
|
+
position: 'relative'
|
|
446
459
|
}
|
|
447
460
|
}, toasts.map(toast => /*#__PURE__*/React.createElement(Toast, {
|
|
448
461
|
key: toast.options.id,
|
|
449
462
|
toast: toast,
|
|
450
463
|
setToasts: setToasts,
|
|
451
|
-
position: position
|
|
452
|
-
|
|
464
|
+
position: position,
|
|
465
|
+
gap: typeof gap === 'string' ? isNaN(+gap) ? 8 : +gap : gap
|
|
466
|
+
}))));
|
|
453
467
|
}
|
|
454
468
|
|
|
455
469
|
const toast = {
|