kitzo 2.0.28 → 2.0.30
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 +55 -6
- 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.30/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 {
|
|
@@ -10,6 +8,9 @@ function toastStyles() {
|
|
|
10
8
|
.toast-content,
|
|
11
9
|
.toast-content-bottom {
|
|
12
10
|
pointer-events: all;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.pre-styled {
|
|
13
14
|
padding-inline: 0.825rem;
|
|
14
15
|
padding-block: 0.625rem;
|
|
15
16
|
display: flex;
|
|
@@ -183,6 +184,8 @@ function toastStyles() {
|
|
|
183
184
|
}
|
|
184
185
|
`;
|
|
185
186
|
}
|
|
187
|
+
const listeners = new Set();
|
|
188
|
+
let isStyleAdded = false;
|
|
186
189
|
function addToast(toast) {
|
|
187
190
|
listeners.forEach(callback => {
|
|
188
191
|
callback(toast);
|
|
@@ -267,6 +270,27 @@ function promise(callback, msgs = {}, options = {}) {
|
|
|
267
270
|
return Promise.reject(normalizedError);
|
|
268
271
|
});
|
|
269
272
|
}
|
|
273
|
+
function custom(render, options = {}) {
|
|
274
|
+
const id = Date.now();
|
|
275
|
+
options = Object.assign({
|
|
276
|
+
duration: 3000,
|
|
277
|
+
id,
|
|
278
|
+
exitDelay: 50
|
|
279
|
+
}, options);
|
|
280
|
+
addToast({
|
|
281
|
+
type: 'custom',
|
|
282
|
+
render,
|
|
283
|
+
options
|
|
284
|
+
});
|
|
285
|
+
return id;
|
|
286
|
+
}
|
|
287
|
+
function dismiss(id, exitDelay) {
|
|
288
|
+
addToast({
|
|
289
|
+
type: 'dismiss',
|
|
290
|
+
id,
|
|
291
|
+
exitDelay
|
|
292
|
+
});
|
|
293
|
+
}
|
|
270
294
|
|
|
271
295
|
function SuccessSvg() {
|
|
272
296
|
return /*#__PURE__*/React.createElement("div", {
|
|
@@ -349,6 +373,16 @@ function Toast({
|
|
|
349
373
|
} : t));
|
|
350
374
|
}, []);
|
|
351
375
|
const transformY = position.includes('bottom') ? `translateY(-${offset || 0}px)` : `translateY(${offset || 0}px)`;
|
|
376
|
+
function renderCustomContent() {
|
|
377
|
+
const render = toast.render;
|
|
378
|
+
if (typeof render === 'function') {
|
|
379
|
+
return render(() => dismiss(toast.id, toast.options.exitDelay));
|
|
380
|
+
}
|
|
381
|
+
if (typeof render === 'string') {
|
|
382
|
+
return /*#__PURE__*/React.createElement("span", null, render);
|
|
383
|
+
}
|
|
384
|
+
return render;
|
|
385
|
+
}
|
|
352
386
|
return /*#__PURE__*/React.createElement("div", {
|
|
353
387
|
ref: ref,
|
|
354
388
|
style: {
|
|
@@ -357,11 +391,13 @@ function Toast({
|
|
|
357
391
|
...getToastPosition(position)
|
|
358
392
|
}
|
|
359
393
|
}, /*#__PURE__*/React.createElement("div", {
|
|
394
|
+
className: `toast-content${position.includes('bottom') ? '-bottom' : ''} ${leaving ? 'exit' : ''}`
|
|
395
|
+
}, type === 'custom' ? renderCustomContent() : /*#__PURE__*/React.createElement("div", {
|
|
360
396
|
style: {
|
|
361
397
|
...style
|
|
362
398
|
},
|
|
363
|
-
className:
|
|
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)));
|
|
399
|
+
className: "pre-styled"
|
|
400
|
+
}, 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
401
|
}
|
|
366
402
|
function getToastPosition(position) {
|
|
367
403
|
const isLeft = position.includes('left');
|
|
@@ -396,6 +432,18 @@ function ToastContainer(props) {
|
|
|
396
432
|
const [toasts, setToasts] = useState([]);
|
|
397
433
|
useEffect(() => {
|
|
398
434
|
const unsub = subscribe(toast => {
|
|
435
|
+
if (toast.type === 'dismiss') {
|
|
436
|
+
setTimeout(() => {
|
|
437
|
+
setToasts(prev => prev.map(t => t.options.id === toast.id ? {
|
|
438
|
+
...t,
|
|
439
|
+
leaving: true
|
|
440
|
+
} : t));
|
|
441
|
+
}, Math.max(0, Number(toast.exitDelay)));
|
|
442
|
+
setTimeout(() => {
|
|
443
|
+
setToasts(prev => prev.filter(t => t.options.id !== toast.id));
|
|
444
|
+
}, Math.max(0, Number(toast.exitDelay) + 300));
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
399
447
|
const duration = toast.options.duration;
|
|
400
448
|
const id = toast.options.id;
|
|
401
449
|
setToasts(prev => {
|
|
@@ -414,7 +462,7 @@ function ToastContainer(props) {
|
|
|
414
462
|
leaving: false
|
|
415
463
|
}, ...prev];
|
|
416
464
|
});
|
|
417
|
-
if (toast.type !== 'loading') {
|
|
465
|
+
if (toast.type !== 'loading' && isFinite(duration)) {
|
|
418
466
|
setTimeout(() => {
|
|
419
467
|
setToasts(prev => prev.map(t => t.options.id === id ? {
|
|
420
468
|
...t,
|
|
@@ -468,7 +516,8 @@ function ToastContainer(props) {
|
|
|
468
516
|
const toast = {
|
|
469
517
|
success,
|
|
470
518
|
error,
|
|
471
|
-
promise
|
|
519
|
+
promise,
|
|
520
|
+
custom
|
|
472
521
|
};
|
|
473
522
|
|
|
474
523
|
export { ToastContainer, toast };
|