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