@konstructio/ui
Version:
A set of reusable and customizable React components built for konstruct.io
484 lines (483 loc) • 18.5 kB
JavaScript
import { t as ge } from "./x-KjMYhcCb.js";
import { n as ie } from "./dist-C4El7dZ5.js";
import { t as Y } from "./dist-CMPkuWNy.js";
import { t as be } from "./dist-EV-1XXQr.js";
import { t as W } from "./utils-COaoD3PI.js";
import { CheckCircleFilledIcon as Se } from "./assets/icons/components/CheckCircleFilled.js";
import { ErrorIcon as Ne } from "./assets/icons/components/Error.js";
import { InformationOutlineIcon as Ae } from "./assets/icons/components/InformationOutline.js";
import { WarningTriangleIcon as _e } from "./assets/icons/components/WarningTriangle.js";
import { a as g, i as De, n as Ie, r as Le, s as Fe, t as _ } from "./dist-D_9sBzad.js";
import { t as $ } from "./dist-D6dB-_Oy.js";
import { i as Me, n as ke, t as Oe } from "./dist-AZfrfAUz.js";
import { t as Ve } from "./dist-DF7Yi2EK.js";
import { t as Ke } from "./dist-CZChgJLY.js";
import { closeToastVariants as We, toastVariants as He, viewportToastVariants as $e } from "./components/Toast/Toast.variants.js";
import * as s from "react";
import { isValidElement as z, useCallback as Ue, useEffect as Xe, useMemo as ne, useRef as Ye } from "react";
import * as ze from "react-dom";
import { Fragment as ce, jsx as n, jsxs as A } from "react/jsx-runtime";
var je = Object.freeze({
position: "absolute",
border: 0,
width: 1,
height: 1,
padding: 0,
margin: -1,
overflow: "hidden",
clip: "rect(0, 0, 0, 0)",
whiteSpace: "nowrap",
wordWrap: "normal"
}), Be = "VisuallyHidden", J = s.forwardRef((e, r) => /* @__PURE__ */ n(_.span, {
...e,
ref: r,
style: {
...je,
...e.style
}
}));
J.displayName = Be;
var Q = "ToastProvider", [Z, qe, Ge] = Ke("Toast"), [le, Ft] = Fe("Toast", [Ge]), [Je, U] = le(Q), ue = (e) => {
const { __scopeToast: r, label: t = "Notification", duration: o = 5e3, swipeDirection: c = "right", swipeThreshold: d = 50, children: p } = e, [m, v] = s.useState(null), [a, h] = s.useState(0), w = s.useRef(!1), x = s.useRef(!1);
return t.trim() || console.error(`Invalid prop \`label\` supplied to \`${Q}\`. Expected non-empty \`string\`.`), /* @__PURE__ */ n(Z.Provider, {
scope: r,
children: /* @__PURE__ */ n(Je, {
scope: r,
label: t,
duration: o,
swipeDirection: c,
swipeThreshold: d,
toastCount: a,
viewport: m,
onViewportChange: v,
onToastAdd: s.useCallback(() => h((R) => R + 1), []),
onToastRemove: s.useCallback(() => h((R) => R - 1), []),
isFocusedToastEscapeKeyDownRef: w,
isClosePausedRef: x,
children: p
})
});
};
ue.displayName = Q;
var de = "ToastViewport", Qe = ["F8"], B = "toast.viewportPause", q = "toast.viewportResume", fe = s.forwardRef((e, r) => {
const { __scopeToast: t, hotkey: o = Qe, label: c = "Notifications ({hotkey})", ...d } = e, p = U(de, t), m = qe(t), v = s.useRef(null), a = s.useRef(null), h = s.useRef(null), w = s.useRef(null), x = ie(r, w, p.onViewportChange), R = o.join("+").replace(/Key/g, "").replace(/Digit/g, ""), b = p.toastCount > 0;
s.useEffect(() => {
const u = (E) => {
o.length !== 0 && o.every((f) => E[f] || E.code === f) && w.current?.focus();
};
return document.addEventListener("keydown", u), () => document.removeEventListener("keydown", u);
}, [o]), s.useEffect(() => {
const u = v.current, E = w.current;
if (b && u && E) {
const f = () => {
if (!p.isClosePausedRef.current) {
const P = new CustomEvent(B);
E.dispatchEvent(P), p.isClosePausedRef.current = !0;
}
}, T = () => {
if (p.isClosePausedRef.current) {
const P = new CustomEvent(q);
E.dispatchEvent(P), p.isClosePausedRef.current = !1;
}
}, y = (P) => {
u.contains(P.relatedTarget) || T();
}, C = () => {
u.contains(document.activeElement) || T();
};
return u.addEventListener("focusin", f), u.addEventListener("focusout", y), u.addEventListener("pointermove", f), u.addEventListener("pointerleave", C), window.addEventListener("blur", f), window.addEventListener("focus", T), () => {
u.removeEventListener("focusin", f), u.removeEventListener("focusout", y), u.removeEventListener("pointermove", f), u.removeEventListener("pointerleave", C), window.removeEventListener("blur", f), window.removeEventListener("focus", T);
};
}
}, [b, p.isClosePausedRef]);
const l = s.useCallback(({ tabbingDirection: u }) => {
const E = m().map((f) => {
const T = f.ref.current, y = [T, ...dt(T)];
return u === "forwards" ? y : y.reverse();
});
return (u === "forwards" ? E.reverse() : E).flat();
}, [m]);
return s.useEffect(() => {
const u = w.current;
if (u) {
const E = (f) => {
const T = f.altKey || f.ctrlKey || f.metaKey;
if (f.key === "Tab" && !T) {
const y = document.activeElement, C = f.shiftKey;
if (f.target === u && C) {
a.current?.focus();
return;
}
const P = l({ tabbingDirection: C ? "backwards" : "forwards" }), D = P.findIndex((I) => I === y);
j(P.slice(D + 1)) ? f.preventDefault() : C ? a.current?.focus() : h.current?.focus();
}
};
return u.addEventListener("keydown", E), () => u.removeEventListener("keydown", E);
}
}, [m, l]), /* @__PURE__ */ A(ke, {
ref: v,
role: "region",
"aria-label": c.replace("{hotkey}", R),
tabIndex: -1,
style: { pointerEvents: b ? void 0 : "none" },
children: [
b && /* @__PURE__ */ n(G, {
ref: a,
onFocusFromOutsideViewport: () => {
j(l({ tabbingDirection: "forwards" }));
}
}),
/* @__PURE__ */ n(Z.Slot, {
scope: t,
children: /* @__PURE__ */ n(_.ol, {
tabIndex: -1,
...d,
ref: x
})
}),
b && /* @__PURE__ */ n(G, {
ref: h,
onFocusFromOutsideViewport: () => {
j(l({ tabbingDirection: "backwards" }));
}
})
]
});
});
fe.displayName = de;
var pe = "ToastFocusProxy", G = s.forwardRef((e, r) => {
const { __scopeToast: t, onFocusFromOutsideViewport: o, ...c } = e, d = U(pe, t);
return /* @__PURE__ */ n(J, {
tabIndex: 0,
...c,
ref: r,
style: { position: "fixed" },
onFocus: (p) => {
const m = p.relatedTarget;
d.viewport?.contains(m) || o();
}
});
});
G.displayName = pe;
var k = "Toast", Ze = "toast.swipeStart", et = "toast.swipeMove", tt = "toast.swipeCancel", rt = "toast.swipeEnd", me = s.forwardRef((e, r) => {
const { forceMount: t, open: o, defaultOpen: c, onOpenChange: d, ...p } = e, [m, v] = Le({
prop: o,
defaultProp: c ?? !0,
onChange: d,
caller: k
});
return /* @__PURE__ */ n(Ve, {
present: t || m,
children: /* @__PURE__ */ n(st, {
open: m,
...p,
ref: r,
onClose: () => v(!1),
onPause: $(e.onPause),
onResume: $(e.onResume),
onSwipeStart: g(e.onSwipeStart, (a) => {
a.currentTarget.setAttribute("data-swipe", "start");
}),
onSwipeMove: g(e.onSwipeMove, (a) => {
const { x: h, y: w } = a.detail.delta;
a.currentTarget.setAttribute("data-swipe", "move"), a.currentTarget.style.setProperty("--radix-toast-swipe-move-x", `${h}px`), a.currentTarget.style.setProperty("--radix-toast-swipe-move-y", `${w}px`);
}),
onSwipeCancel: g(e.onSwipeCancel, (a) => {
a.currentTarget.setAttribute("data-swipe", "cancel"), a.currentTarget.style.removeProperty("--radix-toast-swipe-move-x"), a.currentTarget.style.removeProperty("--radix-toast-swipe-move-y"), a.currentTarget.style.removeProperty("--radix-toast-swipe-end-x"), a.currentTarget.style.removeProperty("--radix-toast-swipe-end-y");
}),
onSwipeEnd: g(e.onSwipeEnd, (a) => {
const { x: h, y: w } = a.detail.delta;
a.currentTarget.setAttribute("data-swipe", "end"), a.currentTarget.style.removeProperty("--radix-toast-swipe-move-x"), a.currentTarget.style.removeProperty("--radix-toast-swipe-move-y"), a.currentTarget.style.setProperty("--radix-toast-swipe-end-x", `${h}px`), a.currentTarget.style.setProperty("--radix-toast-swipe-end-y", `${w}px`), v(!1);
})
})
});
});
me.displayName = k;
var [ot, nt] = le(k, { onClose() {
} }), st = s.forwardRef((e, r) => {
const { __scopeToast: t, type: o = "foreground", duration: c, open: d, onClose: p, onEscapeKeyDown: m, onPause: v, onResume: a, onSwipeStart: h, onSwipeMove: w, onSwipeCancel: x, onSwipeEnd: R, ...b } = e, l = U(k, t), [u, E] = s.useState(null), f = ie(r, (i) => E(i)), T = s.useRef(null), y = s.useRef(null), C = c || l.duration, P = s.useRef(0), D = s.useRef(C), I = s.useRef(0), { onToastAdd: ee, onToastRemove: te } = l, L = $(() => {
u?.contains(document.activeElement) && l.viewport?.focus(), p();
}), O = s.useCallback((i) => {
!i || i === 1 / 0 || (window.clearTimeout(I.current), P.current = (/* @__PURE__ */ new Date()).getTime(), I.current = window.setTimeout(L, i));
}, [L]);
s.useEffect(() => {
const i = l.viewport;
if (i) {
const S = () => {
O(D.current), a?.();
}, N = () => {
const F = (/* @__PURE__ */ new Date()).getTime() - P.current;
D.current = D.current - F, window.clearTimeout(I.current), v?.();
};
return i.addEventListener(B, N), i.addEventListener(q, S), () => {
i.removeEventListener(B, N), i.removeEventListener(q, S);
};
}
}, [
l.viewport,
C,
v,
a,
O
]), s.useEffect(() => {
d && !l.isClosePausedRef.current && O(C);
}, [
d,
C,
l.isClosePausedRef,
O
]), s.useEffect(() => (ee(), () => te()), [ee, te]);
const re = s.useMemo(() => u ? Ce(u) : null, [u]);
return l.viewport ? /* @__PURE__ */ A(ce, { children: [re && /* @__PURE__ */ n(at, {
__scopeToast: t,
role: "status",
"aria-live": o === "foreground" ? "assertive" : "polite",
children: re
}), /* @__PURE__ */ n(ot, {
scope: t,
onClose: L,
children: ze.createPortal(/* @__PURE__ */ n(Z.ItemSlot, {
scope: t,
children: /* @__PURE__ */ n(Me, {
asChild: !0,
onEscapeKeyDown: g(m, () => {
l.isFocusedToastEscapeKeyDownRef.current || L(), l.isFocusedToastEscapeKeyDownRef.current = !1;
}),
children: /* @__PURE__ */ n(_.li, {
tabIndex: 0,
"data-state": d ? "open" : "closed",
"data-swipe-direction": l.swipeDirection,
...b,
ref: f,
style: {
userSelect: "none",
touchAction: "none",
...e.style
},
onKeyDown: g(e.onKeyDown, (i) => {
i.key === "Escape" && (m?.(i.nativeEvent), i.nativeEvent.defaultPrevented || (l.isFocusedToastEscapeKeyDownRef.current = !0, L()));
}),
onPointerDown: g(e.onPointerDown, (i) => {
i.button === 0 && (T.current = {
x: i.clientX,
y: i.clientY
});
}),
onPointerMove: g(e.onPointerMove, (i) => {
if (!T.current) return;
const S = i.clientX - T.current.x, N = i.clientY - T.current.y, F = !!y.current, M = ["left", "right"].includes(l.swipeDirection), V = ["left", "up"].includes(l.swipeDirection) ? Math.min : Math.max, Pe = M ? V(0, S) : 0, Re = M ? 0 : V(0, N), X = i.pointerType === "touch" ? 10 : 2, K = {
x: Pe,
y: Re
}, oe = {
originalEvent: i,
delta: K
};
F ? (y.current = K, H(et, w, oe, { discrete: !1 })) : se(K, l.swipeDirection, X) ? (y.current = K, H(Ze, h, oe, { discrete: !1 }), i.target.setPointerCapture(i.pointerId)) : (Math.abs(S) > X || Math.abs(N) > X) && (T.current = null);
}),
onPointerUp: g(e.onPointerUp, (i) => {
const S = y.current, N = i.target;
if (N.hasPointerCapture(i.pointerId) && N.releasePointerCapture(i.pointerId), y.current = null, T.current = null, S) {
const F = i.currentTarget, M = {
originalEvent: i,
delta: S
};
se(S, l.swipeDirection, l.swipeThreshold) ? H(rt, R, M, { discrete: !0 }) : H(tt, x, M, { discrete: !0 }), F.addEventListener("click", (V) => V.preventDefault(), { once: !0 });
}
})
})
})
}), l.viewport)
})] }) : null;
}), at = (e) => {
const { __scopeToast: r, children: t, ...o } = e, c = U(k, r), [d, p] = s.useState(!1), [m, v] = s.useState(!1);
return lt(() => p(!0)), s.useEffect(() => {
const a = window.setTimeout(() => v(!0), 1e3);
return () => window.clearTimeout(a);
}, []), m ? null : /* @__PURE__ */ n(Oe, {
asChild: !0,
children: /* @__PURE__ */ n(J, {
...o,
children: d && /* @__PURE__ */ A(ce, { children: [
c.label,
" ",
t
] })
})
});
}, it = "ToastTitle", we = s.forwardRef((e, r) => {
const { __scopeToast: t, ...o } = e;
return /* @__PURE__ */ n(_.div, {
...o,
ref: r
});
});
we.displayName = it;
var ct = "ToastDescription", Te = s.forwardRef((e, r) => {
const { __scopeToast: t, ...o } = e;
return /* @__PURE__ */ n(_.div, {
...o,
ref: r
});
});
Te.displayName = ct;
var ve = "ToastAction", Ee = s.forwardRef((e, r) => {
const { altText: t, ...o } = e;
return t.trim() ? /* @__PURE__ */ n(xe, {
altText: t,
asChild: !0,
children: /* @__PURE__ */ n(ye, {
...o,
ref: r
})
}) : (console.error(`Invalid prop \`altText\` supplied to \`${ve}\`. Expected non-empty \`string\`.`), null);
});
Ee.displayName = ve;
var he = "ToastClose", ye = s.forwardRef((e, r) => {
const { __scopeToast: t, ...o } = e, c = nt(he, t);
return /* @__PURE__ */ n(xe, {
asChild: !0,
children: /* @__PURE__ */ n(_.button, {
type: "button",
...o,
ref: r,
onClick: g(e.onClick, c.onClose)
})
});
});
ye.displayName = he;
var xe = s.forwardRef((e, r) => {
const { __scopeToast: t, altText: o, ...c } = e;
return /* @__PURE__ */ n(_.div, {
"data-radix-toast-announce-exclude": "",
"data-radix-toast-announce-alt": o || void 0,
...c,
ref: r
});
});
function Ce(e) {
const r = [];
return Array.from(e.childNodes).forEach((t) => {
if (t.nodeType === t.TEXT_NODE && t.textContent && r.push(t.textContent), ut(t)) {
const o = t.ariaHidden || t.hidden || t.style.display === "none", c = t.dataset.radixToastAnnounceExclude === "";
if (!o) if (c) {
const d = t.dataset.radixToastAnnounceAlt;
d && r.push(d);
} else r.push(...Ce(t));
}
}), r;
}
function H(e, r, t, { discrete: o }) {
const c = t.originalEvent.currentTarget, d = new CustomEvent(e, {
bubbles: !0,
cancelable: !0,
detail: t
});
r && c.addEventListener(e, r, { once: !0 }), o ? Ie(c, d) : c.dispatchEvent(d);
}
var se = (e, r, t = 0) => {
const o = Math.abs(e.x), c = Math.abs(e.y), d = o > c;
return r === "left" || r === "right" ? d && o > t : !d && c > t;
};
function lt(e = () => {
}) {
const r = $(e);
De(() => {
let t = 0, o = 0;
return t = window.requestAnimationFrame(() => o = window.requestAnimationFrame(r)), () => {
window.cancelAnimationFrame(t), window.cancelAnimationFrame(o);
};
}, [r]);
}
function ut(e) {
return e.nodeType === e.ELEMENT_NODE;
}
function dt(e) {
const r = [], t = document.createTreeWalker(e, NodeFilter.SHOW_ELEMENT, { acceptNode: (o) => {
const c = o.tagName === "INPUT" && o.type === "hidden";
return o.disabled || o.hidden || c ? NodeFilter.FILTER_SKIP : o.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
} });
for (; t.nextNode(); ) r.push(t.currentNode);
return r;
}
function j(e) {
const r = document.activeElement;
return e.some((t) => t === r ? !0 : (t.focus(), document.activeElement !== r));
}
var ft = ue, pt = fe, mt = me, wt = we, ae = Te, Tt = Ee, Mt = ({ title: e, duration: r = 5e3, titleClassName: t, descriptionClassName: o, description: c, children: d, theme: p, showCloseButton: m = !0, closeButtonClassName: v, className: a, open: h = !1, setOpen: w, variant: x }) => {
const R = Ye(0);
Xe(() => () => clearTimeout(R.current), []);
const b = ne(() => z(e) ? /* @__PURE__ */ n(Y, {
className: t,
children: e
}) : e, [e, t]), l = ne(() => z(c) ? /* @__PURE__ */ n(Y, {
className: o,
children: c
}) : c, [c, o]);
return /* @__PURE__ */ A(ft, {
swipeDirection: "right",
duration: r,
children: [
/* @__PURE__ */ n(Y, {
onClick: Ue(() => {
w(!1), R.current = window.setTimeout(() => w(!0), 100);
}, [w]),
children: d
}),
/* @__PURE__ */ A(mt, {
className: W(He({
variant: x,
className: a
})),
"data-theme": p,
open: h,
onOpenChange: w,
children: [/* @__PURE__ */ A("div", {
className: "flex gap-2 items-center flex-1",
children: [
x === "success" && /* @__PURE__ */ n(Se, {
size: 20,
className: "shrink-0"
}),
x === "error" && /* @__PURE__ */ n(Ne, {
size: 20,
className: "shrink-0"
}),
x === "warning" && /* @__PURE__ */ n(_e, {
size: 20,
className: "shrink-0"
}),
x === "info" && /* @__PURE__ */ n(Ae, {
size: 20,
className: "shrink-0"
}),
/* @__PURE__ */ A("div", {
className: "flex flex-col gap-1",
children: [/* @__PURE__ */ n(wt, {
asChild: !0,
className: t,
children: b
}), l && (z(l) ? /* @__PURE__ */ n(ae, {
asChild: !0,
children: l
}) : /* @__PURE__ */ n(ae, {
className: W("text-sm", o),
children: l
}))]
})
]
}), m && /* @__PURE__ */ n(Tt, {
asChild: !0,
altText: "Close the toast",
children: /* @__PURE__ */ A("button", {
type: "button",
className: "cursor-pointer",
children: [/* @__PURE__ */ n(ge, { className: W(We({ className: v })) }), /* @__PURE__ */ n(be, { children: "Close toast" })]
})
})]
}),
/* @__PURE__ */ n(pt, { className: W($e()) })
]
});
};
export {
Mt as t
};