@hoosei/voxweave-react
Version:
A customizable and interactive voice UI component for React applications
534 lines (533 loc) • 18.4 kB
JavaScript
import * as c from "react";
import { Primitive as L } from "./index35.js";
import { Presence as I } from "./index34.js";
import { createContextScope as ne } from "./index28.js";
import { useComposedRefs as A } from "./index24.js";
import { useCallbackRef as C } from "./index40.js";
import { useDirection as le } from "./index77.js";
import { useLayoutEffect as ce } from "./index39.js";
import { clamp as ie } from "./index75.js";
import { composeEventHandlers as R } from "./index27.js";
import { jsx as b, jsxs as se, Fragment as ae } from "react/jsx-runtime";
function de(e, t) {
return c.useReducer((r, l) => t[r][l] ?? r, e);
}
var V = "ScrollArea", [j, He] = ne(V), [ue, p] = j(V), q = c.forwardRef(
(e, t) => {
const {
__scopeScrollArea: r,
type: l = "hover",
dir: o,
scrollHideDelay: n = 600,
...i
} = e, [s, a] = c.useState(null), [f, d] = c.useState(null), [h, u] = c.useState(null), [S, w] = c.useState(null), [y, M] = c.useState(null), [P, _] = c.useState(0), [U, D] = c.useState(0), [W, x] = c.useState(!1), [H, z] = c.useState(!1), m = A(t, (E) => a(E)), v = le(o);
return /* @__PURE__ */ b(
ue,
{
scope: r,
type: l,
dir: v,
scrollHideDelay: n,
scrollArea: s,
viewport: f,
onViewportChange: d,
content: h,
onContentChange: u,
scrollbarX: S,
onScrollbarXChange: w,
scrollbarXEnabled: W,
onScrollbarXEnabledChange: x,
scrollbarY: y,
onScrollbarYChange: M,
scrollbarYEnabled: H,
onScrollbarYEnabledChange: z,
onCornerWidthChange: _,
onCornerHeightChange: D,
children: /* @__PURE__ */ b(
L.div,
{
dir: v,
...i,
ref: m,
style: {
position: "relative",
// Pass corner sizes as CSS vars to reduce re-renders of context consumers
"--radix-scroll-area-corner-width": P + "px",
"--radix-scroll-area-corner-height": U + "px",
...e.style
}
}
)
}
);
}
);
q.displayName = V;
var $ = "ScrollAreaViewport", G = c.forwardRef(
(e, t) => {
const { __scopeScrollArea: r, children: l, nonce: o, ...n } = e, i = p($, r), s = c.useRef(null), a = A(t, s, i.onViewportChange);
return /* @__PURE__ */ se(ae, { children: [
/* @__PURE__ */ b(
"style",
{
dangerouslySetInnerHTML: {
__html: "[data-radix-scroll-area-viewport]{scrollbar-width:none;-ms-overflow-style:none;-webkit-overflow-scrolling:touch;}[data-radix-scroll-area-viewport]::-webkit-scrollbar{display:none}"
},
nonce: o
}
),
/* @__PURE__ */ b(
L.div,
{
"data-radix-scroll-area-viewport": "",
...n,
ref: a,
style: {
/**
* We don't support `visible` because the intention is to have at least one scrollbar
* if this component is used and `visible` will behave like `auto` in that case
* https://developer.mozilla.org/en-US/docs/Web/CSS/overflow#description
*
* We don't handle `auto` because the intention is for the native implementation
* to be hidden if using this component. We just want to ensure the node is scrollable
* so could have used either `scroll` or `auto` here. We picked `scroll` to prevent
* the browser from having to work out whether to render native scrollbars or not,
* we tell it to with the intention of hiding them in CSS.
*/
overflowX: i.scrollbarXEnabled ? "scroll" : "hidden",
overflowY: i.scrollbarYEnabled ? "scroll" : "hidden",
...e.style
},
children: /* @__PURE__ */ b("div", { ref: i.onContentChange, style: { minWidth: "100%", display: "table" }, children: l })
}
)
] });
}
);
G.displayName = $;
var g = "ScrollAreaScrollbar", J = c.forwardRef(
(e, t) => {
const { forceMount: r, ...l } = e, o = p(g, e.__scopeScrollArea), { onScrollbarXEnabledChange: n, onScrollbarYEnabledChange: i } = o, s = e.orientation === "horizontal";
return c.useEffect(() => (s ? n(!0) : i(!0), () => {
s ? n(!1) : i(!1);
}), [s, n, i]), o.type === "hover" ? /* @__PURE__ */ b(fe, { ...l, ref: t, forceMount: r }) : o.type === "scroll" ? /* @__PURE__ */ b(he, { ...l, ref: t, forceMount: r }) : o.type === "auto" ? /* @__PURE__ */ b(K, { ...l, ref: t, forceMount: r }) : o.type === "always" ? /* @__PURE__ */ b(k, { ...l, ref: t }) : null;
}
);
J.displayName = g;
var fe = c.forwardRef((e, t) => {
const { forceMount: r, ...l } = e, o = p(g, e.__scopeScrollArea), [n, i] = c.useState(!1);
return c.useEffect(() => {
const s = o.scrollArea;
let a = 0;
if (s) {
const f = () => {
window.clearTimeout(a), i(!0);
}, d = () => {
a = window.setTimeout(() => i(!1), o.scrollHideDelay);
};
return s.addEventListener("pointerenter", f), s.addEventListener("pointerleave", d), () => {
window.clearTimeout(a), s.removeEventListener("pointerenter", f), s.removeEventListener("pointerleave", d);
};
}
}, [o.scrollArea, o.scrollHideDelay]), /* @__PURE__ */ b(I, { present: r || n, children: /* @__PURE__ */ b(
K,
{
"data-state": n ? "visible" : "hidden",
...l,
ref: t
}
) });
}), he = c.forwardRef((e, t) => {
const { forceMount: r, ...l } = e, o = p(g, e.__scopeScrollArea), n = e.orientation === "horizontal", i = Y(() => a("SCROLL_END"), 100), [s, a] = de("hidden", {
hidden: {
SCROLL: "scrolling"
},
scrolling: {
SCROLL_END: "idle",
POINTER_ENTER: "interacting"
},
interacting: {
SCROLL: "interacting",
POINTER_LEAVE: "idle"
},
idle: {
HIDE: "hidden",
SCROLL: "scrolling",
POINTER_ENTER: "interacting"
}
});
return c.useEffect(() => {
if (s === "idle") {
const f = window.setTimeout(() => a("HIDE"), o.scrollHideDelay);
return () => window.clearTimeout(f);
}
}, [s, o.scrollHideDelay, a]), c.useEffect(() => {
const f = o.viewport, d = n ? "scrollLeft" : "scrollTop";
if (f) {
let h = f[d];
const u = () => {
const S = f[d];
h !== S && (a("SCROLL"), i()), h = S;
};
return f.addEventListener("scroll", u), () => f.removeEventListener("scroll", u);
}
}, [o.viewport, n, a, i]), /* @__PURE__ */ b(I, { present: r || s !== "hidden", children: /* @__PURE__ */ b(
k,
{
"data-state": s === "hidden" ? "hidden" : "visible",
...l,
ref: t,
onPointerEnter: R(e.onPointerEnter, () => a("POINTER_ENTER")),
onPointerLeave: R(e.onPointerLeave, () => a("POINTER_LEAVE"))
}
) });
}), K = c.forwardRef((e, t) => {
const r = p(g, e.__scopeScrollArea), { forceMount: l, ...o } = e, [n, i] = c.useState(!1), s = e.orientation === "horizontal", a = Y(() => {
if (r.viewport) {
const f = r.viewport.offsetWidth < r.viewport.scrollWidth, d = r.viewport.offsetHeight < r.viewport.scrollHeight;
i(s ? f : d);
}
}, 10);
return T(r.viewport, a), T(r.content, a), /* @__PURE__ */ b(I, { present: l || n, children: /* @__PURE__ */ b(
k,
{
"data-state": n ? "visible" : "hidden",
...o,
ref: t
}
) });
}), k = c.forwardRef((e, t) => {
const { orientation: r = "vertical", ...l } = e, o = p(g, e.__scopeScrollArea), n = c.useRef(null), i = c.useRef(0), [s, a] = c.useState({
content: 0,
viewport: 0,
scrollbar: { size: 0, paddingStart: 0, paddingEnd: 0 }
}), f = re(s.viewport, s.content), d = {
...l,
sizes: s,
onSizesChange: a,
hasThumb: f > 0 && f < 1,
onThumbChange: (u) => n.current = u,
onThumbPointerUp: () => i.current = 0,
onThumbPointerDown: (u) => i.current = u
};
function h(u, S) {
return ge(u, i.current, s, S);
}
return r === "horizontal" ? /* @__PURE__ */ b(
be,
{
...d,
ref: t,
onThumbPositionChange: () => {
if (o.viewport && n.current) {
const u = o.viewport.scrollLeft, S = F(u, s, o.dir);
n.current.style.transform = `translate3d(${S}px, 0, 0)`;
}
},
onWheelScroll: (u) => {
o.viewport && (o.viewport.scrollLeft = u);
},
onDragScroll: (u) => {
o.viewport && (o.viewport.scrollLeft = h(u, o.dir));
}
}
) : r === "vertical" ? /* @__PURE__ */ b(
Se,
{
...d,
ref: t,
onThumbPositionChange: () => {
if (o.viewport && n.current) {
const u = o.viewport.scrollTop, S = F(u, s);
n.current.style.transform = `translate3d(0, ${S}px, 0)`;
}
},
onWheelScroll: (u) => {
o.viewport && (o.viewport.scrollTop = u);
},
onDragScroll: (u) => {
o.viewport && (o.viewport.scrollTop = h(u));
}
}
) : null;
}), be = c.forwardRef((e, t) => {
const { sizes: r, onSizesChange: l, ...o } = e, n = p(g, e.__scopeScrollArea), [i, s] = c.useState(), a = c.useRef(null), f = A(t, a, n.onScrollbarXChange);
return c.useEffect(() => {
a.current && s(getComputedStyle(a.current));
}, [a]), /* @__PURE__ */ b(
Z,
{
"data-orientation": "horizontal",
...o,
ref: f,
sizes: r,
style: {
bottom: 0,
left: n.dir === "rtl" ? "var(--radix-scroll-area-corner-width)" : 0,
right: n.dir === "ltr" ? "var(--radix-scroll-area-corner-width)" : 0,
"--radix-scroll-area-thumb-width": X(r) + "px",
...e.style
},
onThumbPointerDown: (d) => e.onThumbPointerDown(d.x),
onDragScroll: (d) => e.onDragScroll(d.x),
onWheelScroll: (d, h) => {
if (n.viewport) {
const u = n.viewport.scrollLeft + d.deltaX;
e.onWheelScroll(u), te(u, h) && d.preventDefault();
}
},
onResize: () => {
a.current && n.viewport && i && l({
content: n.viewport.scrollWidth,
viewport: n.viewport.offsetWidth,
scrollbar: {
size: a.current.clientWidth,
paddingStart: O(i.paddingLeft),
paddingEnd: O(i.paddingRight)
}
});
}
}
);
}), Se = c.forwardRef((e, t) => {
const { sizes: r, onSizesChange: l, ...o } = e, n = p(g, e.__scopeScrollArea), [i, s] = c.useState(), a = c.useRef(null), f = A(t, a, n.onScrollbarYChange);
return c.useEffect(() => {
a.current && s(getComputedStyle(a.current));
}, [a]), /* @__PURE__ */ b(
Z,
{
"data-orientation": "vertical",
...o,
ref: f,
sizes: r,
style: {
top: 0,
right: n.dir === "ltr" ? 0 : void 0,
left: n.dir === "rtl" ? 0 : void 0,
bottom: "var(--radix-scroll-area-corner-height)",
"--radix-scroll-area-thumb-height": X(r) + "px",
...e.style
},
onThumbPointerDown: (d) => e.onThumbPointerDown(d.y),
onDragScroll: (d) => e.onDragScroll(d.y),
onWheelScroll: (d, h) => {
if (n.viewport) {
const u = n.viewport.scrollTop + d.deltaY;
e.onWheelScroll(u), te(u, h) && d.preventDefault();
}
},
onResize: () => {
a.current && n.viewport && i && l({
content: n.viewport.scrollHeight,
viewport: n.viewport.offsetHeight,
scrollbar: {
size: a.current.clientHeight,
paddingStart: O(i.paddingTop),
paddingEnd: O(i.paddingBottom)
}
});
}
}
);
}), [me, Q] = j(g), Z = c.forwardRef((e, t) => {
const {
__scopeScrollArea: r,
sizes: l,
hasThumb: o,
onThumbChange: n,
onThumbPointerUp: i,
onThumbPointerDown: s,
onThumbPositionChange: a,
onDragScroll: f,
onWheelScroll: d,
onResize: h,
...u
} = e, S = p(g, r), [w, y] = c.useState(null), M = A(t, (m) => y(m)), P = c.useRef(null), _ = c.useRef(""), U = S.viewport, D = l.content - l.viewport, W = C(d), x = C(a), H = Y(h, 10);
function z(m) {
if (P.current) {
const v = m.clientX - P.current.left, E = m.clientY - P.current.top;
f({ x: v, y: E });
}
}
return c.useEffect(() => {
const m = (v) => {
const E = v.target;
w?.contains(E) && W(v, D);
};
return document.addEventListener("wheel", m, { passive: !1 }), () => document.removeEventListener("wheel", m, { passive: !1 });
}, [U, w, D, W]), c.useEffect(x, [l, x]), T(w, H), T(S.content, H), /* @__PURE__ */ b(
me,
{
scope: r,
scrollbar: w,
hasThumb: o,
onThumbChange: C(n),
onThumbPointerUp: C(i),
onThumbPositionChange: x,
onThumbPointerDown: C(s),
children: /* @__PURE__ */ b(
L.div,
{
...u,
ref: M,
style: { position: "absolute", ...u.style },
onPointerDown: R(e.onPointerDown, (m) => {
m.button === 0 && (m.target.setPointerCapture(m.pointerId), P.current = w.getBoundingClientRect(), _.current = document.body.style.webkitUserSelect, document.body.style.webkitUserSelect = "none", S.viewport && (S.viewport.style.scrollBehavior = "auto"), z(m));
}),
onPointerMove: R(e.onPointerMove, z),
onPointerUp: R(e.onPointerUp, (m) => {
const v = m.target;
v.hasPointerCapture(m.pointerId) && v.releasePointerCapture(m.pointerId), document.body.style.webkitUserSelect = _.current, S.viewport && (S.viewport.style.scrollBehavior = ""), P.current = null;
})
}
)
}
);
}), N = "ScrollAreaThumb", ee = c.forwardRef(
(e, t) => {
const { forceMount: r, ...l } = e, o = Q(N, e.__scopeScrollArea);
return /* @__PURE__ */ b(I, { present: r || o.hasThumb, children: /* @__PURE__ */ b(pe, { ref: t, ...l }) });
}
), pe = c.forwardRef(
(e, t) => {
const { __scopeScrollArea: r, style: l, ...o } = e, n = p(N, r), i = Q(N, r), { onThumbPositionChange: s } = i, a = A(
t,
(h) => i.onThumbChange(h)
), f = c.useRef(void 0), d = Y(() => {
f.current && (f.current(), f.current = void 0);
}, 100);
return c.useEffect(() => {
const h = n.viewport;
if (h) {
const u = () => {
if (d(), !f.current) {
const S = Pe(h, s);
f.current = S, s();
}
};
return s(), h.addEventListener("scroll", u), () => h.removeEventListener("scroll", u);
}
}, [n.viewport, d, s]), /* @__PURE__ */ b(
L.div,
{
"data-state": i.hasThumb ? "visible" : "hidden",
...o,
ref: a,
style: {
width: "var(--radix-scroll-area-thumb-width)",
height: "var(--radix-scroll-area-thumb-height)",
...l
},
onPointerDownCapture: R(e.onPointerDownCapture, (h) => {
const S = h.target.getBoundingClientRect(), w = h.clientX - S.left, y = h.clientY - S.top;
i.onThumbPointerDown({ x: w, y });
}),
onPointerUp: R(e.onPointerUp, i.onThumbPointerUp)
}
);
}
);
ee.displayName = N;
var B = "ScrollAreaCorner", ve = c.forwardRef(
(e, t) => {
const r = p(B, e.__scopeScrollArea), l = !!(r.scrollbarX && r.scrollbarY);
return r.type !== "scroll" && l ? /* @__PURE__ */ b(we, { ...e, ref: t }) : null;
}
);
ve.displayName = B;
var we = c.forwardRef((e, t) => {
const { __scopeScrollArea: r, ...l } = e, o = p(B, r), [n, i] = c.useState(0), [s, a] = c.useState(0), f = !!(n && s);
return T(o.scrollbarX, () => {
const d = o.scrollbarX?.offsetHeight || 0;
o.onCornerHeightChange(d), a(d);
}), T(o.scrollbarY, () => {
const d = o.scrollbarY?.offsetWidth || 0;
o.onCornerWidthChange(d), i(d);
}), f ? /* @__PURE__ */ b(
L.div,
{
...l,
ref: t,
style: {
width: n,
height: s,
position: "absolute",
right: o.dir === "ltr" ? 0 : void 0,
left: o.dir === "rtl" ? 0 : void 0,
bottom: 0,
...e.style
}
}
) : null;
});
function O(e) {
return e ? parseInt(e, 10) : 0;
}
function re(e, t) {
const r = e / t;
return isNaN(r) ? 0 : r;
}
function X(e) {
const t = re(e.viewport, e.content), r = e.scrollbar.paddingStart + e.scrollbar.paddingEnd, l = (e.scrollbar.size - r) * t;
return Math.max(l, 18);
}
function ge(e, t, r, l = "ltr") {
const o = X(r), n = o / 2, i = t || n, s = o - i, a = r.scrollbar.paddingStart + i, f = r.scrollbar.size - r.scrollbar.paddingEnd - s, d = r.content - r.viewport, h = l === "ltr" ? [0, d] : [d * -1, 0];
return oe([a, f], h)(e);
}
function F(e, t, r = "ltr") {
const l = X(t), o = t.scrollbar.paddingStart + t.scrollbar.paddingEnd, n = t.scrollbar.size - o, i = t.content - t.viewport, s = n - l, a = r === "ltr" ? [0, i] : [i * -1, 0], f = ie(e, a);
return oe([0, i], [0, s])(f);
}
function oe(e, t) {
return (r) => {
if (e[0] === e[1] || t[0] === t[1]) return t[0];
const l = (t[1] - t[0]) / (e[1] - e[0]);
return t[0] + l * (r - e[0]);
};
}
function te(e, t) {
return e > 0 && e < t;
}
var Pe = (e, t = () => {
}) => {
let r = { left: e.scrollLeft, top: e.scrollTop }, l = 0;
return function o() {
const n = { left: e.scrollLeft, top: e.scrollTop }, i = r.left !== n.left, s = r.top !== n.top;
(i || s) && t(), r = n, l = window.requestAnimationFrame(o);
}(), () => window.cancelAnimationFrame(l);
};
function Y(e, t) {
const r = C(e), l = c.useRef(0);
return c.useEffect(() => () => window.clearTimeout(l.current), []), c.useCallback(() => {
window.clearTimeout(l.current), l.current = window.setTimeout(r, t);
}, [r, t]);
}
function T(e, t) {
const r = C(t);
ce(() => {
let l = 0;
if (e) {
const o = new ResizeObserver(() => {
cancelAnimationFrame(l), l = window.requestAnimationFrame(r);
});
return o.observe(e), () => {
window.cancelAnimationFrame(l), o.unobserve(e);
};
}
}, [e, r]);
}
var ze = q, Ne = G, Oe = J, Ie = ee;
export {
ze as Root,
q as ScrollArea,
ve as ScrollAreaCorner,
J as ScrollAreaScrollbar,
ee as ScrollAreaThumb,
G as ScrollAreaViewport,
Oe as Scrollbar,
Ie as Thumb,
Ne as Viewport,
He as createScrollAreaScope
};
//# sourceMappingURL=index54.js.map