UNPKG

@undp/design-system-react

Version:

React based design system for UNDP

537 lines (536 loc) 20.2 kB
import { t as e } from "./utils-ca9as9Ye.js"; import { t } from "./dist-CQzJbxrW.js"; import { t as n } from "./createLucideIcon-JAvQ_M0R.js"; import { t as r } from "./x-CXpb8l-i.js"; import { i } from "./dist-DxuGUd9a.js"; import { n as a, t as o } from "./dist-jqIPNHDe.js"; import { r as s } from "./dist-X_NWu4il.js"; import { n as c, t as l } from "./dist-BKfyhBSH.js"; import { t as u } from "./dist-J8GU8wyT.js"; import { n as d, t as f } from "./dist-CwCVtfik.js"; import { t as p } from "./dist-CnnnGPaT.js"; import { t as m } from "./dist-C8FJXTu6.js"; import { r as h, t as g } from "./dist-CMH_aW5P.js"; import { t as _ } from "./dist-vWm-kLzq.js"; import { useToast as v } from "./ToasterHooks.js"; import * as y from "react"; import b, { createElement as x } from "react"; import { Fragment as S, jsx as C, jsxs as w } from "react/jsx-runtime"; import * as ee from "react-dom"; var te = n("circle-check-big", [["path", { d: "M21.801 10A10 10 0 1 1 17 3.335", key: "yps3ct" }], ["path", { d: "m9 11 3 3L22 4", key: "1pflzl" }]]), T = n("circle-x", [ ["circle", { cx: "12", cy: "12", r: "10", key: "1mglay" }], ["path", { d: "m15 9-6 6", key: "1uzhvr" }], ["path", { d: "m9 9 6 6", key: "z0biqf" }] ]), E = n("info", [ ["circle", { cx: "12", cy: "12", r: "10", key: "1mglay" }], ["path", { d: "M12 16v-4", key: "1dtifu" }], ["path", { d: "M12 8h.01", key: "e9boi3" }] ]), D = n("triangle-alert", [ ["path", { d: "m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3", key: "wmoenq" }], ["path", { d: "M12 9v4", key: "juzpu7" }], ["path", { d: "M12 17h.01", key: "p32p05" }] ]), O = Object.defineProperty, k = (e, t) => O(e, "name", { value: t, configurable: !0 }), A = "ToastProvider", [j, M, N] = u("Toast"), [P, F] = c("Toast", [N]), [I, L] = P(A), R = /* @__PURE__ */ k((e) => { let { __scopeToast: t, label: n = "Notification", duration: r = 5e3, swipeDirection: i = "right", swipeThreshold: a = 50, announcerContainer: o, children: s } = e, [c, l] = y.useState(null), [u, d] = y.useState(0), f = y.useRef(!1); return n.trim() || console.error(`Invalid prop \`label\` supplied to \`${A}\`. Expected non-empty \`string\`.`), /* @__PURE__ */ C(j.Provider, { scope: t, children: /* @__PURE__ */ C(I, { scope: t, label: n, duration: r, swipeDirection: i, swipeThreshold: a, toastCount: u, viewport: c, onViewportChange: l, onToastAdd: y.useCallback(() => d((e) => e + 1), []), onToastRemove: y.useCallback(() => d((e) => e - 1), []), isClosePausedRef: f, announcerContainer: o, children: s }) }); }, "ToastProvider"), z = "ToastViewport", B = ["F8"], V = "toast.viewportPause", H = "toast.viewportResume", U = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { __scopeToast: n, hotkey: r = B, label: a = "Notifications ({hotkey})", ...s } = e, c = L(z, n), l = M(n), u = y.useRef(null), d = y.useRef(null), f = y.useRef(null), p = y.useRef(null), m = i(t, p, c.onViewportChange), h = r.join("+").replace(/Key/g, "").replace(/Digit/g, ""), _ = c.toastCount > 0; y.useEffect(() => { let e = /* @__PURE__ */ k((e) => { r.length !== 0 && r.every((t) => e[t] || e.code === t) && p.current?.focus(); }, "handleKeyDown"); return document.addEventListener("keydown", e), () => document.removeEventListener("keydown", e); }, [r]), y.useEffect(() => { let e = u.current, t = p.current; if (_ && e && t) { let n = /* @__PURE__ */ k(() => { if (!c.isClosePausedRef.current) { let e = new CustomEvent(V); t.dispatchEvent(e), c.isClosePausedRef.current = !0; } }, "handlePause"), r = /* @__PURE__ */ k(() => { if (c.isClosePausedRef.current) { let e = new CustomEvent(H); t.dispatchEvent(e), c.isClosePausedRef.current = !1; } }, "handleResume"), i = /* @__PURE__ */ k((t) => { e.contains(t.relatedTarget) || r(); }, "handleFocusOutResume"), a = /* @__PURE__ */ k(() => { e.contains(document.activeElement) || r(); }, "handlePointerLeaveResume"); return e.addEventListener("focusin", n), e.addEventListener("focusout", i), e.addEventListener("pointermove", n), e.addEventListener("pointerleave", a), window.addEventListener("blur", n), window.addEventListener("focus", r), () => { e.removeEventListener("focusin", n), e.removeEventListener("focusout", i), e.removeEventListener("pointermove", n), e.removeEventListener("pointerleave", a), window.removeEventListener("blur", n), window.removeEventListener("focus", r); }; } }, [_, c.isClosePausedRef]); let v = y.useCallback(({ tabbingDirection: e }) => { let t = l().map((t) => { let n = t.ref.current, r = [n, ..._e(n)]; return e === "forwards" ? r : r.reverse(); }); return (e === "forwards" ? t.reverse() : t).flat(); }, [l]); return y.useEffect(() => { let e = p.current; if (e) { let t = /* @__PURE__ */ k((t) => { let n = t.altKey || t.ctrlKey || t.metaKey; if (t.key === "Tab" && !n) { let n = document.activeElement, r = t.shiftKey; if (t.target === e && r) { d.current?.focus(); return; } let i = v({ tabbingDirection: r ? "backwards" : "forwards" }), a = i.findIndex((e) => e === n); $(i.slice(a + 1)) ? t.preventDefault() : r ? d.current?.focus() : f.current?.focus(); } }, "handleKeyDown"); return e.addEventListener("keydown", t), () => e.removeEventListener("keydown", t); } }, [l, v]), /* @__PURE__ */ w(g, { ref: u, role: "region", "aria-label": a.replace("{hotkey}", h), tabIndex: -1, style: { pointerEvents: _ ? void 0 : "none" }, children: [ _ && /* @__PURE__ */ C(G, { ref: d, onFocusFromOutsideViewport: () => { $(v({ tabbingDirection: "forwards" })); } }), /* @__PURE__ */ C(j.Slot, { scope: n, children: /* @__PURE__ */ C(o.ol, { tabIndex: -1, ...s, ref: m }) }), _ && /* @__PURE__ */ C(G, { ref: f, onFocusFromOutsideViewport: () => { $(v({ tabbingDirection: "backwards" })); } }) ] }); }, "ToastViewport")), W = "ToastFocusProxy", G = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { __scopeToast: n, onFocusFromOutsideViewport: r, ...i } = e, a = L(W, n); return /* @__PURE__ */ C(s, { tabIndex: 0, ...i, ref: t, style: { position: "fixed" }, onFocus: (e) => { let t = e.relatedTarget; a.viewport?.contains(t) || r(); } }); }, "ToastFocusProxy")), K = "Toast", ne = "toast.swipeStart", re = "toast.swipeMove", ie = "toast.swipeCancel", ae = "toast.swipeEnd", oe = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { forceMount: n, open: r, defaultOpen: i, onOpenChange: a, ...o } = e, [s, c] = f({ prop: r, defaultProp: i ?? !0, onChange: a, caller: K }); return /* @__PURE__ */ C(p, { present: n || s, children: /* @__PURE__ */ C(le, { open: s, ...o, ref: t, onClose: () => c(!1), onPause: m(e.onPause), onResume: m(e.onResume), onSwipeStart: d(e.onSwipeStart, (e) => { e.currentTarget.setAttribute("data-swipe", "start"); }), onSwipeMove: d(e.onSwipeMove, (e) => { let { x: t, y: n } = e.detail.delta; e.currentTarget.setAttribute("data-swipe", "move"), e.currentTarget.style.setProperty("--radix-toast-swipe-move-x", `${t}px`), e.currentTarget.style.setProperty("--radix-toast-swipe-move-y", `${n}px`); }), onSwipeCancel: d(e.onSwipeCancel, (e) => { e.currentTarget.setAttribute("data-swipe", "cancel"), e.currentTarget.style.removeProperty("--radix-toast-swipe-move-x"), e.currentTarget.style.removeProperty("--radix-toast-swipe-move-y"), e.currentTarget.style.removeProperty("--radix-toast-swipe-end-x"), e.currentTarget.style.removeProperty("--radix-toast-swipe-end-y"); }), onSwipeEnd: d(e.onSwipeEnd, (e) => { let { x: t, y: n } = e.detail.delta; e.currentTarget.setAttribute("data-swipe", "end"), e.currentTarget.style.removeProperty("--radix-toast-swipe-move-x"), e.currentTarget.style.removeProperty("--radix-toast-swipe-move-y"), e.currentTarget.style.setProperty("--radix-toast-swipe-end-x", `${t}px`), e.currentTarget.style.setProperty("--radix-toast-swipe-end-y", `${n}px`), c(!1); }) }) }); }, "Toast")), [se, ce] = P(K, { onClose() {} }), le = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { __scopeToast: n, type: r = "foreground", duration: a, open: s, onClose: c, onEscapeKeyDown: l, onPause: u, onResume: f, onSwipeStart: p, onSwipeMove: g, onSwipeCancel: _, onSwipeEnd: v, ...b } = e, x = L(K, n), te = M(n), [T, E] = y.useState(null), D = i(t, E), O = y.useRef(null), A = y.useRef(null), N = a || x.duration, P = y.useRef(0), F = y.useRef(N), I = y.useRef(0), { onToastAdd: R, onToastRemove: z } = x, B = m(() => { T?.contains(document.activeElement) && x.viewport?.focus(), c(); }), U = y.useCallback((e) => { !e || e === Infinity || (window.clearTimeout(I.current), P.current = (/* @__PURE__ */ new Date()).getTime(), I.current = window.setTimeout(B, e)); }, [B]); y.useEffect(() => { let e = x.viewport; if (e) { let t = /* @__PURE__ */ k(() => { U(F.current), f?.(); }, "handleResume"), n = /* @__PURE__ */ k(() => { let e = (/* @__PURE__ */ new Date()).getTime() - P.current; F.current -= e, window.clearTimeout(I.current), u?.(); }, "handlePause"); return e.addEventListener(V, n), e.addEventListener(H, t), () => { e.removeEventListener(V, n), e.removeEventListener(H, t); }; } }, [ x.viewport, N, u, f, U ]), y.useEffect(() => { s && !x.isClosePausedRef.current && U(N); }, [ s, N, x.isClosePausedRef, U ]), y.useEffect(() => () => { window.clearTimeout(I.current); }, []), y.useEffect(() => (R(), () => z()), [R, z]); let W = y.useMemo(() => T ? J(T) : null, [T]); return x.viewport ? /* @__PURE__ */ w(S, { children: [W && /* @__PURE__ */ C(ue, { __scopeToast: n, role: "status", "aria-live": r === "foreground" ? "assertive" : "polite", children: W }), /* @__PURE__ */ C(se, { scope: n, onClose: B, children: ee.createPortal(/* @__PURE__ */ C(j.ItemSlot, { scope: n, children: /* @__PURE__ */ C(h, { asChild: !0, onEscapeKeyDown: d(l, (e) => { te().some((t) => t.ref.current?.contains(e.target)) || B(); }), children: /* @__PURE__ */ C(o.li, { tabIndex: 0, "data-state": s ? "open" : "closed", "data-swipe-direction": x.swipeDirection, ...b, ref: D, style: { userSelect: "none", touchAction: "none", ...e.style }, onKeyDown: d(e.onKeyDown, (e) => { e.key === "Escape" && (l?.(e.nativeEvent), e.nativeEvent.defaultPrevented || B()); }), onPointerDown: d(e.onPointerDown, (e) => { e.button === 0 && (O.current = { x: e.clientX, y: e.clientY }); }), onPointerMove: d(e.onPointerMove, (e) => { if (!O.current) return; let t = e.clientX - O.current.x, n = e.clientY - O.current.y, r = !!A.current, i = ["left", "right"].includes(x.swipeDirection), a = ["left", "up"].includes(x.swipeDirection) ? Math.min : Math.max, o = i ? a(0, t) : 0, s = i ? 0 : a(0, n), c = e.pointerType === "touch" ? 10 : 2, l = { x: o, y: s }, u = { originalEvent: e, delta: l }; r ? (A.current = l, Y(re, g, u, { discrete: !1 })) : X(l, x.swipeDirection, c) ? (A.current = l, Y(ne, p, u, { discrete: !1 }), e.target.setPointerCapture(e.pointerId)) : (Math.abs(t) > c || Math.abs(n) > c) && (O.current = null); }), onPointerUp: d(e.onPointerUp, (e) => { let t = A.current, n = e.target; if (n.hasPointerCapture(e.pointerId) && n.releasePointerCapture(e.pointerId), A.current = null, O.current = null, t) { let n = e.currentTarget, r = { originalEvent: e, delta: t }; X(t, x.swipeDirection, x.swipeThreshold) ? Y(ae, v, r, { discrete: !0 }) : Y(ie, _, r, { discrete: !0 }), n.addEventListener("click", (e) => e.preventDefault(), { once: !0 }); } }) }) }) }), x.viewport) })] }) : null; }, "ToastImpl")), ue = /* @__PURE__ */ k((e) => { let { __scopeToast: t, children: n, ...r } = e, i = L(K, t), [a, o] = y.useState(!1), [c, l] = y.useState(!1); return Z(() => o(!0)), y.useEffect(() => { let e = window.setTimeout(() => l(!0), 1e3); return () => window.clearTimeout(e); }, []), c ? null : /* @__PURE__ */ C(_, { asChild: !0, container: i.announcerContainer || void 0, children: /* @__PURE__ */ C(s, { ...r, children: a && /* @__PURE__ */ w(S, { children: [ i.label, " ", n ] }) }) }); }, "ToastAnnounce"), de = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { __scopeToast: n, ...r } = e; return /* @__PURE__ */ C(o.div, { ...r, ref: t }); }, "ToastTitle")), fe = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { __scopeToast: n, ...r } = e; return /* @__PURE__ */ C(o.div, { ...r, ref: t }); }, "ToastDescription")), pe = "ToastAction", me = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { altText: n, ...r } = e; return n.trim() ? /* @__PURE__ */ C(q, { altText: n, asChild: !0, children: /* @__PURE__ */ C(ge, { ...r, ref: t }) }) : (console.error(`Invalid prop \`altText\` supplied to \`${pe}\`. Expected non-empty \`string\`.`), null); }, "ToastAction")), he = "ToastClose", ge = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { __scopeToast: n, ...r } = e, i = ce(he, n); return /* @__PURE__ */ C(q, { asChild: !0, children: /* @__PURE__ */ C(o.button, { type: "button", ...r, ref: t, onClick: d(e.onClick, i.onClose) }) }); }, "ToastClose")), q = /* @__PURE__ */ y.forwardRef(/* @__PURE__ */ k(function(e, t) { let { __scopeToast: n, altText: r, ...i } = e; return /* @__PURE__ */ C(o.div, { "data-radix-toast-announce-exclude": "", "data-radix-toast-announce-alt": r || void 0, ...i, ref: t }); }, "ToastAnnounceExclude")); function J(e) { let t = []; return Array.from(e.childNodes).forEach((e) => { if (e.nodeType === e.TEXT_NODE && e.textContent && t.push(e.textContent), Q(e)) { let n = e.ariaHidden || e.hidden || e.style.display === "none", r = e.dataset.radixToastAnnounceExclude === ""; if (!n) if (r) { let n = e.dataset.radixToastAnnounceAlt; n && t.push(n); } else t.push(...J(e)); } }), t; } k(J, "getAnnounceTextContent"); function Y(e, t, n, { discrete: r }) { let i = n.originalEvent.currentTarget, o = new CustomEvent(e, { bubbles: !0, cancelable: !0, detail: n }); t && i.addEventListener(e, t, { once: !0 }), r ? a(i, o) : i.dispatchEvent(o); } k(Y, "handleAndDispatchCustomEvent"); var X = /* @__PURE__ */ k((e, t, n = 0) => { let r = Math.abs(e.x), i = Math.abs(e.y), a = r > i; return t === "left" || t === "right" ? a && r > n : !a && i > n; }, "isDeltaInDirection"); function Z(e = () => {}) { let t = m(e); l(() => { let e = 0, n = 0; return e = window.requestAnimationFrame(() => n = window.requestAnimationFrame(t)), () => { window.cancelAnimationFrame(e), window.cancelAnimationFrame(n); }; }, [t]); } k(Z, "useNextFrame"); function Q(e) { return e.nodeType === e.ELEMENT_NODE; } k(Q, "isHTMLElement"); function _e(e) { let t = [], n = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT, { acceptNode: /* @__PURE__ */ k((e) => { let t = e.tagName === "INPUT" && e.type === "hidden"; return e.disabled || e.hidden || t ? NodeFilter.FILTER_SKIP : e.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP; }, "acceptNode") }); for (; n.nextNode();) t.push(n.currentNode); return t; } k(_e, "getTabbableCandidates"); function $(e) { let t = document.activeElement; return e.some((e) => e === t ? !0 : (e.focus(), document.activeElement !== t)); } k($, "focusFirst"); var ve = R, ye = U, be = oe, xe = de, Se = fe, Ce = me, we = ge, Te = ve, Ee = b.createContext(null), De = b.forwardRef(({ className: t, ...n }, r) => /* @__PURE__ */ C(ye, { ...n, ref: r, className: e("fixed top-0 z-100 flex max-h-screen w-full flex-col-reverse p-4 sm:top-auto sm:right-0 sm:bottom-0 sm:flex-col md:max-w-105", t) })); De.displayName = ye.displayName; var Oe = t("group pointer-events-auto relative shadow-xs bg-primary-white flex w-full text-content-primary items-center justify-between flex gap-2 overflow-hidden rounded-sm border border-stroke p-2 pr-6 transition-all data-[swipe=cancel]:translate-x-0 data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)] data-[swipe=move]:translate-x-[var(--radix-toast-swipe-move-x)] data-[swipe=move]:transition-none data-[state=open]:animate-in data-[state=closed]:animate-out data-[swipe=end]:animate-out data-[state=closed]:fade-out-80 data-[state=closed]:slide-out-to-right-full data-[state=open]:slide-in-from-top-full sm:data-[state=open]:slide-in-from-bottom-full", { variants: { variant: { default: "border border-stroke", success: "border-t-4 border-success", warning: "border-t-4 border-warning", info: "border-t-4 border-info", destructive: "border-t-4 border-error" } }, defaultVariants: { variant: "default" } }), ke = t("", { variants: { variant: { default: "text-content-primary", success: "text-success", warning: "text-warning", info: "text-info", destructive: "text-error" } }, defaultVariants: { variant: "default" } }), Ae = b.forwardRef(({ className: t, variant: n, ...r }, i) => /* @__PURE__ */ C(Ee.Provider, { value: { variant: n }, children: /* @__PURE__ */ C(be, { ...r, ref: i, className: e(Oe({ variant: n }), t) }) })); Ae.displayName = be.displayName; var je = b.forwardRef(({ className: t, ...n }, r) => /* @__PURE__ */ C(Ce, { ...n, ref: r, className: e("inline-flex h-8 shrink-0 items-center justify-center rounded-none bg-transparent px-3 font-medium text-content-primary text-sm transition-colors hover:bg-surface focus:outline-hidden focus:ring-1 disabled:pointer-events-none disabled:opacity-disabled", t) })); je.displayName = Ce.displayName; var Me = b.forwardRef(({ className: t, ...n }, i) => /* @__PURE__ */ C(we, { ...n, ref: i, className: e("absolute top-1 right-1 rounded-md p-1 text-content-primary/50 opacity-0 transition-opacity hover:text-content-primary focus:opacity-100 focus:outline-hidden focus:ring-1 group-hover:opacity-100", t), "toast-close": "", children: /* @__PURE__ */ C(r, { className: "h-4 w-4" }) })); Me.displayName = we.displayName; var Ne = b.forwardRef(({ className: t, ...n }, r) => { let i = b.useContext(Ee), a = e(ke({ variant: i?.variant }), "text-lg font-bold [&+div]:text-xs", t), o = null; switch (i?.variant) { case "destructive": o = /* @__PURE__ */ C(T, { className: "h-4 w-4 stroke-error", strokeWidth: 2 }); break; case "success": o = /* @__PURE__ */ C(te, { className: "h-4 w-4 stroke-success", strokeWidth: 2 }); break; case "warning": o = /* @__PURE__ */ C(D, { className: "h-4 w-4 stroke-warning", strokeWidth: 2 }); break; case "info": o = /* @__PURE__ */ C(E, { className: "h-4 w-4 stroke-info", strokeWidth: 2 }); break; default: o = null; break; } return /* @__PURE__ */ w("div", { className: "flex items-center gap-x-1", children: [o, /* @__PURE__ */ C(xe, { ...n, ref: r, className: a })] }); }); Ne.displayName = xe.displayName; var Pe = b.forwardRef(({ className: t, ...n }, r) => /* @__PURE__ */ C(Se, { ...n, ref: r, className: e("text-base text-content-secondary opacity-90", t) })); Pe.displayName = Se.displayName; //#endregion //#region src/components/ui/toaster.tsx function Fe() { let { toasts: e } = v(); return /* @__PURE__ */ w(Te, { children: [e.map(({ id: e, title: t, description: n, action: r, ...i }) => /* @__PURE__ */ x(Ae, { ...i, key: e }, /* @__PURE__ */ w("div", { className: "grid gap-1", children: [t && /* @__PURE__ */ C(Ne, { children: t }), n && /* @__PURE__ */ C(Pe, { children: n })] }), r, /* @__PURE__ */ C(Me, {}))), /* @__PURE__ */ C(De, {})] }); } //#endregion export { Fe as t }; //# sourceMappingURL=toaster-BRuuk4tH.js.map