UNPKG

@stihl-design-system/components

Version:

Welcome to the STIHL Design System react component library.

117 lines (116 loc) 3.81 kB
"use client"; import { jsx as B, jsxs as G } from "react/jsx-runtime"; import { d as N } from "./chunks/index.CgTHIF3K.js"; import { createContext as V, useContext as W, useState as I, useRef as $, useEffect as M } from "react"; import { g as S } from "./chunks/helpers.BbWkXTr3.js"; import { b as P, g as y } from "./chunks/AriaLiveRegions.utils.Cb327d_2.js"; import { D as z, s as F } from "./chunks/Toast.DG3WVvau.js"; import { h as H } from "./chunks/has-window.ut_-aShB.js"; const J = () => { if (!P() || !y()) throw new Error(S("DSToast", 'The DSAriaLiveRegions component has to be placed at the root of your application, as close to the "body" as possible, e.g. in your "App.tsx" or "layout.tsx".')); }, K = () => { if (!H) return; if (document.querySelectorAll("[data-ds-toast]").length > 1) throw new Error(S("DSToast", 'The destructured "toast" element should only be used once in the DOM.')); }, h = "data-ds-toast-timeout-id", R = "closing", X = 1e4, _ = V(void 0), ae = () => { const n = W(_); if (!n) throw new Error(S("DSToastProvider", "useDSToast must be used within a <DSToastProvider />, which wraps around your root component.")); return n; }, ce = (n) => { const e = N.c(16), { children: i } = n; let u; e[0] === Symbol.for("react.memo_cache_sentinel") ? (u = [], e[0] = u) : u = e[0]; const [a, A] = I(u), [o, p] = I(null), s = $(null); let l; e[1] !== o || e[2] !== a ? (l = () => { !o && a.length > 0 && setTimeout(() => { p(a[0]); }); }, e[1] = o, e[2] = a, e[3] = l) : l = e[3]; let m; e[4] !== a ? (m = [a], e[4] = a, e[5] = m) : m = e[5], M(l, m); let T; e[6] !== o ? (T = [o], e[6] = o, e[7] = T) : T = e[7], M(() => { o && (O(o.message, o.variant), q()); }, T); const w = () => { var t; return ((t = s.current) == null ? void 0 : t.getAttribute(h)) || null; }, D = (t) => { var r; (r = s.current) == null || r.setAttribute(h, t); }, C = () => { var t; return (t = s.current) == null ? void 0 : t.removeAttribute(h); }, O = Y, v = (t, r) => { J(); const Q = { message: t, hideIcon: r == null ? void 0 : r.hideIcon, variant: (r == null ? void 0 : r.variant) || "info", ...r, onClick: () => { L(); }, onMouseEnter: U, onMouseLeave: j }; A((k) => [...k, Q]); }, U = () => { E(); }, E = () => { var r; const t = w(); t && (clearTimeout(t), (r = s.current) == null || r.removeAttribute(h)); }, x = () => { const t = setTimeout(() => { L(); }, X); D(String(t)); }, j = () => { w() !== R && s.current && x(); }, q = () => { var t; K(), o && ((t = s.current) != null && t.showPopover) && (s.current.showPopover(), x()); }, L = () => { E(), D(R), s.current && (s.current.addEventListener("animationend", () => { var t; (t = s.current) != null && t.hidePopover && (s.current.hidePopover(), C(), s.current = null, A(Z), p(null)); }, { once: !0 }), s.current.classList.add(F.rootClosing)); }; let b, d; e[8] !== v ? (d = { queueDSToastMessage: v }, e[8] = v, e[9] = d) : d = e[9], b = d; const g = b; let c; e[10] !== o ? (c = o ? /* @__PURE__ */ B(z, { ...o, ref: s }) : null, e[10] = o, e[11] = c) : c = e[11]; let f; return e[12] !== i || e[13] !== g || e[14] !== c ? (f = /* @__PURE__ */ G(_.Provider, { value: g, children: [ c, i ] }), e[12] = i, e[13] = g, e[14] = c, e[15] = f) : f = e[15], f; }; function Y(n, e) { if (e === "warning") { const i = P(); i && (i.innerText = n); } else if (e === "success" || e === "info") { const i = y(); i && (i.innerText = n); } } function Z(n) { return n.slice(1); } export { ce as DSToastProvider, ae as useDSToast };