UNPKG

@konstructio/ui

Version:

A set of reusable and customizable React components built for konstruct.io

362 lines (361 loc) 11.1 kB
import { n as P } from "./dist-C4El7dZ5.js"; import { a as E, r as ae, s as se, t as C } from "./dist-D_9sBzad.js"; import { t as le } from "./dist-DxDE5irg2.js"; import { t as de } from "./dist-X40eHC8j2.js"; import { t as ce } from "./dist-Cf-pDuuw.js"; import { t as ue } from "./dist-CZChgJLY.js"; import * as d from "react"; import { jsx as p, jsxs as fe } from "react/jsx-runtime"; function U(e, [t, n]) { return Math.min(n, Math.max(t, e)); } var F = ["PageUp", "PageDown"], Y = [ "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight" ], $ = { "from-left": [ "Home", "PageDown", "ArrowDown", "ArrowLeft" ], "from-right": [ "Home", "PageDown", "ArrowDown", "ArrowRight" ], "from-bottom": [ "Home", "PageDown", "ArrowDown", "ArrowLeft" ], "from-top": [ "Home", "PageDown", "ArrowUp", "ArrowLeft" ] }, D = "Slider", [N, me, Se] = ue(D), [j, Ve] = se(D, [Se]), [he, B] = j(D), X = d.forwardRef((e, t) => { const { name: n, min: o = 0, max: a = 100, step: l = 1, orientation: r = "horizontal", disabled: s = !1, minStepsBetweenThumbs: c = 0, defaultValue: g = [o], value: v, onValueChange: i = () => { }, onValueCommit: m = () => { }, inverted: w = !1, form: R, ...h } = e, f = d.useRef(/* @__PURE__ */ new Set()), S = d.useRef(0), x = r === "horizontal" ? pe : ge, [u = [], y] = ae({ prop: v, defaultProp: g, onChange: (b) => { [...f.current][S.current]?.focus(), i(b); } }), A = d.useRef(u); function oe(b) { _(b, ye(u, b)); } function re(b) { _(b, S.current); } function ie() { const b = A.current[S.current]; u[S.current] !== b && m(u); } function _(b, V, { commit: k } = { commit: !1 }) { const I = De(l), K = U(_e(Math.round((b - o) / l) * l + o, I), [o, a]); y((T = []) => { const M = be(T, K, V); if (Pe(M, c * l)) { S.current = M.indexOf(K); const O = String(M) !== String(T); return O && k && m(M), O ? M : T; } else return T; }); } return /* @__PURE__ */ p(he, { scope: e.__scopeSlider, name: n, disabled: s, min: o, max: a, valueIndexToChangeRef: S, thumbs: f.current, values: u, orientation: r, form: R, children: /* @__PURE__ */ p(N.Provider, { scope: e.__scopeSlider, children: /* @__PURE__ */ p(N.Slot, { scope: e.__scopeSlider, children: /* @__PURE__ */ p(x, { "aria-disabled": s, "data-disabled": s ? "" : void 0, ...h, ref: t, onPointerDown: E(h.onPointerDown, () => { s || (A.current = u); }), min: o, max: a, inverted: w, onSlideStart: s ? void 0 : oe, onSlideMove: s ? void 0 : re, onSlideEnd: s ? void 0 : ie, onHomeKeyDown: () => !s && _(o, 0, { commit: !0 }), onEndKeyDown: () => !s && _(a, u.length - 1, { commit: !0 }), onStepKeyDown: ({ event: b, direction: V }) => { if (!s) { const k = F.includes(b.key) || b.shiftKey && Y.includes(b.key) ? 10 : 1, I = S.current, K = u[I]; _(K + l * k * V, I, { commit: !0 }); } } }) }) }) }); }); X.displayName = D; var [G, W] = j(D, { startEdge: "left", endEdge: "right", size: "width", direction: 1 }), pe = d.forwardRef((e, t) => { const { min: n, max: o, dir: a, inverted: l, onSlideStart: r, onSlideMove: s, onSlideEnd: c, onStepKeyDown: g, ...v } = e, [i, m] = d.useState(null), w = P(t, (u) => m(u)), R = d.useRef(void 0), h = ce(a), f = h === "ltr", S = f && !l || !f && l; function x(u) { const y = R.current || i.getBoundingClientRect(), A = L([0, y.width], S ? [n, o] : [o, n]); return R.current = y, A(u - y.left); } return /* @__PURE__ */ p(G, { scope: e.__scopeSlider, startEdge: S ? "left" : "right", endEdge: S ? "right" : "left", direction: S ? 1 : -1, size: "width", children: /* @__PURE__ */ p(q, { dir: h, "data-orientation": "horizontal", ...v, ref: w, style: { ...v.style, "--radix-slider-thumb-transform": "translateX(-50%)" }, onSlideStart: (u) => { const y = x(u.clientX); r?.(y); }, onSlideMove: (u) => { const y = x(u.clientX); s?.(y); }, onSlideEnd: () => { R.current = void 0, c?.(); }, onStepKeyDown: (u) => { const y = $[S ? "from-left" : "from-right"].includes(u.key); g?.({ event: u, direction: y ? -1 : 1 }); } }) }); }), ge = d.forwardRef((e, t) => { const { min: n, max: o, inverted: a, onSlideStart: l, onSlideMove: r, onSlideEnd: s, onStepKeyDown: c, ...g } = e, v = d.useRef(null), i = P(t, v), m = d.useRef(void 0), w = !a; function R(h) { const f = m.current || v.current.getBoundingClientRect(), S = L([0, f.height], w ? [o, n] : [n, o]); return m.current = f, S(h - f.top); } return /* @__PURE__ */ p(G, { scope: e.__scopeSlider, startEdge: w ? "bottom" : "top", endEdge: w ? "top" : "bottom", size: "height", direction: w ? 1 : -1, children: /* @__PURE__ */ p(q, { "data-orientation": "vertical", ...g, ref: i, style: { ...g.style, "--radix-slider-thumb-transform": "translateY(50%)" }, onSlideStart: (h) => { const f = R(h.clientY); l?.(f); }, onSlideMove: (h) => { const f = R(h.clientY); r?.(f); }, onSlideEnd: () => { m.current = void 0, s?.(); }, onStepKeyDown: (h) => { const f = $[w ? "from-bottom" : "from-top"].includes(h.key); c?.({ event: h, direction: f ? -1 : 1 }); } }) }); }), q = d.forwardRef((e, t) => { const { __scopeSlider: n, onSlideStart: o, onSlideMove: a, onSlideEnd: l, onHomeKeyDown: r, onEndKeyDown: s, onStepKeyDown: c, ...g } = e, v = B(D, n); return /* @__PURE__ */ p(C.span, { ...g, ref: t, onKeyDown: E(e.onKeyDown, (i) => { i.key === "Home" ? (r(i), i.preventDefault()) : i.key === "End" ? (s(i), i.preventDefault()) : F.concat(Y).includes(i.key) && (c(i), i.preventDefault()); }), onPointerDown: E(e.onPointerDown, (i) => { const m = i.target; m.setPointerCapture(i.pointerId), i.preventDefault(), v.thumbs.has(m) ? m.focus() : o(i); }), onPointerMove: E(e.onPointerMove, (i) => { i.target.hasPointerCapture(i.pointerId) && a(i); }), onPointerUp: E(e.onPointerUp, (i) => { const m = i.target; m.hasPointerCapture(i.pointerId) && (m.releasePointerCapture(i.pointerId), l(i)); }) }); }), J = "SliderTrack", Q = d.forwardRef((e, t) => { const { __scopeSlider: n, ...o } = e, a = B(J, n); return /* @__PURE__ */ p(C.span, { "data-disabled": a.disabled ? "" : void 0, "data-orientation": a.orientation, ...o, ref: t }); }); Q.displayName = J; var z = "SliderRange", Z = d.forwardRef((e, t) => { const { __scopeSlider: n, ...o } = e, a = B(z, n), l = W(z, n), r = P(t, d.useRef(null)), s = a.values.length, c = a.values.map((i) => ne(i, a.min, a.max)), g = s > 1 ? Math.min(...c) : 0, v = 100 - Math.max(...c); return /* @__PURE__ */ p(C.span, { "data-orientation": a.orientation, "data-disabled": a.disabled ? "" : void 0, ...o, ref: r, style: { ...e.style, [l.startEdge]: g + "%", [l.endEdge]: v + "%" } }); }); Z.displayName = z; var H = "SliderThumb", ee = d.forwardRef((e, t) => { const n = me(e.__scopeSlider), [o, a] = d.useState(null), l = P(t, (s) => a(s)), r = d.useMemo(() => o ? n().findIndex((s) => s.ref.current === o) : -1, [n, o]); return /* @__PURE__ */ p(ve, { ...e, ref: l, index: r }); }), ve = d.forwardRef((e, t) => { const { __scopeSlider: n, index: o, name: a, ...l } = e, r = B(H, n), s = W(H, n), [c, g] = d.useState(null), v = P(t, (x) => g(x)), i = c ? r.form || !!c.closest("form") : !0, m = de(c), w = r.values[o], R = w === void 0 ? 0 : ne(w, r.min, r.max), h = Re(o, r.values.length), f = m?.[s.size], S = f ? xe(f, R, s.direction) : 0; return d.useEffect(() => { if (c) return r.thumbs.add(c), () => { r.thumbs.delete(c); }; }, [c, r.thumbs]), /* @__PURE__ */ fe("span", { style: { transform: "var(--radix-slider-thumb-transform)", position: "absolute", [s.startEdge]: `calc(${R}% + ${S}px)` }, children: [/* @__PURE__ */ p(N.ItemSlot, { scope: e.__scopeSlider, children: /* @__PURE__ */ p(C.span, { role: "slider", "aria-label": e["aria-label"] || h, "aria-valuemin": r.min, "aria-valuenow": w, "aria-valuemax": r.max, "aria-orientation": r.orientation, "data-orientation": r.orientation, "data-disabled": r.disabled ? "" : void 0, tabIndex: r.disabled ? void 0 : 0, ...l, ref: v, style: w === void 0 ? { display: "none" } : e.style, onFocus: E(e.onFocus, () => { r.valueIndexToChangeRef.current = o; }) }) }), i && /* @__PURE__ */ p(te, { name: a ?? (r.name ? r.name + (r.values.length > 1 ? "[]" : "") : void 0), form: r.form, value: w }, o)] }); }); ee.displayName = H; var we = "RadioBubbleInput", te = d.forwardRef(({ __scopeSlider: e, value: t, ...n }, o) => { const a = d.useRef(null), l = P(a, o), r = le(t); return d.useEffect(() => { const s = a.current; if (!s) return; const c = window.HTMLInputElement.prototype, g = Object.getOwnPropertyDescriptor(c, "value").set; if (r !== t && g) { const v = new Event("input", { bubbles: !0 }); g.call(s, t), s.dispatchEvent(v); } }, [r, t]), /* @__PURE__ */ p(C.input, { style: { display: "none" }, ...n, ref: l, defaultValue: t }); }); te.displayName = we; function be(e = [], t, n) { const o = [...e]; return o[n] = t, o.sort((a, l) => a - l); } function ne(e, t, n) { return U(100 / (n - t) * (e - t), [0, 100]); } function Re(e, t) { return t > 2 ? `Value ${e + 1} of ${t}` : t === 2 ? ["Minimum", "Maximum"][e] : void 0; } function ye(e, t) { if (e.length === 1) return 0; const n = e.map((a) => Math.abs(a - t)), o = Math.min(...n); return n.indexOf(o); } function xe(e, t, n) { const o = e / 2; return (o - L([0, 50], [0, o])(t) * n) * n; } function Ee(e) { return e.slice(0, -1).map((t, n) => e[n + 1] - t); } function Pe(e, t) { if (t > 0) { const n = Ee(e); return Math.min(...n) >= t; } return !0; } function L(e, t) { return (n) => { if (e[0] === e[1] || t[0] === t[1]) return t[0]; const o = (t[1] - t[0]) / (e[1] - e[0]); return t[0] + o * (n - e[0]); }; } function De(e) { return (String(e).split(".")[1] || "").length; } function _e(e, t) { const n = Math.pow(10, t); return Math.round(e * n) / n; } var ke = X, Ne = Q, ze = Z, He = ee; export { Ne as i, ke as n, He as r, ze as t };