@underverse-ui/underverse 0.2.30 → 0.2.31

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/dist/index.cjs CHANGED
@@ -2688,11 +2688,6 @@ var ToastProvider = ({ children, position = "top-right", maxToasts = 5 }) => {
2688
2688
  const updated = [newToast, ...prev];
2689
2689
  return updated.slice(0, maxToasts);
2690
2690
  });
2691
- if (toast.duration !== 0) {
2692
- setTimeout(() => {
2693
- removeToast(id);
2694
- }, toast.duration || 5e3);
2695
- }
2696
2691
  },
2697
2692
  [maxToasts, removeToast]
2698
2693
  );
@@ -2713,33 +2708,31 @@ var ToastComponent = ({ toast, onRemove }) => {
2713
2708
  const [isVisible, setIsVisible] = (0, import_react8.useState)(false);
2714
2709
  const [progress, setProgress] = (0, import_react8.useState)(100);
2715
2710
  const [paused, setPaused] = (0, import_react8.useState)(false);
2716
- const [startTs] = (0, import_react8.useState)(() => Date.now());
2717
2711
  const total = toast.duration && toast.duration > 0 ? toast.duration : 5e3;
2718
- const [remaining, setRemaining] = (0, import_react8.useState)(total);
2712
+ const endTsRef = (0, import_react8.useRef)(Date.now() + total);
2713
+ const remainingRef = (0, import_react8.useRef)(total);
2714
+ const pausedRef = (0, import_react8.useRef)(false);
2715
+ const handleRemove = (0, import_react8.useCallback)(() => {
2716
+ setIsVisible(false);
2717
+ setTimeout(() => onRemove(toast.id), 150);
2718
+ }, [onRemove, toast.id]);
2719
2719
  (0, import_react8.useEffect)(() => {
2720
2720
  setIsVisible(true);
2721
2721
  if (toast.duration === 0) return;
2722
- let raf;
2723
- const tick = () => {
2724
- if (!paused) {
2725
- const elapsed = Date.now() - startTs;
2726
- const remain = Math.max(total - elapsed, 0);
2727
- setRemaining(remain);
2722
+ remainingRef.current = total;
2723
+ endTsRef.current = Date.now() + total;
2724
+ const intervalId = window.setInterval(() => {
2725
+ if (!pausedRef.current) {
2726
+ const remain = Math.max(endTsRef.current - Date.now(), 0);
2727
+ remainingRef.current = remain;
2728
2728
  setProgress(remain / total * 100);
2729
2729
  if (remain === 0) {
2730
2730
  handleRemove();
2731
- return;
2732
2731
  }
2733
2732
  }
2734
- raf = requestAnimationFrame(tick);
2735
- };
2736
- raf = requestAnimationFrame(tick);
2737
- return () => cancelAnimationFrame(raf);
2738
- }, []);
2739
- const handleRemove = () => {
2740
- setIsVisible(false);
2741
- setTimeout(() => onRemove(toast.id), 150);
2742
- };
2733
+ }, 50);
2734
+ return () => window.clearInterval(intervalId);
2735
+ }, [handleRemove, toast.duration, total]);
2743
2736
  const typeConfig = {
2744
2737
  success: {
2745
2738
  icon: import_lucide_react7.CheckCircle,
@@ -2775,8 +2768,18 @@ var ToastComponent = ({ toast, onRemove }) => {
2775
2768
  ),
2776
2769
  role: "status",
2777
2770
  "aria-live": toast.type === "error" ? "assertive" : "polite",
2778
- onMouseEnter: () => setPaused(true),
2779
- onMouseLeave: () => setPaused(false),
2771
+ onMouseEnter: () => {
2772
+ if (toast.duration === 0) return;
2773
+ pausedRef.current = true;
2774
+ remainingRef.current = Math.max(endTsRef.current - Date.now(), 0);
2775
+ setPaused(true);
2776
+ },
2777
+ onMouseLeave: () => {
2778
+ if (toast.duration === 0) return;
2779
+ pausedRef.current = false;
2780
+ endTsRef.current = Date.now() + remainingRef.current;
2781
+ setPaused(false);
2782
+ },
2780
2783
  children: [
2781
2784
  /* @__PURE__ */ (0, import_jsx_runtime15.jsxs)("div", { className: "flex items-start gap-3 p-4", children: [
2782
2785
  /* @__PURE__ */ (0, import_jsx_runtime15.jsx)(Icon, { className: cn("h-5 w-5 mt-0.5 shrink-0", config.iconClassName) }),