@hoosei/voxweave-react
Version:
A customizable and interactive voice UI component for React applications
531 lines (530 loc) • 14.3 kB
JavaScript
import { evaluate as T, getSideAxis as B, getSide as W, clamp as X, getOppositePlacement as ee, getExpandedPlacements as te, getOppositeAxisPlacements as ne, getAlignmentSides as ie, getAlignment as I, min as $, max as Y, getPaddingObject as K, rectToClientRect as _, getOppositeAxis as N, getAlignmentAxis as Q, sides as se, getAxisLength as U } from "./index100.js";
function q(t, e, d) {
let {
reference: a,
floating: r
} = t;
const n = B(e), o = Q(e), m = U(o), u = W(e), h = n === "y", c = a.x + a.width / 2 - r.width / 2, s = a.y + a.height / 2 - r.height / 2, f = a[m] / 2 - r[m] / 2;
let i;
switch (u) {
case "top":
i = {
x: c,
y: a.y - r.height
};
break;
case "bottom":
i = {
x: c,
y: a.y + a.height
};
break;
case "right":
i = {
x: a.x + a.width,
y: s
};
break;
case "left":
i = {
x: a.x - r.width,
y: s
};
break;
default:
i = {
x: a.x,
y: a.y
};
}
switch (I(e)) {
case "start":
i[o] -= f * (d && h ? -1 : 1);
break;
case "end":
i[o] += f * (d && h ? -1 : 1);
break;
}
return i;
}
const ce = async (t, e, d) => {
const {
placement: a = "bottom",
strategy: r = "absolute",
middleware: n = [],
platform: o
} = d, m = n.filter(Boolean), u = await (o.isRTL == null ? void 0 : o.isRTL(e));
let h = await o.getElementRects({
reference: t,
floating: e,
strategy: r
}), {
x: c,
y: s
} = q(h, a, u), f = a, i = {}, l = 0;
for (let g = 0; g < m.length; g++) {
const {
name: x,
fn: A
} = m[g], {
x: v,
y: p,
data: y,
reset: w
} = await A({
x: c,
y: s,
initialPlacement: a,
placement: f,
strategy: r,
middlewareData: i,
rects: h,
platform: o,
elements: {
reference: t,
floating: e
}
});
c = v ?? c, s = p ?? s, i = {
...i,
[x]: {
...i[x],
...y
}
}, w && l <= 50 && (l++, typeof w == "object" && (w.placement && (f = w.placement), w.rects && (h = w.rects === !0 ? await o.getElementRects({
reference: t,
floating: e,
strategy: r
}) : w.rects), {
x: c,
y: s
} = q(h, f, u)), g = -1);
}
return {
x: c,
y: s,
placement: f,
strategy: r,
middlewareData: i
};
};
async function z(t, e) {
var d;
e === void 0 && (e = {});
const {
x: a,
y: r,
platform: n,
rects: o,
elements: m,
strategy: u
} = t, {
boundary: h = "clippingAncestors",
rootBoundary: c = "viewport",
elementContext: s = "floating",
altBoundary: f = !1,
padding: i = 0
} = T(e, t), l = K(i), x = m[f ? s === "floating" ? "reference" : "floating" : s], A = _(await n.getClippingRect({
element: (d = await (n.isElement == null ? void 0 : n.isElement(x))) == null || d ? x : x.contextElement || await (n.getDocumentElement == null ? void 0 : n.getDocumentElement(m.floating)),
boundary: h,
rootBoundary: c,
strategy: u
})), v = s === "floating" ? {
x: a,
y: r,
width: o.floating.width,
height: o.floating.height
} : o.reference, p = await (n.getOffsetParent == null ? void 0 : n.getOffsetParent(m.floating)), y = await (n.isElement == null ? void 0 : n.isElement(p)) ? await (n.getScale == null ? void 0 : n.getScale(p)) || {
x: 1,
y: 1
} : {
x: 1,
y: 1
}, w = _(n.convertOffsetParentRelativeRectToViewportRelativeRect ? await n.convertOffsetParentRelativeRectToViewportRelativeRect({
elements: m,
rect: v,
offsetParent: p,
strategy: u
}) : v);
return {
top: (A.top - w.top + l.top) / y.y,
bottom: (w.bottom - A.bottom + l.bottom) / y.y,
left: (A.left - w.left + l.left) / y.x,
right: (w.right - A.right + l.right) / y.x
};
}
const le = (t) => ({
name: "arrow",
options: t,
async fn(e) {
const {
x: d,
y: a,
placement: r,
rects: n,
platform: o,
elements: m,
middlewareData: u
} = e, {
element: h,
padding: c = 0
} = T(t, e) || {};
if (h == null)
return {};
const s = K(c), f = {
x: d,
y: a
}, i = Q(r), l = U(i), g = await o.getDimensions(h), x = i === "y", A = x ? "top" : "left", v = x ? "bottom" : "right", p = x ? "clientHeight" : "clientWidth", y = n.reference[l] + n.reference[i] - f[i] - n.floating[l], w = f[i] - n.reference[i], O = await (o.getOffsetParent == null ? void 0 : o.getOffsetParent(h));
let P = O ? O[p] : 0;
(!P || !await (o.isElement == null ? void 0 : o.isElement(O))) && (P = m.floating[p] || n.floating[l]);
const M = y / 2 - w / 2, R = P / 2 - g[l] / 2 - 1, b = $(s[A], R), L = $(s[v], R), S = b, H = P - g[l] - L, k = P / 2 - g[l] / 2 + M, C = X(S, k, H), j = !u.arrow && I(r) != null && k !== C && n.reference[l] / 2 - (k < S ? b : L) - g[l] / 2 < 0, E = j ? k < S ? k - S : k - H : 0;
return {
[i]: f[i] + E,
data: {
[i]: C,
centerOffset: k - C - E,
...j && {
alignmentOffset: E
}
},
reset: j
};
}
}), re = function(t) {
return t === void 0 && (t = {}), {
name: "flip",
options: t,
async fn(e) {
var d, a;
const {
placement: r,
middlewareData: n,
rects: o,
initialPlacement: m,
platform: u,
elements: h
} = e, {
mainAxis: c = !0,
crossAxis: s = !0,
fallbackPlacements: f,
fallbackStrategy: i = "bestFit",
fallbackAxisSideDirection: l = "none",
flipAlignment: g = !0,
...x
} = T(t, e);
if ((d = n.arrow) != null && d.alignmentOffset)
return {};
const A = W(r), v = B(m), p = W(m) === m, y = await (u.isRTL == null ? void 0 : u.isRTL(h.floating)), w = f || (p || !g ? [ee(m)] : te(m)), O = l !== "none";
!f && O && w.push(...ne(m, g, l, y));
const P = [m, ...w], M = await z(e, x), R = [];
let b = ((a = n.flip) == null ? void 0 : a.overflows) || [];
if (c && R.push(M[A]), s) {
const C = ie(r, o, y);
R.push(M[C[0]], M[C[1]]);
}
if (b = [...b, {
placement: r,
overflows: R
}], !R.every((C) => C <= 0)) {
var L, S;
const C = (((L = n.flip) == null ? void 0 : L.index) || 0) + 1, j = P[C];
if (j) {
var H;
const F = s === "alignment" ? v !== B(j) : !1, D = ((H = b[0]) == null ? void 0 : H.overflows[0]) > 0;
if (!F || D)
return {
data: {
index: C,
overflows: b
},
reset: {
placement: j
}
};
}
let E = (S = b.filter((F) => F.overflows[0] <= 0).sort((F, D) => F.overflows[1] - D.overflows[1])[0]) == null ? void 0 : S.placement;
if (!E)
switch (i) {
case "bestFit": {
var k;
const F = (k = b.filter((D) => {
if (O) {
const V = B(D.placement);
return V === v || // Create a bias to the `y` side axis due to horizontal
// reading directions favoring greater width.
V === "y";
}
return !0;
}).map((D) => [D.placement, D.overflows.filter((V) => V > 0).reduce((V, Z) => V + Z, 0)]).sort((D, V) => D[1] - V[1])[0]) == null ? void 0 : k[0];
F && (E = F);
break;
}
case "initialPlacement":
E = m;
break;
}
if (r !== E)
return {
reset: {
placement: E
}
};
}
return {};
}
};
};
function G(t, e) {
return {
top: t.top - e.height,
right: t.right - e.width,
bottom: t.bottom - e.height,
left: t.left - e.width
};
}
function J(t) {
return se.some((e) => t[e] >= 0);
}
const fe = function(t) {
return t === void 0 && (t = {}), {
name: "hide",
options: t,
async fn(e) {
const {
rects: d
} = e, {
strategy: a = "referenceHidden",
...r
} = T(t, e);
switch (a) {
case "referenceHidden": {
const n = await z(e, {
...r,
elementContext: "reference"
}), o = G(n, d.reference);
return {
data: {
referenceHiddenOffsets: o,
referenceHidden: J(o)
}
};
}
case "escaped": {
const n = await z(e, {
...r,
altBoundary: !0
}), o = G(n, d.floating);
return {
data: {
escapedOffsets: o,
escaped: J(o)
}
};
}
default:
return {};
}
}
};
};
async function oe(t, e) {
const {
placement: d,
platform: a,
elements: r
} = t, n = await (a.isRTL == null ? void 0 : a.isRTL(r.floating)), o = W(d), m = I(d), u = B(d) === "y", h = ["left", "top"].includes(o) ? -1 : 1, c = n && u ? -1 : 1, s = T(e, t);
let {
mainAxis: f,
crossAxis: i,
alignmentAxis: l
} = typeof s == "number" ? {
mainAxis: s,
crossAxis: 0,
alignmentAxis: null
} : {
mainAxis: s.mainAxis || 0,
crossAxis: s.crossAxis || 0,
alignmentAxis: s.alignmentAxis
};
return m && typeof l == "number" && (i = m === "end" ? l * -1 : l), u ? {
x: i * c,
y: f * h
} : {
x: f * h,
y: i * c
};
}
const me = function(t) {
return t === void 0 && (t = 0), {
name: "offset",
options: t,
async fn(e) {
var d, a;
const {
x: r,
y: n,
placement: o,
middlewareData: m
} = e, u = await oe(e, t);
return o === ((d = m.offset) == null ? void 0 : d.placement) && (a = m.arrow) != null && a.alignmentOffset ? {} : {
x: r + u.x,
y: n + u.y,
data: {
...u,
placement: o
}
};
}
};
}, de = function(t) {
return t === void 0 && (t = {}), {
name: "shift",
options: t,
async fn(e) {
const {
x: d,
y: a,
placement: r
} = e, {
mainAxis: n = !0,
crossAxis: o = !1,
limiter: m = {
fn: (x) => {
let {
x: A,
y: v
} = x;
return {
x: A,
y: v
};
}
},
...u
} = T(t, e), h = {
x: d,
y: a
}, c = await z(e, u), s = B(W(r)), f = N(s);
let i = h[f], l = h[s];
if (n) {
const x = f === "y" ? "top" : "left", A = f === "y" ? "bottom" : "right", v = i + c[x], p = i - c[A];
i = X(v, i, p);
}
if (o) {
const x = s === "y" ? "top" : "left", A = s === "y" ? "bottom" : "right", v = l + c[x], p = l - c[A];
l = X(v, l, p);
}
const g = m.fn({
...e,
[f]: i,
[s]: l
});
return {
...g,
data: {
x: g.x - d,
y: g.y - a,
enabled: {
[f]: n,
[s]: o
}
}
};
}
};
}, xe = function(t) {
return t === void 0 && (t = {}), {
options: t,
fn(e) {
const {
x: d,
y: a,
placement: r,
rects: n,
middlewareData: o
} = e, {
offset: m = 0,
mainAxis: u = !0,
crossAxis: h = !0
} = T(t, e), c = {
x: d,
y: a
}, s = B(r), f = N(s);
let i = c[f], l = c[s];
const g = T(m, e), x = typeof g == "number" ? {
mainAxis: g,
crossAxis: 0
} : {
mainAxis: 0,
crossAxis: 0,
...g
};
if (u) {
const p = f === "y" ? "height" : "width", y = n.reference[f] - n.floating[p] + x.mainAxis, w = n.reference[f] + n.reference[p] - x.mainAxis;
i < y ? i = y : i > w && (i = w);
}
if (h) {
var A, v;
const p = f === "y" ? "width" : "height", y = ["top", "left"].includes(W(r)), w = n.reference[s] - n.floating[p] + (y && ((A = o.offset) == null ? void 0 : A[s]) || 0) + (y ? 0 : x.crossAxis), O = n.reference[s] + n.reference[p] + (y ? 0 : ((v = o.offset) == null ? void 0 : v[s]) || 0) - (y ? x.crossAxis : 0);
l < w ? l = w : l > O && (l = O);
}
return {
[f]: i,
[s]: l
};
}
};
}, ge = function(t) {
return t === void 0 && (t = {}), {
name: "size",
options: t,
async fn(e) {
var d, a;
const {
placement: r,
rects: n,
platform: o,
elements: m
} = e, {
apply: u = () => {
},
...h
} = T(t, e), c = await z(e, h), s = W(r), f = I(r), i = B(r) === "y", {
width: l,
height: g
} = n.floating;
let x, A;
s === "top" || s === "bottom" ? (x = s, A = f === (await (o.isRTL == null ? void 0 : o.isRTL(m.floating)) ? "start" : "end") ? "left" : "right") : (A = s, x = f === "end" ? "top" : "bottom");
const v = g - c.top - c.bottom, p = l - c.left - c.right, y = $(g - c[x], v), w = $(l - c[A], p), O = !e.middlewareData.shift;
let P = y, M = w;
if ((d = e.middlewareData.shift) != null && d.enabled.x && (M = p), (a = e.middlewareData.shift) != null && a.enabled.y && (P = v), O && !f) {
const b = Y(c.left, 0), L = Y(c.right, 0), S = Y(c.top, 0), H = Y(c.bottom, 0);
i ? M = l - 2 * (b !== 0 || L !== 0 ? b + L : Y(c.left, c.right)) : P = g - 2 * (S !== 0 || H !== 0 ? S + H : Y(c.top, c.bottom));
}
await u({
...e,
availableWidth: M,
availableHeight: P
});
const R = await o.getDimensions(m.floating);
return l !== R.width || g !== R.height ? {
reset: {
rects: !0
}
} : {};
}
};
};
export {
le as arrow,
ce as computePosition,
z as detectOverflow,
re as flip,
fe as hide,
xe as limitShift,
me as offset,
_ as rectToClientRect,
de as shift,
ge as size
};
//# sourceMappingURL=index99.js.map