UNPKG

@konstructio/ui

Version:

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

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