@laravel-inertia-toast/react 0.1.0 → 0.1.1
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.cjs +1 -1
- package/dist/index.js +64 -64
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("react/jsx-runtime"),r=require("react"),w=require("@inertiajs/react");let j=0;function C(){return`toast-${++j}-${Date.now()}`}const y={duration:5e3,position:"top-right",maxVisible:5,propKey:"toasts"};function k(t,i){switch(i.type){case"ADD":{const a=[{...i.payload,id:C()},...t.items].slice(0,t.config.maxVisible);return{...t,items:a}}case"REMOVE":return{...t,items:t.items.filter(s=>s.id!==i.payload)};case"CLEAR":return{...t,items:[]};default:return t}}const g=r.createContext(null);function R({children:t,config:i}){const s={...y,...i},[a,l]=r.useReducer(k,{items:[],config:s}),f=r.useRef(s);f.current=s,r.useEffect(()=>w.router.on("navigate",o=>{const u=o.detail.page.props[f.current.propKey];u&&Array.isArray(u)&&u.forEach(b=>{l({type:"ADD",payload:b})})}),[]);const c=r.useCallback((n,o,u)=>{l({type:"ADD",payload:{message:n,level:o,duration:u??null}})},[]),h={items:a.items,config:a.config,success:r.useCallback((n,o)=>c(n,"success",o),[c]),error:r.useCallback((n,o)=>c(n,"error",o),[c]),info:r.useCallback((n,o)=>c(n,"info",o),[c]),warning:r.useCallback((n,o)=>c(n,"warning",o),[c]),remove:r.useCallback(n=>l({type:"REMOVE",payload:n}),[]),clear:r.useCallback(()=>l({type:"CLEAR"}),[])};return e.jsx(g.Provider,{value:h,children:t})}function p(){const t=r.useContext(g);if(!t)throw new Error("useToast must be used within a <ToastProvider>");return t}const m={success:"bg-green-50 text-green-800 dark:bg-green-900/30 dark:text-green-300",error:"bg-red-50 text-red-800 dark:bg-red-900/30 dark:text-red-300",warning:"bg-yellow-50 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-300",info:"bg-blue-50 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300"},d={success:"text-green-500 dark:text-green-400",error:"text-red-500 dark:text-red-400",warning:"text-yellow-500 dark:text-yellow-400",info:"text-blue-500 dark:text-blue-400"};function N(){return e.jsx("svg",{className:"h-5 w-5",viewBox:"0 0 20 20",fill:"currentColor",children:e.jsx("path",{fillRule:"evenodd",d:"M10 18a8 8 0 100-16 8 8 0 000 16zm3.857-9.809a.75.75 0 00-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 10-1.06 1.061l2.5 2.5a.75.75 0 001.137-.089l4-5.5z",clipRule:"evenodd"})})}function L(){return e.jsx("svg",{className:"h-5 w-5",viewBox:"0 0 20 20",fill:"currentColor",children:e.jsx("path",{fillRule:"evenodd",d:"M10 18a8 8 0 100-16 8 8 0 000 16zM8.28 7.22a.75.75 0 00-1.06 1.06L8.94 10l-1.72 1.72a.75.75 0 101.06 1.06L10 11.06l1.72 1.72a.75.75 0 101.06-1.06L11.06 10l1.72-1.72a.75.75 0 00-1.06-1.06L10 8.94 8.28 7.22z",clipRule:"evenodd"})})}function T(){return e.jsx("svg",{className:"h-5 w-5",viewBox:"0 0 20 20",fill:"currentColor",children:e.jsx("path",{fillRule:"evenodd",d:"M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495zM10 5a.75.75 0 01.75.75v3.5a.75.75 0 01-1.5 0v-3.5A.75.75 0 0110 5zm0 9a1 1 0 100-2 1 1 0 000 2z",clipRule:"evenodd"})})}function z(){return e.jsx("svg",{className:"h-5 w-5",viewBox:"0 0 20 20",fill:"currentColor",children:e.jsx("path",{fillRule:"evenodd",d:"M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a.75.75 0 000 1.5h.253a.25.25 0 01.244.304l-.459 2.066A1.75 1.75 0 0010.747 15H11a.75.75 0 000-1.5h-.253a.25.25 0 01-.244-.304l.459-2.066A1.75 1.75 0 009.253 9H9z",clipRule:"evenodd"})})}function M({level:t}){switch(t){case"success":return e.jsx(N,{});case"error":return e.jsx(L,{});case"warning":return e.jsx(T,{});default:return e.jsx(z,{})}}function v({toast:t,config:i,onRemove:s}){const a=r.useRef(null),l=t.duration??i.duration;return r.useEffect(()=>(l>0&&(a.current=setTimeout(()=>{s(t.id)},l)),()=>{a.current&&clearTimeout(a.current)}),[l,t.id,s]),e.jsx("div",{className:`pointer-events-auto w-full max-w-sm overflow-hidden rounded-lg shadow-lg ring-1 ring-black/5 ${m[t.level]??m.info}`,role:"alert",children:e.jsx("div",{className:"p-4",children:e.jsxs("div",{className:"flex items-start",children:[e.jsx("div",{className:`shrink-0 ${d[t.level]??d.info}`,children:e.jsx(M,{level:t.level})}),e.jsx("div",{className:"ml-3 w-0 flex-1",children:e.jsx("p",{className:"text-sm font-medium",children:t.message})}),e.jsx("div",{className:"ml-4 flex shrink-0",children:e.jsxs("button",{type:"button",className:`inline-flex rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 ${d[t.level]??d.info}`,onClick:()=>s(t.id),children:[e.jsx("span",{className:"sr-only",children:"Close"}),e.jsx("svg",{className:"h-5 w-5",viewBox:"0 0 20 20",fill:"currentColor",children:e.jsx("path",{d:"M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"})})]})})]})})})}const x={"top-right":"top-4 right-4 items-end","top-left":"top-4 left-4 items-start","top-center":"top-4 left-1/2 -translate-x-1/2 items-center","bottom-right":"bottom-4 right-4 items-end","bottom-left":"bottom-4 left-4 items-start","bottom-center":"bottom-4 left-1/2 -translate-x-1/2 items-center"};function E(){const{items:t,config:i,remove:s}=p(),a=x[i.position]??x["top-right"];return e.jsx("div",{className:`pointer-events-none fixed z-50 flex w-full max-w-sm flex-col gap-3 ${a}`,"aria-live":"polite",children:t.map(l=>e.jsx(v,{toast:l,config:i,onRemove:s},l.id))})}exports.ToastItem=v;exports.ToastProvider=R;exports.Toasts=E;exports.useToast=p;
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { jsx as t, jsxs as
|
|
2
|
-
import { createContext as y, useReducer as C, useRef as
|
|
3
|
-
import {
|
|
4
|
-
let
|
|
5
|
-
function
|
|
6
|
-
return `toast-${++
|
|
1
|
+
import { jsx as t, jsxs as m } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as y, useReducer as C, useRef as v, useEffect as h, useCallback as c, useContext as k } from "react";
|
|
3
|
+
import { router as R } from "@inertiajs/react";
|
|
4
|
+
let N = 0;
|
|
5
|
+
function L() {
|
|
6
|
+
return `toast-${++N}-${Date.now()}`;
|
|
7
7
|
}
|
|
8
|
-
const
|
|
8
|
+
const z = {
|
|
9
9
|
duration: 5e3,
|
|
10
10
|
position: "top-right",
|
|
11
11
|
maxVisible: 5,
|
|
12
12
|
propKey: "toasts"
|
|
13
13
|
};
|
|
14
|
-
function
|
|
14
|
+
function M(e, i) {
|
|
15
15
|
switch (i.type) {
|
|
16
16
|
case "ADD": {
|
|
17
17
|
const s = [{
|
|
18
18
|
...i.payload,
|
|
19
|
-
id:
|
|
19
|
+
id: L()
|
|
20
20
|
}, ...e.items].slice(0, e.config.maxVisible);
|
|
21
21
|
return { ...e, items: s };
|
|
22
22
|
}
|
|
23
23
|
case "REMOVE":
|
|
24
24
|
return {
|
|
25
25
|
...e,
|
|
26
|
-
items: e.items.filter((
|
|
26
|
+
items: e.items.filter((r) => r.id !== i.payload)
|
|
27
27
|
};
|
|
28
28
|
case "CLEAR":
|
|
29
29
|
return { ...e, items: [] };
|
|
@@ -31,53 +31,53 @@ function A(e, i) {
|
|
|
31
31
|
return e;
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
const
|
|
35
|
-
function
|
|
36
|
-
const
|
|
34
|
+
const x = y(null);
|
|
35
|
+
function j({ children: e, config: i }) {
|
|
36
|
+
const r = { ...z, ...i }, [s, o] = C(M, {
|
|
37
37
|
items: [],
|
|
38
|
-
config:
|
|
39
|
-
}), f =
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
38
|
+
config: r
|
|
39
|
+
}), f = v(r);
|
|
40
|
+
f.current = r, h(() => R.on("navigate", (l) => {
|
|
41
|
+
const d = l.detail.page.props[f.current.propKey];
|
|
42
|
+
d && Array.isArray(d) && d.forEach((b) => {
|
|
43
|
+
o({ type: "ADD", payload: b });
|
|
44
44
|
});
|
|
45
|
-
}), [
|
|
46
|
-
const a = c((
|
|
47
|
-
|
|
45
|
+
}), []);
|
|
46
|
+
const a = c((n, l, d) => {
|
|
47
|
+
o({
|
|
48
48
|
type: "ADD",
|
|
49
|
-
payload: { message:
|
|
49
|
+
payload: { message: n, level: l, duration: d ?? null }
|
|
50
50
|
});
|
|
51
|
-
}, []),
|
|
51
|
+
}, []), w = {
|
|
52
52
|
items: s.items,
|
|
53
53
|
config: s.config,
|
|
54
|
-
success: c((
|
|
55
|
-
error: c((
|
|
56
|
-
info: c((
|
|
57
|
-
warning: c((
|
|
58
|
-
remove: c((
|
|
59
|
-
clear: c(() =>
|
|
54
|
+
success: c((n, l) => a(n, "success", l), [a]),
|
|
55
|
+
error: c((n, l) => a(n, "error", l), [a]),
|
|
56
|
+
info: c((n, l) => a(n, "info", l), [a]),
|
|
57
|
+
warning: c((n, l) => a(n, "warning", l), [a]),
|
|
58
|
+
remove: c((n) => o({ type: "REMOVE", payload: n }), []),
|
|
59
|
+
clear: c(() => o({ type: "CLEAR" }), [])
|
|
60
60
|
};
|
|
61
|
-
return /* @__PURE__ */ t(
|
|
61
|
+
return /* @__PURE__ */ t(x.Provider, { value: w, children: e });
|
|
62
62
|
}
|
|
63
|
-
function
|
|
64
|
-
const e = k(
|
|
63
|
+
function A() {
|
|
64
|
+
const e = k(x);
|
|
65
65
|
if (!e)
|
|
66
66
|
throw new Error("useToast must be used within a <ToastProvider>");
|
|
67
67
|
return e;
|
|
68
68
|
}
|
|
69
|
-
const
|
|
69
|
+
const p = {
|
|
70
70
|
success: "bg-green-50 text-green-800 dark:bg-green-900/30 dark:text-green-300",
|
|
71
71
|
error: "bg-red-50 text-red-800 dark:bg-red-900/30 dark:text-red-300",
|
|
72
72
|
warning: "bg-yellow-50 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-300",
|
|
73
73
|
info: "bg-blue-50 text-blue-800 dark:bg-blue-900/30 dark:text-blue-300"
|
|
74
|
-
},
|
|
74
|
+
}, u = {
|
|
75
75
|
success: "text-green-500 dark:text-green-400",
|
|
76
76
|
error: "text-red-500 dark:text-red-400",
|
|
77
77
|
warning: "text-yellow-500 dark:text-yellow-400",
|
|
78
78
|
info: "text-blue-500 dark:text-blue-400"
|
|
79
79
|
};
|
|
80
|
-
function
|
|
80
|
+
function E() {
|
|
81
81
|
return /* @__PURE__ */ t("svg", { className: "h-5 w-5", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ t(
|
|
82
82
|
"path",
|
|
83
83
|
{
|
|
@@ -87,7 +87,7 @@ function T() {
|
|
|
87
87
|
}
|
|
88
88
|
) });
|
|
89
89
|
}
|
|
90
|
-
function
|
|
90
|
+
function T() {
|
|
91
91
|
return /* @__PURE__ */ t("svg", { className: "h-5 w-5", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ t(
|
|
92
92
|
"path",
|
|
93
93
|
{
|
|
@@ -97,7 +97,7 @@ function I() {
|
|
|
97
97
|
}
|
|
98
98
|
) });
|
|
99
99
|
}
|
|
100
|
-
function
|
|
100
|
+
function I() {
|
|
101
101
|
return /* @__PURE__ */ t("svg", { className: "h-5 w-5", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ t(
|
|
102
102
|
"path",
|
|
103
103
|
{
|
|
@@ -107,7 +107,7 @@ function D() {
|
|
|
107
107
|
}
|
|
108
108
|
) });
|
|
109
109
|
}
|
|
110
|
-
function
|
|
110
|
+
function D() {
|
|
111
111
|
return /* @__PURE__ */ t("svg", { className: "h-5 w-5", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ t(
|
|
112
112
|
"path",
|
|
113
113
|
{
|
|
@@ -117,38 +117,38 @@ function $() {
|
|
|
117
117
|
}
|
|
118
118
|
) });
|
|
119
119
|
}
|
|
120
|
-
function
|
|
120
|
+
function $({ level: e }) {
|
|
121
121
|
switch (e) {
|
|
122
122
|
case "success":
|
|
123
|
-
return /* @__PURE__ */ t(
|
|
123
|
+
return /* @__PURE__ */ t(E, {});
|
|
124
124
|
case "error":
|
|
125
|
-
return /* @__PURE__ */ t(
|
|
125
|
+
return /* @__PURE__ */ t(T, {});
|
|
126
126
|
case "warning":
|
|
127
|
-
return /* @__PURE__ */ t(
|
|
127
|
+
return /* @__PURE__ */ t(I, {});
|
|
128
128
|
default:
|
|
129
|
-
return /* @__PURE__ */ t(
|
|
129
|
+
return /* @__PURE__ */ t(D, {});
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
|
-
function
|
|
133
|
-
const s =
|
|
134
|
-
return
|
|
135
|
-
|
|
136
|
-
},
|
|
132
|
+
function B({ toast: e, config: i, onRemove: r }) {
|
|
133
|
+
const s = v(null), o = e.duration ?? i.duration;
|
|
134
|
+
return h(() => (o > 0 && (s.current = setTimeout(() => {
|
|
135
|
+
r(e.id);
|
|
136
|
+
}, o)), () => {
|
|
137
137
|
s.current && clearTimeout(s.current);
|
|
138
|
-
}), [
|
|
138
|
+
}), [o, e.id, r]), /* @__PURE__ */ t(
|
|
139
139
|
"div",
|
|
140
140
|
{
|
|
141
|
-
className: `pointer-events-auto w-full max-w-sm overflow-hidden rounded-lg shadow-lg ring-1 ring-black/5 ${
|
|
141
|
+
className: `pointer-events-auto w-full max-w-sm overflow-hidden rounded-lg shadow-lg ring-1 ring-black/5 ${p[e.level] ?? p.info}`,
|
|
142
142
|
role: "alert",
|
|
143
|
-
children: /* @__PURE__ */ t("div", { className: "p-4", children: /* @__PURE__ */
|
|
144
|
-
/* @__PURE__ */ t("div", { className: `shrink-0 ${
|
|
143
|
+
children: /* @__PURE__ */ t("div", { className: "p-4", children: /* @__PURE__ */ m("div", { className: "flex items-start", children: [
|
|
144
|
+
/* @__PURE__ */ t("div", { className: `shrink-0 ${u[e.level] ?? u.info}`, children: /* @__PURE__ */ t($, { level: e.level }) }),
|
|
145
145
|
/* @__PURE__ */ t("div", { className: "ml-3 w-0 flex-1", children: /* @__PURE__ */ t("p", { className: "text-sm font-medium", children: e.message }) }),
|
|
146
|
-
/* @__PURE__ */ t("div", { className: "ml-4 flex shrink-0", children: /* @__PURE__ */
|
|
146
|
+
/* @__PURE__ */ t("div", { className: "ml-4 flex shrink-0", children: /* @__PURE__ */ m(
|
|
147
147
|
"button",
|
|
148
148
|
{
|
|
149
149
|
type: "button",
|
|
150
|
-
className: `inline-flex rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 ${
|
|
151
|
-
onClick: () =>
|
|
150
|
+
className: `inline-flex rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 ${u[e.level] ?? u.info}`,
|
|
151
|
+
onClick: () => r(e.id),
|
|
152
152
|
children: [
|
|
153
153
|
/* @__PURE__ */ t("span", { className: "sr-only", children: "Close" }),
|
|
154
154
|
/* @__PURE__ */ t("svg", { className: "h-5 w-5", viewBox: "0 0 20 20", fill: "currentColor", children: /* @__PURE__ */ t("path", { d: "M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z" }) })
|
|
@@ -159,7 +159,7 @@ function P({ toast: e, config: i, onRemove: n }) {
|
|
|
159
159
|
}
|
|
160
160
|
);
|
|
161
161
|
}
|
|
162
|
-
const
|
|
162
|
+
const g = {
|
|
163
163
|
"top-right": "top-4 right-4 items-end",
|
|
164
164
|
"top-left": "top-4 left-4 items-start",
|
|
165
165
|
"top-center": "top-4 left-1/2 -translate-x-1/2 items-center",
|
|
@@ -167,20 +167,20 @@ const v = {
|
|
|
167
167
|
"bottom-left": "bottom-4 left-4 items-start",
|
|
168
168
|
"bottom-center": "bottom-4 left-1/2 -translate-x-1/2 items-center"
|
|
169
169
|
};
|
|
170
|
-
function
|
|
171
|
-
const { items: e, config: i, remove:
|
|
170
|
+
function K() {
|
|
171
|
+
const { items: e, config: i, remove: r } = A(), s = g[i.position] ?? g["top-right"];
|
|
172
172
|
return /* @__PURE__ */ t(
|
|
173
173
|
"div",
|
|
174
174
|
{
|
|
175
175
|
className: `pointer-events-none fixed z-50 flex w-full max-w-sm flex-col gap-3 ${s}`,
|
|
176
176
|
"aria-live": "polite",
|
|
177
|
-
children: e.map((
|
|
177
|
+
children: e.map((o) => /* @__PURE__ */ t(B, { toast: o, config: i, onRemove: r }, o.id))
|
|
178
178
|
}
|
|
179
179
|
);
|
|
180
180
|
}
|
|
181
181
|
export {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
182
|
+
B as ToastItem,
|
|
183
|
+
j as ToastProvider,
|
|
184
|
+
K as Toasts,
|
|
185
|
+
A as useToast
|
|
186
186
|
};
|