kitzo 2.0.28 → 2.0.29
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 +39 -5
- 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.29/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,7 +1,5 @@
|
|
|
1
1
|
import React, { useRef, useLayoutEffect, useState, useEffect } from 'react';
|
|
2
2
|
|
|
3
|
-
const listeners = new Set();
|
|
4
|
-
let isStyleAdded = false;
|
|
5
3
|
function toastStyles() {
|
|
6
4
|
return `.toast-content,
|
|
7
5
|
.toast-content-bottom {
|
|
@@ -183,6 +181,8 @@ function toastStyles() {
|
|
|
183
181
|
}
|
|
184
182
|
`;
|
|
185
183
|
}
|
|
184
|
+
const listeners = new Set();
|
|
185
|
+
let isStyleAdded = false;
|
|
186
186
|
function addToast(toast) {
|
|
187
187
|
listeners.forEach(callback => {
|
|
188
188
|
callback(toast);
|
|
@@ -267,6 +267,27 @@ function promise(callback, msgs = {}, options = {}) {
|
|
|
267
267
|
return Promise.reject(normalizedError);
|
|
268
268
|
});
|
|
269
269
|
}
|
|
270
|
+
function custom(render, options = {}) {
|
|
271
|
+
const id = Date.now();
|
|
272
|
+
options = Object.assign({
|
|
273
|
+
duration: 3000,
|
|
274
|
+
id,
|
|
275
|
+
exitDelay: 50
|
|
276
|
+
}, options);
|
|
277
|
+
addToast({
|
|
278
|
+
type: 'custom',
|
|
279
|
+
render,
|
|
280
|
+
options
|
|
281
|
+
});
|
|
282
|
+
return id;
|
|
283
|
+
}
|
|
284
|
+
function dismiss(id, exitDelay) {
|
|
285
|
+
addToast({
|
|
286
|
+
type: 'dismiss',
|
|
287
|
+
id,
|
|
288
|
+
exitDelay
|
|
289
|
+
});
|
|
290
|
+
}
|
|
270
291
|
|
|
271
292
|
function SuccessSvg() {
|
|
272
293
|
return /*#__PURE__*/React.createElement("div", {
|
|
@@ -361,7 +382,7 @@ function Toast({
|
|
|
361
382
|
...style
|
|
362
383
|
},
|
|
363
384
|
className: `toast-content${position.includes('bottom') ? '-bottom' : ''} ${leaving ? 'exit' : ''}`
|
|
364
|
-
}, 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)));
|
|
385
|
+
}, type === 'custom' ? typeof toast.render === 'function' ? toast.render(() => dismiss(toast.id, toast.options.exitDelay)) : typeof toast.render === 'string' ? /*#__PURE__*/React.createElement("span", null, toast.render) : toast.render : /*#__PURE__*/React.createElement(React.Fragment, null, 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))));
|
|
365
386
|
}
|
|
366
387
|
function getToastPosition(position) {
|
|
367
388
|
const isLeft = position.includes('left');
|
|
@@ -396,6 +417,18 @@ function ToastContainer(props) {
|
|
|
396
417
|
const [toasts, setToasts] = useState([]);
|
|
397
418
|
useEffect(() => {
|
|
398
419
|
const unsub = subscribe(toast => {
|
|
420
|
+
if (toast.type === 'dismiss') {
|
|
421
|
+
setTimeout(() => {
|
|
422
|
+
setToasts(prev => prev.map(t => t.options.id === toast.id ? {
|
|
423
|
+
...t,
|
|
424
|
+
leaving: true
|
|
425
|
+
} : t));
|
|
426
|
+
}, Math.max(0, Number(toast.exitDelay)));
|
|
427
|
+
setTimeout(() => {
|
|
428
|
+
setToasts(prev => prev.filter(t => t.options.id !== toast.id));
|
|
429
|
+
}, Math.max(0, Number(toast.exitDelay) + 300));
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
399
432
|
const duration = toast.options.duration;
|
|
400
433
|
const id = toast.options.id;
|
|
401
434
|
setToasts(prev => {
|
|
@@ -414,7 +447,7 @@ function ToastContainer(props) {
|
|
|
414
447
|
leaving: false
|
|
415
448
|
}, ...prev];
|
|
416
449
|
});
|
|
417
|
-
if (toast.type !== 'loading') {
|
|
450
|
+
if (toast.type !== 'loading' && isFinite(duration)) {
|
|
418
451
|
setTimeout(() => {
|
|
419
452
|
setToasts(prev => prev.map(t => t.options.id === id ? {
|
|
420
453
|
...t,
|
|
@@ -468,7 +501,8 @@ function ToastContainer(props) {
|
|
|
468
501
|
const toast = {
|
|
469
502
|
success,
|
|
470
503
|
error,
|
|
471
|
-
promise
|
|
504
|
+
promise,
|
|
505
|
+
custom
|
|
472
506
|
};
|
|
473
507
|
|
|
474
508
|
export { ToastContainer, toast };
|