@konstructio/ui
Version:
A set of reusable and customizable React components built for konstruct.io
487 lines (481 loc) • 14.9 kB
JavaScript
import * as i from "react";
import { useState as te } from "react";
var y = function() {
return y = Object.assign || function(t) {
for (var r, n = 1, a = arguments.length; n < a; n++) {
r = arguments[n];
for (var c in r) Object.prototype.hasOwnProperty.call(r, c) && (t[c] = r[c]);
}
return t;
}, y.apply(this, arguments);
};
function Q(e, t) {
var r = {};
for (var n in e) Object.prototype.hasOwnProperty.call(e, n) && t.indexOf(n) < 0 && (r[n] = e[n]);
if (e != null && typeof Object.getOwnPropertySymbols == "function")
for (var a = 0, n = Object.getOwnPropertySymbols(e); a < n.length; a++) t.indexOf(n[a]) < 0 && Object.prototype.propertyIsEnumerable.call(e, n[a]) && (r[n[a]] = e[n[a]]);
return r;
}
function ne(e, t, r) {
if (r || arguments.length === 2)
for (var n = 0, a = t.length, c; n < a; n++) (c || !(n in t)) && (c || (c = Array.prototype.slice.call(t, 0, n)), c[n] = t[n]);
return e.concat(c || Array.prototype.slice.call(t));
}
var B = "right-scroll-bar-position", A = "width-before-scroll-bar", re = "with-scroll-bars-hidden", ae = "--removed-body-scroll-bar-size";
function W(e, t) {
return typeof e == "function" ? e(t) : e && (e.current = t), e;
}
function oe(e, t) {
var r = te(function() {
return {
value: e,
callback: t,
facade: {
get current() {
return r.value;
},
set current(n) {
var a = r.value;
a !== n && (r.value = n, r.callback(n, a));
}
}
};
})[0];
return r.callback = t, r.facade;
}
var ce = typeof window < "u" ? i.useLayoutEffect : i.useEffect, F = /* @__PURE__ */ new WeakMap();
function ie(e, t) {
var r = oe(t || null, function(n) {
return e.forEach(function(a) {
return W(a, n);
});
});
return ce(function() {
var n = F.get(r);
if (n) {
var a = new Set(n), c = new Set(e), f = r.current;
a.forEach(function(u) {
c.has(u) || W(u, null);
}), c.forEach(function(u) {
a.has(u) || W(u, f);
});
}
F.set(r, e);
}, [e]), r;
}
function ue(e) {
return e;
}
function le(e, t) {
t === void 0 && (t = ue);
var r = [], n = !1;
return {
read: function() {
if (n) throw new Error("Sidecar: could not `read` from an `assigned` medium. `read` could be used only with `useMedium`.");
return r.length ? r[r.length - 1] : e;
},
useMedium: function(a) {
var c = t(a, n);
return r.push(c), function() {
r = r.filter(function(f) {
return f !== c;
});
};
},
assignSyncMedium: function(a) {
for (n = !0; r.length; ) {
var c = r;
r = [], c.forEach(a);
}
r = {
push: function(f) {
return a(f);
},
filter: function() {
return r;
}
};
},
assignMedium: function(a) {
n = !0;
var c = [];
if (r.length) {
var f = r;
r = [], f.forEach(a), c = r;
}
var u = function() {
var s = c;
c = [], s.forEach(a);
}, v = function() {
return Promise.resolve().then(u);
};
v(), r = {
push: function(s) {
c.push(s), v();
},
filter: function(s) {
return c = c.filter(s), r;
}
};
}
};
}
function fe(e) {
e === void 0 && (e = {});
var t = le(null);
return t.options = y({
async: !0,
ssr: !1
}, e), t;
}
var U = function(e) {
var t = e.sideCar, r = Q(e, ["sideCar"]);
if (!t) throw new Error("Sidecar: please provide `sideCar` property to import the right car");
var n = t.read();
if (!n) throw new Error("Sidecar medium not found");
return i.createElement(n, y({}, r));
};
U.isSideCarExport = !0;
function se(e, t) {
return e.useMedium(t), U;
}
var K = fe(), D = function() {
}, I = i.forwardRef(function(e, t) {
var r = i.useRef(null), n = i.useState({
onScrollCapture: D,
onWheelCapture: D,
onTouchMoveCapture: D
}), a = n[0], c = n[1], f = e.forwardProps, u = e.children, v = e.className, s = e.removeScrollBar, p = e.enabled, b = e.shards, C = e.sideCar, E = e.noRelative, R = e.noIsolation, o = e.inert, l = e.allowPinchZoom, m = e.as, g = m === void 0 ? "div" : m, h = e.gapMode, d = Q(e, [
"forwardProps",
"children",
"className",
"removeScrollBar",
"enabled",
"shards",
"sideCar",
"noRelative",
"noIsolation",
"inert",
"allowPinchZoom",
"as",
"gapMode"
]), k = C, S = ie([r, t]), w = y(y({}, d), a);
return i.createElement(i.Fragment, null, p && i.createElement(k, {
sideCar: K,
removeScrollBar: s,
shards: b,
noRelative: E,
noIsolation: R,
inert: o,
setCallbacks: c,
allowPinchZoom: !!l,
lockRef: r,
gapMode: h
}), f ? i.cloneElement(i.Children.only(u), y(y({}, w), { ref: S })) : i.createElement(g, y({}, w, {
className: v,
ref: S
}), u));
});
I.defaultProps = {
enabled: !0,
removeScrollBar: !0,
inert: !1
};
I.classNames = {
fullWidth: A,
zeroRight: B
};
var z, de = function() {
if (z) return z;
if (typeof __webpack_nonce__ < "u") return __webpack_nonce__;
};
function ve() {
if (!document) return null;
var e = document.createElement("style");
e.type = "text/css";
var t = de();
return t && e.setAttribute("nonce", t), e;
}
function he(e, t) {
e.styleSheet ? e.styleSheet.cssText = t : e.appendChild(document.createTextNode(t));
}
function me(e) {
(document.head || document.getElementsByTagName("head")[0]).appendChild(e);
}
var ge = function() {
var e = 0, t = null;
return {
add: function(r) {
e == 0 && (t = ve()) && (he(t, r), me(t)), e++;
},
remove: function() {
e--, !e && t && (t.parentNode && t.parentNode.removeChild(t), t = null);
}
};
}, ye = function() {
var e = ge();
return function(t, r) {
i.useEffect(function() {
return e.add(t), function() {
e.remove();
};
}, [t && r]);
};
}, $ = function() {
var e = ye(), t = function(r) {
var n = r.styles, a = r.dynamic;
return e(n, a), null;
};
return t;
}, be = {
left: 0,
top: 0,
right: 0,
gap: 0
}, X = function(e) {
return parseInt(e || "", 10) || 0;
}, Se = function(e) {
var t = window.getComputedStyle(document.body), r = t[e === "padding" ? "paddingLeft" : "marginLeft"], n = t[e === "padding" ? "paddingTop" : "marginTop"], a = t[e === "padding" ? "paddingRight" : "marginRight"];
return [
X(r),
X(n),
X(a)
];
}, we = function(e) {
if (e === void 0 && (e = "margin"), typeof window > "u") return be;
var t = Se(e), r = document.documentElement.clientWidth, n = window.innerWidth;
return {
left: t[0],
top: t[1],
right: t[2],
gap: Math.max(0, n - r + t[2] - t[0])
};
}, pe = $(), T = "data-scroll-locked", Ce = function(e, t, r, n) {
var a = e.left, c = e.top, f = e.right, u = e.gap;
return r === void 0 && (r = "margin"), `
.`.concat(re, ` {
overflow: hidden `).concat(n, `;
padding-right: `).concat(u, "px ").concat(n, `;
}
body[`).concat(T, `] {
overflow: hidden `).concat(n, `;
overscroll-behavior: contain;
`).concat([
t && "position: relative ".concat(n, ";"),
r === "margin" && `
padding-left: `.concat(a, `px;
padding-top: `).concat(c, `px;
padding-right: `).concat(f, `px;
margin-left:0;
margin-top:0;
margin-right: `).concat(u, "px ").concat(n, `;
`),
r === "padding" && "padding-right: ".concat(u, "px ").concat(n, ";")
].filter(Boolean).join(""), `
}
.`).concat(B, ` {
right: `).concat(u, "px ").concat(n, `;
}
.`).concat(A, ` {
margin-right: `).concat(u, "px ").concat(n, `;
}
.`).concat(B, " .").concat(B, ` {
right: 0 `).concat(n, `;
}
.`).concat(A, " .").concat(A, ` {
margin-right: 0 `).concat(n, `;
}
body[`).concat(T, `] {
`).concat(ae, ": ").concat(u, `px;
}
`);
}, H = function() {
var e = parseInt(document.body.getAttribute("data-scroll-locked") || "0", 10);
return isFinite(e) ? e : 0;
}, Ee = function() {
i.useEffect(function() {
return document.body.setAttribute(T, (H() + 1).toString()), function() {
var e = H() - 1;
e <= 0 ? document.body.removeAttribute(T) : document.body.setAttribute(T, e.toString());
};
}, []);
}, Re = function(e) {
var t = e.noRelative, r = e.noImportant, n = e.gapMode, a = n === void 0 ? "margin" : n;
Ee();
var c = i.useMemo(function() {
return we(a);
}, [a]);
return i.createElement(pe, { styles: Ce(c, !t, a, r ? "" : "!important") });
}, Y = !1;
if (typeof window < "u") try {
var L = Object.defineProperty({}, "passive", { get: function() {
return Y = !0, !0;
} });
window.addEventListener("test", L, L), window.removeEventListener("test", L, L);
} catch {
Y = !1;
}
var P = Y ? { passive: !1 } : !1, ke = function(e) {
return e.tagName === "TEXTAREA";
}, q = function(e, t) {
if (!(e instanceof Element)) return !1;
var r = window.getComputedStyle(e);
return r[t] !== "hidden" && !(r.overflowY === r.overflowX && !ke(e) && r[t] === "visible");
}, Pe = function(e) {
return q(e, "overflowY");
}, Ne = function(e) {
return q(e, "overflowX");
}, Z = function(e, t) {
var r = t.ownerDocument, n = t;
do {
if (typeof ShadowRoot < "u" && n instanceof ShadowRoot && (n = n.host), J(e, n)) {
var a = ee(e, n);
if (a[1] > a[2]) return !0;
}
n = n.parentNode;
} while (n && n !== r.body);
return !1;
}, Te = function(e) {
return [
e.scrollTop,
e.scrollHeight,
e.clientHeight
];
}, Me = function(e) {
return [
e.scrollLeft,
e.scrollWidth,
e.clientWidth
];
}, J = function(e, t) {
return e === "v" ? Pe(t) : Ne(t);
}, ee = function(e, t) {
return e === "v" ? Te(t) : Me(t);
}, Le = function(e, t) {
return e === "h" && t === "rtl" ? -1 : 1;
}, xe = function(e, t, r, n, a) {
var c = Le(e, window.getComputedStyle(t).direction), f = c * n, u = r.target, v = t.contains(u), s = !1, p = f > 0, b = 0, C = 0;
do {
if (!u) break;
var E = ee(e, u), R = E[0], o = E[1] - E[2] - c * R;
(R || o) && J(e, u) && (b += o, C += R);
var l = u.parentNode;
u = l && l.nodeType === Node.DOCUMENT_FRAGMENT_NODE ? l.host : l;
} while (!v && u !== document.body || v && (t.contains(u) || t === u));
return (p && (a && Math.abs(b) < 1 || !a && f > b) || !p && (a && Math.abs(C) < 1 || !a && -f > C)) && (s = !0), s;
}, x = function(e) {
return "changedTouches" in e ? [e.changedTouches[0].clientX, e.changedTouches[0].clientY] : [0, 0];
}, _ = function(e) {
return [e.deltaX, e.deltaY];
}, G = function(e) {
return e && "current" in e ? e.current : e;
}, Be = function(e, t) {
return e[0] === t[0] && e[1] === t[1];
}, Ae = function(e) {
return `
.block-interactivity-`.concat(e, ` {pointer-events: none;}
.allow-interactivity-`).concat(e, ` {pointer-events: all;}
`);
}, Ie = 0, N = [];
function Oe(e) {
var t = i.useRef([]), r = i.useRef([0, 0]), n = i.useRef(), a = i.useState(Ie++)[0], c = i.useState($)[0], f = i.useRef(e);
i.useEffect(function() {
f.current = e;
}, [e]), i.useEffect(function() {
if (e.inert) {
document.body.classList.add("block-interactivity-".concat(a));
var o = ne([e.lockRef.current], (e.shards || []).map(G), !0).filter(Boolean);
return o.forEach(function(l) {
return l.classList.add("allow-interactivity-".concat(a));
}), function() {
document.body.classList.remove("block-interactivity-".concat(a)), o.forEach(function(l) {
return l.classList.remove("allow-interactivity-".concat(a));
});
};
}
}, [
e.inert,
e.lockRef.current,
e.shards
]);
var u = i.useCallback(function(o, l) {
if ("touches" in o && o.touches.length === 2 || o.type === "wheel" && o.ctrlKey) return !f.current.allowPinchZoom;
var m = x(o), g = r.current, h = "deltaX" in o ? o.deltaX : g[0] - m[0], d = "deltaY" in o ? o.deltaY : g[1] - m[1], k, S = o.target, w = Math.abs(h) > Math.abs(d) ? "h" : "v";
if ("touches" in o && w === "h" && S.type === "range") return !1;
var j = window.getSelection(), O = j && j.anchorNode;
if (O && (O === S || O.contains(S))) return !1;
var M = Z(w, S);
if (!M) return !0;
if (M ? k = w : (k = w === "v" ? "h" : "v", M = Z(w, S)), !M) return !1;
if (!n.current && "changedTouches" in o && (h || d) && (n.current = k), !k) return !0;
var V = n.current || k;
return xe(V, l, o, V === "h" ? h : d, !0);
}, []), v = i.useCallback(function(o) {
var l = o;
if (!(!N.length || N[N.length - 1] !== c)) {
var m = "deltaY" in l ? _(l) : x(l), g = t.current.filter(function(d) {
return d.name === l.type && (d.target === l.target || l.target === d.shadowParent) && Be(d.delta, m);
})[0];
if (g && g.should) {
l.cancelable && l.preventDefault();
return;
}
if (!g) {
var h = (f.current.shards || []).map(G).filter(Boolean).filter(function(d) {
return d.contains(l.target);
});
(h.length > 0 ? u(l, h[0]) : !f.current.noIsolation) && l.cancelable && l.preventDefault();
}
}
}, []), s = i.useCallback(function(o, l, m, g) {
var h = {
name: o,
delta: l,
target: m,
should: g,
shadowParent: We(m)
};
t.current.push(h), setTimeout(function() {
t.current = t.current.filter(function(d) {
return d !== h;
});
}, 1);
}, []), p = i.useCallback(function(o) {
r.current = x(o), n.current = void 0;
}, []), b = i.useCallback(function(o) {
s(o.type, _(o), o.target, u(o, e.lockRef.current));
}, []), C = i.useCallback(function(o) {
s(o.type, x(o), o.target, u(o, e.lockRef.current));
}, []);
i.useEffect(function() {
return N.push(c), e.setCallbacks({
onScrollCapture: b,
onWheelCapture: b,
onTouchMoveCapture: C
}), document.addEventListener("wheel", v, P), document.addEventListener("touchmove", v, P), document.addEventListener("touchstart", p, P), function() {
N = N.filter(function(o) {
return o !== c;
}), document.removeEventListener("wheel", v, P), document.removeEventListener("touchmove", v, P), document.removeEventListener("touchstart", p, P);
};
}, []);
var E = e.removeScrollBar, R = e.inert;
return i.createElement(i.Fragment, null, R ? i.createElement(c, { styles: Ae(a) }) : null, E ? i.createElement(Re, {
noRelative: e.noRelative,
gapMode: e.gapMode
}) : null);
}
function We(e) {
for (var t = null; e !== null; )
e instanceof ShadowRoot && (t = e.host, e = e.host), e = e.parentNode;
return t;
}
var De = se(K, Oe), Xe = i.forwardRef(function(e, t) {
return i.createElement(I, y({}, e, {
ref: t,
sideCar: De
}));
});
Xe.classNames = I.classNames;
export {
Xe as t
};