kitzo 2.0.5 → 2.0.7

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 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.5/dist/kitzo.umd.min.js"></script>
28
+ <script src="https://cdn.jsdelivr.net/npm/kitzo@2.0.6/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
@@ -328,26 +328,23 @@ function Toast({
328
328
  const ref = useRef(null);
329
329
  useLayoutEffect(() => {
330
330
  if (!ref.current) return;
331
- const h = ref.current.getBoundingClientRect().height;
331
+ const height = ref.current.getBoundingClientRect().height;
332
332
  setToasts(prev => {
333
- const idx = prev.findIndex(t => t.options.id === toast.id);
334
- if (idx === -1) return prev;
333
+ const index = prev.findIndex(t => t.options.id === toast.options.id);
334
+ if (index === -1) return prev;
335
335
  const newToasts = [...prev];
336
- newToasts[idx] = {
337
- ...newToasts[idx],
338
- height: h
336
+ newToasts[index] = {
337
+ ...newToasts[index],
338
+ height
339
339
  };
340
340
  let offset = 0;
341
- for (let i = 0; i < newToasts.length; i++) {
342
- newToasts[i] = {
343
- ...newToasts[i],
344
- offset
345
- };
346
- offset += newToasts[i].height + GAP;
341
+ for (let t of newToasts) {
342
+ t.offset = offset;
343
+ offset += (t.height || 0) + GAP;
347
344
  }
348
345
  return newToasts;
349
346
  });
350
- }, [setToasts, toast.id]);
347
+ }, [setToasts, toast.options.id]);
351
348
  const transformY = position.includes('bottom') ? `translateY(-${toast.offset || 0}px)` : `translateY(${toast.offset || 0}px)`;
352
349
  return /*#__PURE__*/React.createElement("div", {
353
350
  ref: ref,
@@ -389,11 +386,12 @@ function ToastContainer(props) {
389
386
  position
390
387
  } = props;
391
388
  const [toasts, setToasts] = useState([]);
389
+ const timeouts = useRef(new Set());
392
390
  useEffect(() => {
393
391
  const unsub = subscribe(toast => {
394
392
  const duration = toast.options.duration;
395
393
  const id = toast.options.id;
396
- console.log(id);
394
+ console.log(toast);
397
395
  setToasts(prev => {
398
396
  const exists = prev.some(t => t.options.id === id);
399
397
  if (exists) {
@@ -410,19 +408,36 @@ function ToastContainer(props) {
410
408
  }, ...prev];
411
409
  });
412
410
  if (toast.type !== 'loading') {
413
- setTimeout(() => {
411
+ const exit = setTimeout(() => {
414
412
  setToasts(prev => prev.map(t => t.options.id === id ? {
415
413
  ...t,
416
414
  leaving: true
417
415
  } : t));
418
416
  }, Math.max(0, duration - 300));
419
- setTimeout(() => {
417
+ const remove = setTimeout(() => {
420
418
  setToasts(prev => prev.filter(t => t.options.id !== id));
421
419
  }, duration);
420
+ timeouts.current.add(exit);
421
+ timeouts.current.add(remove);
422
422
  }
423
423
  });
424
- return () => unsub();
424
+ return () => {
425
+ unsub();
426
+ timeouts.current.forEach(clearTimeout);
427
+ timeouts.current.clear();
428
+ };
425
429
  }, []);
430
+ useEffect(() => {
431
+ let offset = 0;
432
+ setToasts(prev => prev.map(t => {
433
+ const newToast = {
434
+ ...t,
435
+ offset
436
+ };
437
+ offset += (t.height || 0) + 10;
438
+ return newToast;
439
+ }));
440
+ }, [toasts.map(t => t.height).join(',')]);
426
441
  return /*#__PURE__*/React.createElement("div", {
427
442
  style: {
428
443
  position: 'fixed',
@@ -432,9 +447,9 @@ function ToastContainer(props) {
432
447
  fontFamily: 'inherit',
433
448
  padding: '1rem'
434
449
  }
435
- }, toasts.map(t => /*#__PURE__*/React.createElement(Toast, {
436
- key: t.options.id,
437
- toast: t,
450
+ }, toasts.map(toast => /*#__PURE__*/React.createElement(Toast, {
451
+ key: toast.options.id,
452
+ toast: toast,
438
453
  setToasts: setToasts,
439
454
  position: position
440
455
  })));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitzo",
3
- "version": "2.0.5",
3
+ "version": "2.0.7",
4
4
  "description": "A lightweight JavaScript UI micro-library.",
5
5
  "type": "module",
6
6
  "main": "./dist/kitzo.umd.js",