@stihl-design-system/components
Version:
Welcome to the STIHL Design System react component library.
90 lines (89 loc) • 2.98 kB
JavaScript
"use client";
import { jsxs as P, jsx as y } from "react/jsx-runtime";
import { createContext as C, useContext as O, useState as g, useRef as _, useEffect as p, useMemo as U } from "react";
import { g as u } from "./chunks/helpers.CexwVao7.js";
import "./chunks/index.D-sRdssb.js";
import { b as A, g as w } from "./chunks/AriaLiveRegions.utils.CybNIBSa.js";
import { D as j, s as q } from "./chunks/Toast.L5HAuEOn.js";
import { h as Q } from "./chunks/has-window.ut_-aShB.js";
const k = () => {
if (!A() || !w())
throw new Error(u("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".'));
}, B = () => {
if (!Q)
return;
if (document.querySelectorAll("[data-ds-toast]").length > 1)
throw new Error(u("DSToast", 'The destructured "toast" element should only be used once in the DOM.'));
}, i = "data-ds-toast-timeout-id", S = "closing", G = 1e4, D = C(void 0), K = () => {
const r = O(D);
if (!r)
throw new Error(u("DSToastProvider", "useDSToast must be used within a <DSToastProvider />, which wraps around your root component."));
return r;
}, X = ({
children: r
}) => {
const [a, c] = g([]), [o, l] = g(null), e = _(null);
p(() => {
!o && a.length > 0 && setTimeout(() => {
l(a[0]);
});
}, [a]), p(() => {
o && (x(o.message, o.variant), I());
}, [o]);
const m = () => e.current?.getAttribute(i) || null, T = (t) => {
e.current?.setAttribute(i, t);
}, E = () => e.current?.removeAttribute(i), x = (t, s) => {
if (s === "warning") {
const n = A();
n && (n.innerText = t);
} else if (s === "success" || s === "info") {
const n = w();
n && (n.innerText = t);
}
}, d = (t, s) => {
k();
const n = {
message: t,
hideIcon: s?.hideIcon,
variant: s?.variant || "info",
...s,
onClick: () => {
v();
},
onMouseEnter: L,
onMouseLeave: M
};
c((b) => [...b, n]);
}, L = () => {
f();
}, f = () => {
const t = m();
t && (clearTimeout(t), e.current?.removeAttribute(i));
}, h = () => {
const t = setTimeout(() => {
v();
}, G);
T(String(t));
}, M = () => {
m() !== S && e.current && h();
}, I = () => {
B(), o && e.current?.showPopover && (e.current.showPopover(), h());
}, v = () => {
f(), T(S), e.current && (e.current.addEventListener("animationend", () => {
e.current?.hidePopover && (e.current.hidePopover(), E(), e.current = null, c((t) => t.slice(1)), l(null));
}, {
once: !0
// removes event listener after being called once
}), e.current.classList.add(q.rootClosing));
}, R = U(() => ({
queueDSToastMessage: d
}), [d]);
return /* @__PURE__ */ P(D.Provider, { value: R, children: [
o ? /* @__PURE__ */ y(j, { ...o, ref: e }) : null,
r
] });
};
export {
X as DSToastProvider,
K as useDSToast
};