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