@carton-org/react-neumorphism
Version:
A React component library implementing the neumorphism design style
69 lines (68 loc) • 1.83 kB
JavaScript
import { jsxs as f, jsx as C } from "@emotion/react/jsx-runtime";
import { useContext as O, createContext as E, useState as P, useCallback as l } from "react";
import { createPortal as x } from "react-dom";
import { ToastContainer as _ } from "../components/Toast/ToastContainer.js";
import { TOP_CENTER as y, TOP_LEFT as I, TOP_RIGHT as b, BOTTOM_CENTER as B, BOTTOM_LEFT as R, BOTTOM_RIGHT as c } from "../types/position.js";
const m = E({
createToast: () => console.log("Toast provider not well initialized")
}), L = () => O(m), U = 3e3, M = ({
children: u
}) => {
const [r, a] = P({
[c]: [],
[R]: [],
[B]: [],
[b]: [],
[I]: [],
[y]: []
}), T = l((e, o) => {
a((s) => ({
...s,
[o]: s[o].filter((t) => t.id !== e)
}));
}, []), p = l((e = {
type: "info"
}, o) => {
const t = {
...{
duration: U,
position: c,
size: "md",
isClosable: !1,
bordered: !1
},
...o
}, n = {
...e,
...t,
id: crypto.randomUUID()
};
console.log("@@", n, r), (o == null ? void 0 : o.duration) === 0 || setTimeout(() => {
a((i) => ({
...i,
[t.position]: i[t.position].map((d) => d.id === n.id ? {
...d,
isExiting: !0
} : d)
})), setTimeout(() => {
T(n.id, t.position);
}, 300);
}, t.duration), a((i) => ({
...i,
[t.position]: [...i[t.position], n]
}));
}, [T]);
return /* @__PURE__ */ f(m.Provider, { value: {
createToast: p
}, children: [
u,
Object.keys(r).map((e) => {
const o = e, s = r[o];
return s.length === 0 ? null : x(/* @__PURE__ */ C(_, { toasts: s, removeToast: (t) => T(t, o), position: o }), document.body);
})
] });
};
export {
M as ToastsProvider,
L as useToasts
};