UNPKG

@undp/design-system-react

Version:

React based design system for UNDP

100 lines (99 loc) 1.83 kB
import e, { useEffect as t } from "react"; //#region src/hooks/use-toast.ts var n = 1, r = 1e6, i = 0; function a() { return i = (i + 1) % (2 ** 53 - 1), i.toString(); } var o = /* @__PURE__ */ new Map(), s = (e) => { if (o.has(e)) return; let t = setTimeout(() => { o.delete(e), d({ type: "REMOVE_TOAST", toastId: e }); }, r); o.set(e, t); }, c = (e, t) => { switch (t.type) { case "ADD_TOAST": return { ...e, toasts: [t.toast, ...e.toasts].slice(0, n) }; case "UPDATE_TOAST": return { ...e, toasts: e.toasts.map((e) => e.id === t.toast.id ? { ...e, ...t.toast } : e) }; case "DISMISS_TOAST": { let { toastId: n } = t; return n ? s(n) : e.toasts.forEach((e) => { s(e.id); }), { ...e, toasts: e.toasts.map((e) => e.id === n || n === void 0 ? { ...e, open: !1 } : e) }; } case "REMOVE_TOAST": return t.toastId === void 0 ? { ...e, toasts: [] } : { ...e, toasts: e.toasts.filter((e) => e.id !== t.toastId) }; default: return { ...e }; } }, l = [], u = { toasts: [] }; function d(e) { u = c(u, e), l.forEach((e) => { e(u); }); } function f({ ...e }) { let t = a(), n = (e) => d({ type: "UPDATE_TOAST", toast: { ...e, id: t } }), r = () => d({ type: "DISMISS_TOAST", toastId: t }); return d({ type: "ADD_TOAST", toast: { ...e, id: t, open: !0, onOpenChange: (e) => { e || r(); } } }), { id: t, dismiss: r, update: n }; } function p() { let [n, r] = e.useState(u); return t(() => (l.push(r), () => { let e = l.indexOf(r); e > -1 && l.splice(e, 1); }), [n]), { ...n, toast: f, dismiss: (e) => d({ type: "DISMISS_TOAST", toastId: e }) }; } //#endregion export { c as reducer, f as toast, p as useToast }; //# sourceMappingURL=ToasterHooks.js.map