UNPKG

@stihl-design-system/components

Version:

Welcome to the STIHL Design System react component library.

951 lines (950 loc) 23.7 kB
"use client"; const U = Math.min, F = Math.max, K = Math.round, X = Math.floor, C = (t) => ({ x: t, y: t }), Pt = { left: "right", right: "left", bottom: "top", top: "bottom" }, Bt = { start: "end", end: "start" }; function gt(t, e, n) { return F(t, U(e, n)); } function Q(t, e) { return typeof t == "function" ? t(e) : t; } function P(t) { return t.split("-")[0]; } function Z(t) { return t.split("-")[1]; } function vt(t) { return t === "x" ? "y" : "x"; } function bt(t) { return t === "y" ? "height" : "width"; } function W(t) { return ["top", "bottom"].includes(P(t)) ? "y" : "x"; } function At(t) { return vt(W(t)); } function Mt(t, e, n) { n === void 0 && (n = !1); const o = Z(t), i = At(t), r = bt(i); let s = i === "x" ? o === (n ? "end" : "start") ? "right" : "left" : o === "start" ? "bottom" : "top"; return e.reference[r] > e.floating[r] && (s = G(s)), [s, G(s)]; } function Nt(t) { const e = G(t); return [st(t), e, st(e)]; } function st(t) { return t.replace(/start|end/g, (e) => Bt[e]); } function Vt(t, e, n) { const o = ["left", "right"], i = ["right", "left"], r = ["top", "bottom"], s = ["bottom", "top"]; switch (t) { case "top": case "bottom": return n ? e ? i : o : e ? o : i; case "left": case "right": return e ? r : s; default: return []; } } function Wt(t, e, n, o) { const i = Z(t); let r = Vt(P(t), n === "start", o); return i && (r = r.map((s) => s + "-" + i), e && (r = r.concat(r.map(st)))), r; } function G(t) { return t.replace(/left|right|bottom|top/g, (e) => Pt[e]); } function Ht(t) { return { top: 0, right: 0, bottom: 0, left: 0, ...t }; } function $t(t) { return typeof t != "number" ? Ht(t) : { top: t, right: t, bottom: t, left: t }; } function J(t) { const { x: e, y: n, width: o, height: i } = t; return { width: o, height: i, top: n, left: e, right: e + o, bottom: n + i, x: e, y: n }; } function pt(t, e, n) { let { reference: o, floating: i } = t; const r = W(e), s = At(e), c = bt(s), l = P(e), f = r === "y", d = o.x + o.width / 2 - i.width / 2, a = o.y + o.height / 2 - i.height / 2, m = o[c] / 2 - i[c] / 2; let u; switch (l) { case "top": u = { x: d, y: o.y - i.height }; break; case "bottom": u = { x: d, y: o.y + o.height }; break; case "right": u = { x: o.x + o.width, y: a }; break; case "left": u = { x: o.x - i.width, y: a }; break; default: u = { x: o.x, y: o.y }; } switch (Z(e)) { case "start": u[s] -= m * (n && f ? -1 : 1); break; case "end": u[s] += m * (n && f ? -1 : 1); break; } return u; } const _t = async (t, e, n) => { const { placement: o = "bottom", strategy: i = "absolute", middleware: r = [], platform: s } = n, c = r.filter(Boolean), l = await (s.isRTL == null ? void 0 : s.isRTL(e)); let f = await s.getElementRects({ reference: t, floating: e, strategy: i }), { x: d, y: a } = pt(f, o, l), m = o, u = {}, h = 0; for (let p = 0; p < c.length; p++) { const { name: w, fn: g } = c[p], { x, y, data: b, reset: v } = await g({ x: d, y: a, initialPlacement: o, placement: m, strategy: i, middlewareData: u, rects: f, platform: s, elements: { reference: t, floating: e } }); d = x ?? d, a = y ?? a, u = { ...u, [w]: { ...u[w], ...b } }, v && h <= 50 && (h++, typeof v == "object" && (v.placement && (m = v.placement), v.rects && (f = v.rects === !0 ? await s.getElementRects({ reference: t, floating: e, strategy: i }) : v.rects), { x: d, y: a } = pt(f, m, l)), p = -1); } return { x: d, y: a, placement: m, strategy: i, middlewareData: u }; }; async function Rt(t, e) { var n; e === void 0 && (e = {}); const { x: o, y: i, platform: r, rects: s, elements: c, strategy: l } = t, { boundary: f = "clippingAncestors", rootBoundary: d = "viewport", elementContext: a = "floating", altBoundary: m = !1, padding: u = 0 } = Q(e, t), h = $t(u), w = c[m ? a === "floating" ? "reference" : "floating" : a], g = J(await r.getClippingRect({ element: (n = await (r.isElement == null ? void 0 : r.isElement(w))) == null || n ? w : w.contextElement || await (r.getDocumentElement == null ? void 0 : r.getDocumentElement(c.floating)), boundary: f, rootBoundary: d, strategy: l })), x = a === "floating" ? { x: o, y: i, width: s.floating.width, height: s.floating.height } : s.reference, y = await (r.getOffsetParent == null ? void 0 : r.getOffsetParent(c.floating)), b = await (r.isElement == null ? void 0 : r.isElement(y)) ? await (r.getScale == null ? void 0 : r.getScale(y)) || { x: 1, y: 1 } : { x: 1, y: 1 }, v = J(r.convertOffsetParentRelativeRectToViewportRelativeRect ? await r.convertOffsetParentRelativeRectToViewportRelativeRect({ elements: c, rect: x, offsetParent: y, strategy: l }) : x); return { top: (g.top - v.top + h.top) / b.y, bottom: (v.bottom - g.bottom + h.bottom) / b.y, left: (g.left - v.left + h.left) / b.x, right: (v.right - g.right + h.right) / b.x }; } const zt = function(t) { return t === void 0 && (t = {}), { name: "flip", options: t, async fn(e) { var n, o; const { placement: i, middlewareData: r, rects: s, initialPlacement: c, platform: l, elements: f } = e, { mainAxis: d = !0, crossAxis: a = !0, fallbackPlacements: m, fallbackStrategy: u = "bestFit", fallbackAxisSideDirection: h = "none", flipAlignment: p = !0, ...w } = Q(t, e); if ((n = r.arrow) != null && n.alignmentOffset) return {}; const g = P(i), x = W(c), y = P(c) === c, b = await (l.isRTL == null ? void 0 : l.isRTL(f.floating)), v = m || (y || !p ? [G(c)] : Nt(c)), _ = h !== "none"; !m && _ && v.push(...Wt(c, p, h, b)); const M = [c, ...v], ot = await Rt(e, w), q = []; let z = ((o = r.flip) == null ? void 0 : o.overflows) || []; if (d && q.push(ot[g]), a) { const k = Mt(i, s, b); q.push(ot[k[0]], ot[k[1]]); } if (z = [...z, { placement: i, overflows: q }], !q.every((k) => k <= 0)) { var ut, dt; const k = (((ut = r.flip) == null ? void 0 : ut.index) || 0) + 1, ht = M[k]; if (ht) return { data: { index: k, overflows: z }, reset: { placement: ht } }; let I = (dt = z.filter((N) => N.overflows[0] <= 0).sort((N, L) => N.overflows[1] - L.overflows[1])[0]) == null ? void 0 : dt.placement; if (!I) switch (u) { case "bestFit": { var mt; const N = (mt = z.filter((L) => { if (_) { const S = W(L.placement); return S === x || // Create a bias to the `y` side axis due to horizontal // reading directions favoring greater width. S === "y"; } return !0; }).map((L) => [L.placement, L.overflows.filter((S) => S > 0).reduce((S, Ft) => S + Ft, 0)]).sort((L, S) => L[1] - S[1])[0]) == null ? void 0 : mt[0]; N && (I = N); break; } case "initialPlacement": I = c; break; } if (i !== I) return { reset: { placement: I } }; } return {}; } }; }; async function It(t, e) { const { placement: n, platform: o, elements: i } = t, r = await (o.isRTL == null ? void 0 : o.isRTL(i.floating)), s = P(n), c = Z(n), l = W(n) === "y", f = ["left", "top"].includes(s) ? -1 : 1, d = r && l ? -1 : 1, a = Q(e, t); let { mainAxis: m, crossAxis: u, alignmentAxis: h } = typeof a == "number" ? { mainAxis: a, crossAxis: 0, alignmentAxis: null } : { mainAxis: a.mainAxis || 0, crossAxis: a.crossAxis || 0, alignmentAxis: a.alignmentAxis }; return c && typeof h == "number" && (u = c === "end" ? h * -1 : h), l ? { x: u * d, y: m * f } : { x: m * f, y: u * d }; } const jt = function(t) { return t === void 0 && (t = 0), { name: "offset", options: t, async fn(e) { var n, o; const { x: i, y: r, placement: s, middlewareData: c } = e, l = await It(e, t); return s === ((n = c.offset) == null ? void 0 : n.placement) && (o = c.arrow) != null && o.alignmentOffset ? {} : { x: i + l.x, y: r + l.y, data: { ...l, placement: s } }; } }; }, Yt = function(t) { return t === void 0 && (t = {}), { name: "shift", options: t, async fn(e) { const { x: n, y: o, placement: i } = e, { mainAxis: r = !0, crossAxis: s = !1, limiter: c = { fn: (w) => { let { x: g, y: x } = w; return { x: g, y: x }; } }, ...l } = Q(t, e), f = { x: n, y: o }, d = await Rt(e, l), a = W(P(i)), m = vt(a); let u = f[m], h = f[a]; if (r) { const w = m === "y" ? "top" : "left", g = m === "y" ? "bottom" : "right", x = u + d[w], y = u - d[g]; u = gt(x, u, y); } if (s) { const w = a === "y" ? "top" : "left", g = a === "y" ? "bottom" : "right", x = h + d[w], y = h - d[g]; h = gt(x, h, y); } const p = c.fn({ ...e, [m]: u, [a]: h }); return { ...p, data: { x: p.x - n, y: p.y - o, enabled: { [m]: r, [a]: s } } }; } }; }; function tt() { return typeof window < "u"; } function $(t) { return Ot(t) ? (t.nodeName || "").toLowerCase() : "#document"; } function A(t) { var e; return (t == null || (e = t.ownerDocument) == null ? void 0 : e.defaultView) || window; } function T(t) { var e; return (e = (Ot(t) ? t.ownerDocument : t.document) || window.document) == null ? void 0 : e.documentElement; } function Ot(t) { return tt() ? t instanceof Node || t instanceof A(t).Node : !1; } function R(t) { return tt() ? t instanceof Element || t instanceof A(t).Element : !1; } function E(t) { return tt() ? t instanceof HTMLElement || t instanceof A(t).HTMLElement : !1; } function wt(t) { return !tt() || typeof ShadowRoot > "u" ? !1 : t instanceof ShadowRoot || t instanceof A(t).ShadowRoot; } function Y(t) { const { overflow: e, overflowX: n, overflowY: o, display: i } = O(t); return /auto|scroll|overlay|hidden|clip/.test(e + o + n) && !["inline", "contents"].includes(i); } function qt(t) { return ["table", "td", "th"].includes($(t)); } function et(t) { return [":popover-open", ":modal"].some((e) => { try { return t.matches(e); } catch { return !1; } }); } function ct(t) { const e = lt(), n = R(t) ? O(t) : t; return ["transform", "translate", "scale", "rotate", "perspective"].some((o) => n[o] ? n[o] !== "none" : !1) || (n.containerType ? n.containerType !== "normal" : !1) || !e && (n.backdropFilter ? n.backdropFilter !== "none" : !1) || !e && (n.filter ? n.filter !== "none" : !1) || ["transform", "translate", "scale", "rotate", "perspective", "filter"].some((o) => (n.willChange || "").includes(o)) || ["paint", "layout", "strict", "content"].some((o) => (n.contain || "").includes(o)); } function Xt(t) { let e = D(t); for (; E(e) && !H(e); ) { if (ct(e)) return e; if (et(e)) return null; e = D(e); } return null; } function lt() { return typeof CSS > "u" || !CSS.supports ? !1 : CSS.supports("-webkit-backdrop-filter", "none"); } function H(t) { return ["html", "body", "#document"].includes($(t)); } function O(t) { return A(t).getComputedStyle(t); } function nt(t) { return R(t) ? { scrollLeft: t.scrollLeft, scrollTop: t.scrollTop } : { scrollLeft: t.scrollX, scrollTop: t.scrollY }; } function D(t) { if ($(t) === "html") return t; const e = ( // Step into the shadow DOM of the parent of a slotted node. t.assignedSlot || // DOM Element detected. t.parentNode || // ShadowRoot detected. wt(t) && t.host || // Fallback. T(t) ); return wt(e) ? e.host : e; } function Ct(t) { const e = D(t); return H(e) ? t.ownerDocument ? t.ownerDocument.body : t.body : E(e) && Y(e) ? e : Ct(e); } function j(t, e, n) { var o; e === void 0 && (e = []), n === void 0 && (n = !0); const i = Ct(t), r = i === ((o = t.ownerDocument) == null ? void 0 : o.body), s = A(i); if (r) { const c = rt(s); return e.concat(s, s.visualViewport || [], Y(i) ? i : [], c && n ? j(c) : []); } return e.concat(i, j(i, [], n)); } function rt(t) { return t.parent && Object.getPrototypeOf(t.parent) ? t.frameElement : null; } function Et(t) { const e = O(t); let n = parseFloat(e.width) || 0, o = parseFloat(e.height) || 0; const i = E(t), r = i ? t.offsetWidth : n, s = i ? t.offsetHeight : o, c = K(n) !== r || K(o) !== s; return c && (n = r, o = s), { width: n, height: o, $: c }; } function ft(t) { return R(t) ? t : t.contextElement; } function V(t) { const e = ft(t); if (!E(e)) return C(1); const n = e.getBoundingClientRect(), { width: o, height: i, $: r } = Et(e); let s = (r ? K(n.width) : n.width) / o, c = (r ? K(n.height) : n.height) / i; return (!s || !Number.isFinite(s)) && (s = 1), (!c || !Number.isFinite(c)) && (c = 1), { x: s, y: c }; } const Ut = /* @__PURE__ */ C(0); function Tt(t) { const e = A(t); return !lt() || !e.visualViewport ? Ut : { x: e.visualViewport.offsetLeft, y: e.visualViewport.offsetTop }; } function Kt(t, e, n) { return e === void 0 && (e = !1), !n || e && n !== A(t) ? !1 : e; } function B(t, e, n, o) { e === void 0 && (e = !1), n === void 0 && (n = !1); const i = t.getBoundingClientRect(), r = ft(t); let s = C(1); e && (o ? R(o) && (s = V(o)) : s = V(t)); const c = Kt(r, n, o) ? Tt(r) : C(0); let l = (i.left + c.x) / s.x, f = (i.top + c.y) / s.y, d = i.width / s.x, a = i.height / s.y; if (r) { const m = A(r), u = o && R(o) ? A(o) : o; let h = m, p = rt(h); for (; p && o && u !== h; ) { const w = V(p), g = p.getBoundingClientRect(), x = O(p), y = g.left + (p.clientLeft + parseFloat(x.paddingLeft)) * w.x, b = g.top + (p.clientTop + parseFloat(x.paddingTop)) * w.y; l *= w.x, f *= w.y, d *= w.x, a *= w.y, l += y, f += b, h = A(p), p = rt(h); } } return J({ width: d, height: a, x: l, y: f }); } function at(t, e) { const n = nt(t).scrollLeft; return e ? e.left + n : B(T(t)).left + n; } function Lt(t, e, n) { n === void 0 && (n = !1); const o = t.getBoundingClientRect(), i = o.left + e.scrollLeft - (n ? 0 : ( // RTL <body> scrollbar. at(t, o) )), r = o.top + e.scrollTop; return { x: i, y: r }; } function Gt(t) { let { elements: e, rect: n, offsetParent: o, strategy: i } = t; const r = i === "fixed", s = T(o), c = e ? et(e.floating) : !1; if (o === s || c && r) return n; let l = { scrollLeft: 0, scrollTop: 0 }, f = C(1); const d = C(0), a = E(o); if ((a || !a && !r) && (($(o) !== "body" || Y(s)) && (l = nt(o)), E(o))) { const u = B(o); f = V(o), d.x = u.x + o.clientLeft, d.y = u.y + o.clientTop; } const m = s && !a && !r ? Lt(s, l, !0) : C(0); return { width: n.width * f.x, height: n.height * f.y, x: n.x * f.x - l.scrollLeft * f.x + d.x + m.x, y: n.y * f.y - l.scrollTop * f.y + d.y + m.y }; } function Jt(t) { return Array.from(t.getClientRects()); } function Qt(t) { const e = T(t), n = nt(t), o = t.ownerDocument.body, i = F(e.scrollWidth, e.clientWidth, o.scrollWidth, o.clientWidth), r = F(e.scrollHeight, e.clientHeight, o.scrollHeight, o.clientHeight); let s = -n.scrollLeft + at(t); const c = -n.scrollTop; return O(o).direction === "rtl" && (s += F(e.clientWidth, o.clientWidth) - i), { width: i, height: r, x: s, y: c }; } function Zt(t, e) { const n = A(t), o = T(t), i = n.visualViewport; let r = o.clientWidth, s = o.clientHeight, c = 0, l = 0; if (i) { r = i.width, s = i.height; const f = lt(); (!f || f && e === "fixed") && (c = i.offsetLeft, l = i.offsetTop); } return { width: r, height: s, x: c, y: l }; } function te(t, e) { const n = B(t, !0, e === "fixed"), o = n.top + t.clientTop, i = n.left + t.clientLeft, r = E(t) ? V(t) : C(1), s = t.clientWidth * r.x, c = t.clientHeight * r.y, l = i * r.x, f = o * r.y; return { width: s, height: c, x: l, y: f }; } function xt(t, e, n) { let o; if (e === "viewport") o = Zt(t, n); else if (e === "document") o = Qt(T(t)); else if (R(e)) o = te(e, n); else { const i = Tt(t); o = { x: e.x - i.x, y: e.y - i.y, width: e.width, height: e.height }; } return J(o); } function St(t, e) { const n = D(t); return n === e || !R(n) || H(n) ? !1 : O(n).position === "fixed" || St(n, e); } function ee(t, e) { const n = e.get(t); if (n) return n; let o = j(t, [], !1).filter((c) => R(c) && $(c) !== "body"), i = null; const r = O(t).position === "fixed"; let s = r ? D(t) : t; for (; R(s) && !H(s); ) { const c = O(s), l = ct(s); !l && c.position === "fixed" && (i = null), (r ? !l && !i : !l && c.position === "static" && !!i && ["absolute", "fixed"].includes(i.position) || Y(s) && !l && St(t, s)) ? o = o.filter((d) => d !== s) : i = c, s = D(s); } return e.set(t, o), o; } function ne(t) { let { element: e, boundary: n, rootBoundary: o, strategy: i } = t; const s = [...n === "clippingAncestors" ? et(e) ? [] : ee(e, this._c) : [].concat(n), o], c = s[0], l = s.reduce((f, d) => { const a = xt(e, d, i); return f.top = F(a.top, f.top), f.right = U(a.right, f.right), f.bottom = U(a.bottom, f.bottom), f.left = F(a.left, f.left), f; }, xt(e, c, i)); return { width: l.right - l.left, height: l.bottom - l.top, x: l.left, y: l.top }; } function oe(t) { const { width: e, height: n } = Et(t); return { width: e, height: n }; } function ie(t, e, n) { const o = E(e), i = T(e), r = n === "fixed", s = B(t, !0, r, e); let c = { scrollLeft: 0, scrollTop: 0 }; const l = C(0); if (o || !o && !r) if (($(e) !== "body" || Y(i)) && (c = nt(e)), o) { const m = B(e, !0, r, e); l.x = m.x + e.clientLeft, l.y = m.y + e.clientTop; } else i && (l.x = at(i)); const f = i && !o && !r ? Lt(i, c) : C(0), d = s.left + c.scrollLeft - l.x - f.x, a = s.top + c.scrollTop - l.y - f.y; return { x: d, y: a, width: s.width, height: s.height }; } function it(t) { return O(t).position === "static"; } function yt(t, e) { if (!E(t) || O(t).position === "fixed") return null; if (e) return e(t); let n = t.offsetParent; return T(t) === n && (n = n.ownerDocument.body), n; } function Dt(t, e) { const n = A(t); if (et(t)) return n; if (!E(t)) { let i = D(t); for (; i && !H(i); ) { if (R(i) && !it(i)) return i; i = D(i); } return n; } let o = yt(t, e); for (; o && qt(o) && it(o); ) o = yt(o, e); return o && H(o) && it(o) && !ct(o) ? n : o || Xt(t) || n; } const se = async function(t) { const e = this.getOffsetParent || Dt, n = this.getDimensions, o = await n(t.floating); return { reference: ie(t.reference, await e(t.floating), t.strategy), floating: { x: 0, y: 0, width: o.width, height: o.height } }; }; function re(t) { return O(t).direction === "rtl"; } const ce = { convertOffsetParentRelativeRectToViewportRelativeRect: Gt, getDocumentElement: T, getClippingRect: ne, getOffsetParent: Dt, getElementRects: se, getClientRects: Jt, getDimensions: oe, getScale: V, isElement: R, isRTL: re }; function kt(t, e) { return t.x === e.x && t.y === e.y && t.width === e.width && t.height === e.height; } function le(t, e) { let n = null, o; const i = T(t); function r() { var c; clearTimeout(o), (c = n) == null || c.disconnect(), n = null; } function s(c, l) { c === void 0 && (c = !1), l === void 0 && (l = 1), r(); const f = t.getBoundingClientRect(), { left: d, top: a, width: m, height: u } = f; if (c || e(), !m || !u) return; const h = X(a), p = X(i.clientWidth - (d + m)), w = X(i.clientHeight - (a + u)), g = X(d), y = { rootMargin: -h + "px " + -p + "px " + -w + "px " + -g + "px", threshold: F(0, U(1, l)) || 1 }; let b = !0; function v(_) { const M = _[0].intersectionRatio; if (M !== l) { if (!b) return s(); M ? s(!1, M) : o = setTimeout(() => { s(!1, 1e-7); }, 1e3); } M === 1 && !kt(f, t.getBoundingClientRect()) && s(), b = !1; } try { n = new IntersectionObserver(v, { ...y, // Handle <iframe>s root: i.ownerDocument }); } catch { n = new IntersectionObserver(v, y); } n.observe(t); } return s(!0), r; } function fe(t, e, n, o) { o === void 0 && (o = {}); const { ancestorScroll: i = !0, ancestorResize: r = !0, elementResize: s = typeof ResizeObserver == "function", layoutShift: c = typeof IntersectionObserver == "function", animationFrame: l = !1 } = o, f = ft(t), d = i || r ? [...f ? j(f) : [], ...j(e)] : []; d.forEach((g) => { i && g.addEventListener("scroll", n, { passive: !0 }), r && g.addEventListener("resize", n); }); const a = f && c ? le(f, n) : null; let m = -1, u = null; s && (u = new ResizeObserver((g) => { let [x] = g; x && x.target === f && u && (u.unobserve(e), cancelAnimationFrame(m), m = requestAnimationFrame(() => { var y; (y = u) == null || y.observe(e); })), n(); }), f && !l && u.observe(f), u.observe(e)); let h, p = l ? B(t) : null; l && w(); function w() { const g = B(t); p && !kt(p, g) && n(), p = g, h = requestAnimationFrame(w); } return n(), () => { var g; d.forEach((x) => { i && x.removeEventListener("scroll", n), r && x.removeEventListener("resize", n); }), a == null || a(), (g = u) == null || g.disconnect(), u = null, l && cancelAnimationFrame(h); }; } const ae = jt, ue = Yt, de = zt, me = (t, e, n) => { const o = /* @__PURE__ */ new Map(), i = { platform: ce, ...n }, r = { ...i.platform, _c: o }; return _t(t, e, { ...i, platform: r }); }; export { fe as a, E as b, me as c, R as d, H as e, de as f, $ as g, D as h, wt as i, O as j, j as k, lt as l, ae as o, ue as s };