kitzo 2.0.6 → 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.
Files changed (2) hide show
  1. package/dist/react.esm.js +35 -19
  2. package/package.json +1 -1
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,10 +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;
394
+ console.log(toast);
396
395
  setToasts(prev => {
397
396
  const exists = prev.some(t => t.options.id === id);
398
397
  if (exists) {
@@ -409,19 +408,36 @@ function ToastContainer(props) {
409
408
  }, ...prev];
410
409
  });
411
410
  if (toast.type !== 'loading') {
412
- setTimeout(() => {
411
+ const exit = setTimeout(() => {
413
412
  setToasts(prev => prev.map(t => t.options.id === id ? {
414
413
  ...t,
415
414
  leaving: true
416
415
  } : t));
417
416
  }, Math.max(0, duration - 300));
418
- setTimeout(() => {
417
+ const remove = setTimeout(() => {
419
418
  setToasts(prev => prev.filter(t => t.options.id !== id));
420
419
  }, duration);
420
+ timeouts.current.add(exit);
421
+ timeouts.current.add(remove);
421
422
  }
422
423
  });
423
- return () => unsub();
424
+ return () => {
425
+ unsub();
426
+ timeouts.current.forEach(clearTimeout);
427
+ timeouts.current.clear();
428
+ };
424
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(',')]);
425
441
  return /*#__PURE__*/React.createElement("div", {
426
442
  style: {
427
443
  position: 'fixed',
@@ -431,9 +447,9 @@ function ToastContainer(props) {
431
447
  fontFamily: 'inherit',
432
448
  padding: '1rem'
433
449
  }
434
- }, toasts.map(t => /*#__PURE__*/React.createElement(Toast, {
435
- key: t.options.id,
436
- toast: t,
450
+ }, toasts.map(toast => /*#__PURE__*/React.createElement(Toast, {
451
+ key: toast.options.id,
452
+ toast: toast,
437
453
  setToasts: setToasts,
438
454
  position: position
439
455
  })));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitzo",
3
- "version": "2.0.6",
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",