UNPKG

@adaptui/react

Version:

Collection of headless components/hooks that are accessible, composable, customizable from low level to build your own UI & Design System powered by Reakit

49 lines (46 loc) 1.47 kB
import React from "react"; import { getToast } from "./helpers"; export var useToastTimer = (toasts, updateToast, dismissToast) => { React.useEffect(() => { var now = Date.now(); var timeouts = toasts.map(t => { if (!t.autoDismiss) return undefined; if (t.pausedAt) return undefined; var durationLeft = (t.dismissDuration || 0) + t.pauseDuration - (now - t.createdAt); if (durationLeft < 0) { if (t.visible) { dismissToast(t.id); } return undefined; } return setTimeout(() => { dismissToast(t.id); }, durationLeft); }); return () => { timeouts.forEach(timeout => timeout && clearTimeout(timeout)); }; }, [toasts, dismissToast]); var pauseTimer = React.useCallback(toastId => { var toast = getToast(toasts, toastId); if (!(toast !== null && toast !== void 0 && toast.autoDismiss)) return; updateToast(toastId, { pausedAt: Date.now() }); }, [toasts, updateToast]); var resumeTimer = React.useCallback(toastId => { var toast = getToast(toasts, toastId); if (!(toast !== null && toast !== void 0 && toast.autoDismiss)) return; var now = Date.now(); var diff = now - (toast.pausedAt || 0); updateToast(toastId, { pausedAt: null, pauseDuration: toast.pauseDuration + diff }); }, [toasts, updateToast]); return { resumeTimer, pauseTimer }; }; //# sourceMappingURL=useToastTimer.js.map