kitzo 2.3.24 → 2.3.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -42,6 +42,7 @@ declare type ToasterProps = {
42
42
  gap?: number | `${number}`;
43
43
  edgeOffset?: number | `${number}`;
44
44
  isDark?: boolean;
45
+ pauseOnHover?: boolean;
45
46
  };
46
47
 
47
48
  declare type ToastFn = {
@@ -1,21 +1,23 @@
1
- import { jsx as i } from "react/jsx-runtime";
2
- import { useState as b, useEffect as k, useRef as x, useCallback as v, useLayoutEffect as E } from "react";
3
- import { subscribe as T } from "./helpers/listenar.js";
1
+ import { jsx as s } from "react/jsx-runtime";
2
+ import { useState as k, useEffect as v, useRef as x, useCallback as T, useLayoutEffect as E } from "react";
3
+ import { subscribe as O } from "./helpers/listenar.js";
4
4
  import z from "./helpers/manageToasts/manageToasts.js";
5
- import O from "./partials/ToastContainer.js";
6
- function w(p) {
5
+ import C from "./partials/ToastContainer.js";
6
+ import { ToasterContext as $ } from "./context/ToasterContext.js";
7
+ function S(m) {
7
8
  const {
8
- position: l = "top-center",
9
- richColors: u = !1,
10
- animateTransformOrigin: m = !0,
11
- gap: o = 8,
12
- edgeOffset: n = 16,
13
- isDark: d = window.matchMedia("(prefers-color-scheme: dark)").matches
14
- } = p, [c, h] = b([]);
15
- k(() => T((e) => z({ toast: e, setToasts: h })), []);
16
- const s = x(null), r = v(() => {
17
- if (!s.current) return;
18
- const t = s.current.querySelectorAll("[data-toast-container]"), e = {
9
+ position: d = "top-center",
10
+ richColors: c = !1,
11
+ animateTransformOrigin: h = !0,
12
+ gap: e = 8,
13
+ edgeOffset: r = 16,
14
+ isDark: f = window.matchMedia("(prefers-color-scheme: dark)").matches,
15
+ pauseOnHover: g = !0
16
+ } = m, [p, l] = k([]);
17
+ v(() => O((o) => z({ toast: o, setToasts: l })), []);
18
+ const a = x(null), i = T(() => {
19
+ if (!a.current) return;
20
+ const t = a.current.querySelectorAll("[data-toast-container]"), o = {
19
21
  "top-left": 0,
20
22
  "top-center": 0,
21
23
  "top-right": 0,
@@ -23,49 +25,56 @@ function w(p) {
23
25
  "bottom-center": 0,
24
26
  "bottom-right": 0
25
27
  };
26
- t.forEach((a) => {
27
- const f = a.getAttribute("data-toast-position") || "top-center", g = parseFloat(a.style.getPropertyValue("--toast-height")) || 0;
28
- a.style.setProperty("--toast-offset-y", `${e[f]}px`);
29
- const y = isNaN(+o) ? 8 : +o;
30
- e[f] += g + y;
28
+ t.forEach((n) => {
29
+ const u = n.getAttribute("data-toast-position") || "top-center", y = parseFloat(n.style.getPropertyValue("--toast-height")) || 0;
30
+ n.style.setProperty("--toast-offset-y", `${o[u]}px`);
31
+ const b = isNaN(+e) ? 8 : +e;
32
+ o[u] += y + b;
31
33
  });
32
- }, [o]);
34
+ }, [e]);
33
35
  return E(() => {
34
- r();
35
- }, [c, r]), /* @__PURE__ */ i(
36
+ i();
37
+ }, [p, i]), /* @__PURE__ */ s(
36
38
  "div",
37
39
  {
38
- ref: s,
40
+ ref: a,
39
41
  style: {
40
42
  position: "fixed",
41
43
  inset: 0,
42
44
  zIndex: 2147483647,
43
45
  pointerEvents: "none",
44
46
  display: "grid",
45
- padding: n != null ? `${Math.max(Math.min(+n, 200), 0)}px` : 16
47
+ padding: r != null ? `${Math.max(Math.min(+r, 200), 0)}px` : 16
46
48
  },
47
- className: `kitzo-toaster ${u ? "kitzo-toaster-rich-colors" : ""} ${d ? "kitzo-toaster-dark" : ""}`,
48
- children: /* @__PURE__ */ i(
49
+ className: `kitzo-toaster ${c ? "kitzo-toaster-rich-colors" : ""} ${f ? "kitzo-toaster-dark" : ""}`,
50
+ children: /* @__PURE__ */ s(
49
51
  "div",
50
52
  {
51
53
  style: {
52
54
  position: "relative"
53
55
  },
54
- children: c.map((t) => /* @__PURE__ */ i(
55
- O,
56
+ children: /* @__PURE__ */ s(
57
+ $.Provider,
56
58
  {
57
- t,
58
- animateTransformOrigin: m,
59
- containerPosition: l,
60
- updateOffsets: r
61
- },
62
- t.id
63
- ))
59
+ value: {
60
+ position: d,
61
+ richColors: c,
62
+ isDark: f,
63
+ gap: e,
64
+ edgeOffset: r,
65
+ animateTransformOrigin: h,
66
+ pauseOnHover: g,
67
+ setToasts: l,
68
+ updateOffsets: i
69
+ },
70
+ children: p.map((t) => /* @__PURE__ */ s(C, { t }, t.id))
71
+ }
72
+ )
64
73
  }
65
74
  )
66
75
  }
67
76
  );
68
77
  }
69
78
  export {
70
- w as default
79
+ S as default
71
80
  };
@@ -0,0 +1,12 @@
1
+ import { createContext as e, useContext as o } from "react";
2
+ const n = e(null);
3
+ function s() {
4
+ const t = o(n);
5
+ if (!t)
6
+ throw new Error("Toast components must be used inside <Toaster />");
7
+ return t;
8
+ }
9
+ export {
10
+ n as ToasterContext,
11
+ s as useToasterContext
12
+ };
@@ -1,39 +1,76 @@
1
- const t = /* @__PURE__ */ new Map(), o = 600, a = 700;
2
- function m(e) {
1
+ const o = /* @__PURE__ */ new Map(), u = 280, s = 500, f = 0;
2
+ function c(e) {
3
3
  const i = Number(e);
4
4
  if (!isFinite(i)) return null;
5
- const n = Math.max(a, i);
5
+ const t = Math.max(f, i);
6
6
  return {
7
- leaving: n,
8
- left: n + o
7
+ totalTime: t,
8
+ leavingDuration: t,
9
+ leftDuration: t + s
9
10
  };
10
11
  }
11
- function d(e) {
12
- const i = t.get(e);
13
- i && (clearTimeout(i.leaving), clearTimeout(i.left), t.delete(e));
12
+ function T(e) {
13
+ const i = o.get(e);
14
+ i && (clearTimeout(i.leavingTimeoutId), clearTimeout(i.leftTimeoutId), o.delete(e));
14
15
  }
15
- function f() {
16
- t.forEach(({ leaving: e, left: i }) => {
16
+ function g() {
17
+ o.forEach(({ leavingTimeoutId: e, leftTimeoutId: i }) => {
17
18
  clearTimeout(e), clearTimeout(i);
18
- }), t.clear();
19
+ }), o.clear();
19
20
  }
20
- function s(e, i) {
21
- const n = m(e.duration);
22
- if (!n) return;
23
- d(e.id);
24
- const c = setTimeout(() => {
21
+ function d(e, i) {
22
+ const t = c(e.duration);
23
+ if (!t) return;
24
+ T(e.id);
25
+ const r = setTimeout(() => {
25
26
  i(
26
- (l) => l.map((r) => r.id === e.id ? { ...r, status: "leaving" } : r)
27
+ (a) => a.map((n) => n.id === e.id ? { ...n, status: "leaving" } : n)
27
28
  );
28
- }, n.leaving), u = setTimeout(() => {
29
- i((l) => l.filter((r) => r.id !== e.id)), t.delete(e.id);
30
- }, n.left);
31
- t.set(e.id, { leaving: c, left: u });
29
+ }, t.leavingDuration + u), m = setTimeout(() => {
30
+ i((a) => a.filter((n) => n.id !== e.id)), T(e.id), o.delete(e.id);
31
+ }, t.leftDuration + u), l = Date.now();
32
+ o.set(e.id, {
33
+ leavingTimeoutId: r,
34
+ leftTimeoutId: m,
35
+ startingTime: l,
36
+ totalTime: t.totalTime,
37
+ remainningTime: 0
38
+ });
39
+ }
40
+ function I(e) {
41
+ const i = o.get(e);
42
+ if (!i || i.remainningTime > 0) return;
43
+ const r = Date.now() - i.startingTime, m = Math.max(i.totalTime - r, 0);
44
+ m !== 0 && (o.set(e, { ...i, remainningTime: m }), clearTimeout(i.leavingTimeoutId), clearTimeout(i.leftTimeoutId));
45
+ }
46
+ function D(e, i) {
47
+ const t = o.get(e);
48
+ if (!t) return;
49
+ const r = c(t.remainningTime);
50
+ if (!r) return;
51
+ const m = setTimeout(() => {
52
+ i(
53
+ (a) => a.map((n) => n.id === e ? { ...n, status: "leaving" } : n)
54
+ );
55
+ }, r.leavingDuration + u), l = setTimeout(() => {
56
+ i((a) => a.filter((n) => n.id !== e)), T(e), o.delete(e);
57
+ }, r.leftDuration + u);
58
+ o.set(e, {
59
+ ...t,
60
+ startingTime: Date.now(),
61
+ totalTime: t.remainningTime,
62
+ remainningTime: 0,
63
+ leavingTimeoutId: m,
64
+ leftTimeoutId: l
65
+ });
32
66
  }
33
67
  export {
34
- o as LEAVE_DELAY,
35
- a as MIN_VISIBLE,
36
- s as addTimers,
37
- f as clearAllTimers,
38
- d as clearTimer
68
+ s as LEAVE_DELAY,
69
+ f as MIN_VISIBLE,
70
+ u as TRANSITION_DURATION,
71
+ d as addTimers,
72
+ g as clearAllTimers,
73
+ T as clearTimer,
74
+ I as pauseToast,
75
+ D as resumeToast
39
76
  };
@@ -1,48 +1,53 @@
1
- import { jsxs as m, jsx as n, Fragment as e } from "react/jsx-runtime";
2
- import p from "../helpers/triggerToasts.js";
3
- import { InfoSvg as u, ErrorSvg as v, WarningSvg as h, SuccessSvg as y, LoadingSvg as x } from "./Svgs.js";
4
- function I({
5
- t: l,
6
- position: s,
7
- shouldAnimateTransformOrigin: a
1
+ import { jsxs as g, jsx as t, Fragment as s } from "react/jsx-runtime";
2
+ import { resumeToast as v, pauseToast as h } from "../helpers/manageToasts/timers.js";
3
+ import y from "../helpers/triggerToasts.js";
4
+ import { InfoSvg as x, ErrorSvg as S, WarningSvg as $, SuccessSvg as T, LoadingSvg as k } from "./Svgs.js";
5
+ import { useToasterContext as E } from "../context/ToasterContext.js";
6
+ function C({
7
+ t: n,
8
+ position: o,
9
+ shouldAnimateTransformOrigin: d
8
10
  }) {
9
- const { id: c, type: i, action: d, content: r, status: f, showIcon: g, icon: t } = l, o = {
10
- loading: /* @__PURE__ */ n(x, {}),
11
- success: /* @__PURE__ */ n(y, {}),
12
- warning: /* @__PURE__ */ n(h, {}),
13
- error: /* @__PURE__ */ n(v, {}),
14
- info: /* @__PURE__ */ n(u, {}),
11
+ const { setToasts: m, pauseOnHover: a } = E(), { id: l, type: r, action: f, content: i, status: p, showIcon: u, icon: e } = n, c = {
12
+ loading: /* @__PURE__ */ t(k, {}),
13
+ success: /* @__PURE__ */ t(T, {}),
14
+ warning: /* @__PURE__ */ t($, {}),
15
+ error: /* @__PURE__ */ t(S, {}),
16
+ info: /* @__PURE__ */ t(x, {}),
15
17
  default: null,
16
18
  custom: null
17
19
  };
18
- return /* @__PURE__ */ m(
20
+ return /* @__PURE__ */ g(
19
21
  "div",
20
22
  {
23
+ id: `toast-id-${l}`,
21
24
  style: {
22
25
  pointerEvents: "all",
23
26
  display: "flex",
24
27
  alignItems: "center",
25
28
  gap: 8
26
29
  },
27
- className: `kitzo-toast type-${i} action-${d} status-${f} pos-y-${s.split("-")[0]} pos-x-${s.split("-")[1]} ${a ? `transform-origin-${s}` : ""}`,
30
+ onMouseEnter: () => a && h(n.id),
31
+ onMouseLeave: () => a && v(n.id, m),
32
+ className: `kitzo-toast type-${r} action-${f} status-${p} pos-y-${o.split("-")[0]} pos-x-${o.split("-")[1]} ${d ? `transform-origin-${o}` : ""}`,
28
33
  children: [
29
- g && /* @__PURE__ */ n(e, { children: t ? /* @__PURE__ */ n(e, { children: typeof t == "string" || typeof t == "number" ? /* @__PURE__ */ n(
34
+ u && /* @__PURE__ */ t(s, { children: e ? /* @__PURE__ */ t(s, { children: typeof e == "string" || typeof e == "number" ? /* @__PURE__ */ t(
30
35
  "div",
31
36
  {
32
37
  style: {
33
38
  flexShrink: 0
34
39
  },
35
40
  className: "svg-container",
36
- children: t
41
+ children: e
37
42
  }
38
- ) : /* @__PURE__ */ n(e, { children: t }) }) : /* @__PURE__ */ n(e, { children: o[i] && /* @__PURE__ */ n("div", { style: { flexShrink: 0 }, className: "svg-container", children: o[i] }) }) }),
39
- /* @__PURE__ */ n(
43
+ ) : /* @__PURE__ */ t(s, { children: e }) }) : /* @__PURE__ */ t(s, { children: c[r] && /* @__PURE__ */ t("div", { style: { flexShrink: 0 }, className: "svg-container", children: c[r] }) }) }),
44
+ /* @__PURE__ */ t(
40
45
  "div",
41
46
  {
42
47
  style: {
43
48
  flex: 1
44
49
  },
45
- children: typeof r == "function" ? r(() => p.dismiss(c)) : r
50
+ children: typeof i == "function" ? i(() => y.dismiss(l)) : i
46
51
  }
47
52
  )
48
53
  ]
@@ -50,5 +55,5 @@ function I({
50
55
  );
51
56
  }
52
57
  export {
53
- I as default
58
+ C as default
54
59
  };
@@ -1,22 +1,22 @@
1
- import { jsx as c } from "react/jsx-runtime";
1
+ import { jsx as f } from "react/jsx-runtime";
2
2
  import { useRef as d, useLayoutEffect as b } from "react";
3
- import h from "./Toast.js";
4
- function v({
5
- t,
6
- animateTransformOrigin: f,
7
- containerPosition: l,
8
- updateOffsets: r
9
- }) {
10
- const s = d(null);
3
+ import g from "./Toast.js";
4
+ import { useToasterContext as h } from "../context/ToasterContext.js";
5
+ function v({ t }) {
6
+ const {
7
+ updateOffsets: s,
8
+ position: c,
9
+ animateTransformOrigin: l
10
+ } = h(), r = d(null);
11
11
  b(() => {
12
- const e = s.current;
12
+ const e = r.current;
13
13
  if (!e) return;
14
14
  const a = new ResizeObserver(() => {
15
15
  const p = e.offsetHeight;
16
- e.style.setProperty("--toast-height", `${p}px`), r();
16
+ e.style.setProperty("--toast-height", `${p}px`), s();
17
17
  });
18
18
  return a.observe(e), () => a.disconnect();
19
- }, [r]);
19
+ }, [s]);
20
20
  const m = [
21
21
  "top-left",
22
22
  "top-center",
@@ -24,11 +24,11 @@ function v({
24
24
  "bottom-left",
25
25
  "bottom-center",
26
26
  "bottom-right"
27
- ], i = t.position || l, o = m.includes(i) ? i : "top-center", n = o.includes("bottom"), u = typeof t.animateTransformOrigin == "boolean" ? t.animateTransformOrigin : f;
28
- return /* @__PURE__ */ c(
27
+ ], i = t.position || c, o = m.includes(i) ? i : "top-center", n = o?.includes("bottom"), u = typeof t.animateTransformOrigin == "boolean" ? t.animateTransformOrigin : l;
28
+ return /* @__PURE__ */ f(
29
29
  "div",
30
30
  {
31
- ref: s,
31
+ ref: r,
32
32
  "data-toast-container": !0,
33
33
  "data-toast-position": o,
34
34
  style: {
@@ -38,13 +38,13 @@ function v({
38
38
  right: 0,
39
39
  transition: "transform 180ms",
40
40
  display: "flex",
41
- justifyContent: o.includes("left") ? "flex-start" : o.includes("center") ? "center" : "flex-end",
41
+ justifyContent: o?.includes("left") ? "flex-start" : o?.includes("center") ? "center" : "flex-end",
42
42
  top: n ? "auto" : 0,
43
43
  bottom: n ? 0 : "auto",
44
44
  transform: `translateY(calc(var(--toast-offset-y, 0px) * ${n ? -1 : 1}))`
45
45
  },
46
- children: /* @__PURE__ */ c(
47
- h,
46
+ children: /* @__PURE__ */ f(
47
+ g,
48
48
  {
49
49
  t,
50
50
  position: o,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kitzo",
3
- "version": "2.3.24",
3
+ "version": "2.3.25",
4
4
  "description": "A lightweight React micro-utility.",
5
5
  "type": "module",
6
6
  "files": [
@@ -21,6 +21,7 @@
21
21
  },
22
22
  "scripts": {
23
23
  "dev": "vite",
24
+ "dev:host": "vite --host",
24
25
  "build": "tsc -b && vite build",
25
26
  "lint": "eslint .",
26
27
  "preview": "vite preview"