react-inspire-toast 1.0.0
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.mts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +120 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +81 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +45 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type ToastType = "info" | "success" | "error" | "warning";
|
|
4
|
+
interface Toast {
|
|
5
|
+
id: number;
|
|
6
|
+
message: string;
|
|
7
|
+
type: ToastType;
|
|
8
|
+
}
|
|
9
|
+
interface ToastOptions {
|
|
10
|
+
truncate?: number;
|
|
11
|
+
}
|
|
12
|
+
interface ToastContextProps {
|
|
13
|
+
addToast: (message: string, type?: ToastType, duration?: number, options?: ToastOptions) => void;
|
|
14
|
+
}
|
|
15
|
+
declare const useToast: () => ToastContextProps;
|
|
16
|
+
declare const ToastProvider: React.FC<{
|
|
17
|
+
children: React.ReactNode;
|
|
18
|
+
}>;
|
|
19
|
+
|
|
20
|
+
declare function showQuoteToast(addToast: (message: string, type: ToastType, duration: number) => void, duration?: number, type?: ToastType): Promise<void>;
|
|
21
|
+
|
|
22
|
+
export { type Toast, type ToastOptions, ToastProvider, type ToastType, showQuoteToast, useToast };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
type ToastType = "info" | "success" | "error" | "warning";
|
|
4
|
+
interface Toast {
|
|
5
|
+
id: number;
|
|
6
|
+
message: string;
|
|
7
|
+
type: ToastType;
|
|
8
|
+
}
|
|
9
|
+
interface ToastOptions {
|
|
10
|
+
truncate?: number;
|
|
11
|
+
}
|
|
12
|
+
interface ToastContextProps {
|
|
13
|
+
addToast: (message: string, type?: ToastType, duration?: number, options?: ToastOptions) => void;
|
|
14
|
+
}
|
|
15
|
+
declare const useToast: () => ToastContextProps;
|
|
16
|
+
declare const ToastProvider: React.FC<{
|
|
17
|
+
children: React.ReactNode;
|
|
18
|
+
}>;
|
|
19
|
+
|
|
20
|
+
declare function showQuoteToast(addToast: (message: string, type: ToastType, duration: number) => void, duration?: number, type?: ToastType): Promise<void>;
|
|
21
|
+
|
|
22
|
+
export { type Toast, type ToastOptions, ToastProvider, type ToastType, showQuoteToast, useToast };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
ToastProvider: () => ToastProvider,
|
|
34
|
+
showQuoteToast: () => showQuoteToast,
|
|
35
|
+
useToast: () => useToast
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(index_exports);
|
|
38
|
+
|
|
39
|
+
// src/ToastProvider.tsx
|
|
40
|
+
var import_react = __toESM(require("react"));
|
|
41
|
+
|
|
42
|
+
// #style-inject:#style-inject
|
|
43
|
+
function styleInject(css, { insertAt } = {}) {
|
|
44
|
+
if (!css || typeof document === "undefined") return;
|
|
45
|
+
const head = document.head || document.getElementsByTagName("head")[0];
|
|
46
|
+
const style = document.createElement("style");
|
|
47
|
+
style.type = "text/css";
|
|
48
|
+
if (insertAt === "top") {
|
|
49
|
+
if (head.firstChild) {
|
|
50
|
+
head.insertBefore(style, head.firstChild);
|
|
51
|
+
} else {
|
|
52
|
+
head.appendChild(style);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
head.appendChild(style);
|
|
56
|
+
}
|
|
57
|
+
if (style.styleSheet) {
|
|
58
|
+
style.styleSheet.cssText = css;
|
|
59
|
+
} else {
|
|
60
|
+
style.appendChild(document.createTextNode(css));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/toast.css
|
|
65
|
+
styleInject(".toast-container {\n position: fixed;\n top: 1rem;\n right: 1rem;\n width: 320px;\n max-height: 80vh;\n overflow-y: auto;\n z-index: 9999;\n display: flex;\n flex-direction: column;\n gap: 0.75rem;\n}\n.toast {\n padding: 1rem 1.5rem;\n border-radius: 8px;\n color: #fff;\n font-size: 1rem;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);\n animation: fadeIn 0.3s;\n word-break: break-word;\n}\n.toast-info {\n background: #2196f3;\n}\n.toast-success {\n background: #43a047;\n}\n.toast-error {\n background: #e53935;\n}\n.toast-warning {\n background: #ffa000;\n color: #222;\n}\n@keyframes fadeIn {\n from {\n opacity: 0;\n transform: translateY(-10px);\n }\n to {\n opacity: 1;\n transform: translateY(0);\n }\n}\n");
|
|
66
|
+
|
|
67
|
+
// src/ToastProvider.tsx
|
|
68
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
69
|
+
var ToastContext = import_react.default.createContext(
|
|
70
|
+
void 0
|
|
71
|
+
);
|
|
72
|
+
var useToast = () => {
|
|
73
|
+
const ctx = import_react.default.useContext(ToastContext);
|
|
74
|
+
if (!ctx) throw new Error("useToast must be used within ToastProvider");
|
|
75
|
+
return ctx;
|
|
76
|
+
};
|
|
77
|
+
var ToastProvider = ({
|
|
78
|
+
children
|
|
79
|
+
}) => {
|
|
80
|
+
const [toasts, setToasts] = (0, import_react.useState)([]);
|
|
81
|
+
const idRef = (0, import_react.useRef)(0);
|
|
82
|
+
const addToast = (0, import_react.useCallback)(
|
|
83
|
+
(message, type = "info", duration = 4e3, options) => {
|
|
84
|
+
let displayMsg = message;
|
|
85
|
+
if (options?.truncate && message.length > options.truncate) {
|
|
86
|
+
displayMsg = message.slice(0, options.truncate) + "...";
|
|
87
|
+
}
|
|
88
|
+
const id = ++idRef.current;
|
|
89
|
+
setToasts((prev) => [...prev, { id, message: displayMsg, type }]);
|
|
90
|
+
setTimeout(() => {
|
|
91
|
+
setToasts((prev) => prev.filter((t) => t.id !== id));
|
|
92
|
+
}, duration);
|
|
93
|
+
},
|
|
94
|
+
[]
|
|
95
|
+
);
|
|
96
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ToastContext.Provider, { value: { addToast }, children: [
|
|
97
|
+
children,
|
|
98
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "toast-container", children: toasts.map((toast) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: `toast toast-${toast.type}`, children: toast.message }, toast.id)) })
|
|
99
|
+
] });
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// src/showQuoteToast.ts
|
|
103
|
+
async function showQuoteToast(addToast, duration = 4e3, type = "info") {
|
|
104
|
+
try {
|
|
105
|
+
const res = await fetch(
|
|
106
|
+
"https://thequoteshub.com/api/random-quote?format=json"
|
|
107
|
+
);
|
|
108
|
+
const data = await res.json();
|
|
109
|
+
addToast(data.text, type, duration);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
addToast("Failed to fetch quote", "error", duration);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
115
|
+
0 && (module.exports = {
|
|
116
|
+
ToastProvider,
|
|
117
|
+
showQuoteToast,
|
|
118
|
+
useToast
|
|
119
|
+
});
|
|
120
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/ToastProvider.tsx","#style-inject:#style-inject","../src/toast.css","../src/showQuoteToast.ts"],"sourcesContent":["export { ToastProvider, useToast } from \"./ToastProvider\";\r\nexport { showQuoteToast } from \"./showQuoteToast\";\r\nexport type { Toast, ToastType, ToastOptions } from \"./ToastProvider\";\r\n","import React, { useCallback, useRef, useState } from \"react\";\r\nimport \"./toast.css\";\r\n\r\nexport type ToastType = \"info\" | \"success\" | \"error\" | \"warning\";\r\n\r\nexport interface Toast {\r\n id: number;\r\n message: string;\r\n type: ToastType;\r\n}\r\n\r\nexport interface ToastOptions {\r\n truncate?: number; // max length of message, if set\r\n}\r\n\r\ninterface ToastContextProps {\r\n addToast: (\r\n message: string,\r\n type?: ToastType,\r\n duration?: number,\r\n options?: ToastOptions,\r\n ) => void;\r\n}\r\n\r\nconst ToastContext = React.createContext<ToastContextProps | undefined>(\r\n undefined,\r\n);\r\n\r\nexport const useToast = () => {\r\n const ctx = React.useContext(ToastContext);\r\n if (!ctx) throw new Error(\"useToast must be used within ToastProvider\");\r\n return ctx;\r\n};\r\n\r\nexport const ToastProvider: React.FC<{ children: React.ReactNode }> = ({\r\n children,\r\n}) => {\r\n const [toasts, setToasts] = useState<Toast[]>([]);\r\n const idRef = useRef(0);\r\n\r\n const addToast = useCallback(\r\n (\r\n message: string,\r\n type: ToastType = \"info\",\r\n duration = 4000,\r\n options?: ToastOptions,\r\n ) => {\r\n let displayMsg = message;\r\n if (options?.truncate && message.length > options.truncate) {\r\n displayMsg = message.slice(0, options.truncate) + \"...\";\r\n }\r\n const id = ++idRef.current;\r\n setToasts((prev) => [...prev, { id, message: displayMsg, type }]);\r\n setTimeout(() => {\r\n setToasts((prev) => prev.filter((t) => t.id !== id));\r\n }, duration);\r\n },\r\n [],\r\n );\r\n\r\n return (\r\n <ToastContext.Provider value={{ addToast }}>\r\n {children}\r\n <div className=\"toast-container\">\r\n {toasts.map((toast) => (\r\n <div key={toast.id} className={`toast toast-${toast.type}`}>\r\n {toast.message}\r\n </div>\r\n ))}\r\n </div>\r\n </ToastContext.Provider>\r\n );\r\n};\r\n","\n export default function styleInject(css, { insertAt } = {}) {\n if (!css || typeof document === 'undefined') return\n \n const head = document.head || document.getElementsByTagName('head')[0]\n const style = document.createElement('style')\n style.type = 'text/css'\n \n if (insertAt === 'top') {\n if (head.firstChild) {\n head.insertBefore(style, head.firstChild)\n } else {\n head.appendChild(style)\n }\n } else {\n head.appendChild(style)\n }\n \n if (style.styleSheet) {\n style.styleSheet.cssText = css\n } else {\n style.appendChild(document.createTextNode(css))\n }\n }\n ","import styleInject from '#style-inject';styleInject(\".toast-container {\\n position: fixed;\\n top: 1rem;\\n right: 1rem;\\n width: 320px;\\n max-height: 80vh;\\n overflow-y: auto;\\n z-index: 9999;\\n display: flex;\\n flex-direction: column;\\n gap: 0.75rem;\\n}\\n.toast {\\n padding: 1rem 1.5rem;\\n border-radius: 8px;\\n color: #fff;\\n font-size: 1rem;\\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);\\n animation: fadeIn 0.3s;\\n word-break: break-word;\\n}\\n.toast-info {\\n background: #2196f3;\\n}\\n.toast-success {\\n background: #43a047;\\n}\\n.toast-error {\\n background: #e53935;\\n}\\n.toast-warning {\\n background: #ffa000;\\n color: #222;\\n}\\n@keyframes fadeIn {\\n from {\\n opacity: 0;\\n transform: translateY(-10px);\\n }\\n to {\\n opacity: 1;\\n transform: translateY(0);\\n }\\n}\\n\")","// useQuoteToast.ts\r\nimport { type ToastType } from \"./ToastProvider\";\r\n\r\nexport async function showQuoteToast(\r\n addToast: (message: string, type: ToastType, duration: number) => void,\r\n duration = 4000,\r\n type: ToastType = \"info\",\r\n) {\r\n try {\r\n const res = await fetch(\r\n \"https://thequoteshub.com/api/random-quote?format=json\",\r\n );\r\n const data = await res.json();\r\n addToast(data.text, type, duration);\r\n } catch (error) {\r\n addToast(\"Failed to fetch quote\", \"error\", duration);\r\n }\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAAqD;;;ACC5B,SAAR,YAA6B,KAAK,EAAE,SAAS,IAAI,CAAC,GAAG;AAC1D,MAAI,CAAC,OAAO,OAAO,aAAa,YAAa;AAE7C,QAAM,OAAO,SAAS,QAAQ,SAAS,qBAAqB,MAAM,EAAE,CAAC;AACrE,QAAM,QAAQ,SAAS,cAAc,OAAO;AAC5C,QAAM,OAAO;AAEb,MAAI,aAAa,OAAO;AACtB,QAAI,KAAK,YAAY;AACnB,WAAK,aAAa,OAAO,KAAK,UAAU;AAAA,IAC1C,OAAO;AACL,WAAK,YAAY,KAAK;AAAA,IACxB;AAAA,EACF,OAAO;AACL,SAAK,YAAY,KAAK;AAAA,EACxB;AAEA,MAAI,MAAM,YAAY;AACpB,UAAM,WAAW,UAAU;AAAA,EAC7B,OAAO;AACL,UAAM,YAAY,SAAS,eAAe,GAAG,CAAC;AAAA,EAChD;AACF;;;ACvB8B,YAAY,kvBAAkvB;;;AF6DlyB;AArCJ,IAAM,eAAe,aAAAA,QAAM;AAAA,EACzB;AACF;AAEO,IAAM,WAAW,MAAM;AAC5B,QAAM,MAAM,aAAAA,QAAM,WAAW,YAAY;AACzC,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,4CAA4C;AACtE,SAAO;AACT;AAEO,IAAM,gBAAyD,CAAC;AAAA,EACrE;AACF,MAAM;AACJ,QAAM,CAAC,QAAQ,SAAS,QAAI,uBAAkB,CAAC,CAAC;AAChD,QAAM,YAAQ,qBAAO,CAAC;AAEtB,QAAM,eAAW;AAAA,IACf,CACE,SACA,OAAkB,QAClB,WAAW,KACX,YACG;AACH,UAAI,aAAa;AACjB,UAAI,SAAS,YAAY,QAAQ,SAAS,QAAQ,UAAU;AAC1D,qBAAa,QAAQ,MAAM,GAAG,QAAQ,QAAQ,IAAI;AAAA,MACpD;AACA,YAAM,KAAK,EAAE,MAAM;AACnB,gBAAU,CAAC,SAAS,CAAC,GAAG,MAAM,EAAE,IAAI,SAAS,YAAY,KAAK,CAAC,CAAC;AAChE,iBAAW,MAAM;AACf,kBAAU,CAAC,SAAS,KAAK,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;AAAA,MACrD,GAAG,QAAQ;AAAA,IACb;AAAA,IACA,CAAC;AAAA,EACH;AAEA,SACE,6CAAC,aAAa,UAAb,EAAsB,OAAO,EAAE,SAAS,GACtC;AAAA;AAAA,IACD,4CAAC,SAAI,WAAU,mBACZ,iBAAO,IAAI,CAAC,UACX,4CAAC,SAAmB,WAAW,eAAe,MAAM,IAAI,IACrD,gBAAM,WADC,MAAM,EAEhB,CACD,GACH;AAAA,KACF;AAEJ;;;AGrEA,eAAsB,eACpB,UACA,WAAW,KACX,OAAkB,QAClB;AACA,MAAI;AACF,UAAM,MAAM,MAAM;AAAA,MAChB;AAAA,IACF;AACA,UAAM,OAAO,MAAM,IAAI,KAAK;AAC5B,aAAS,KAAK,MAAM,MAAM,QAAQ;AAAA,EACpC,SAAS,OAAO;AACd,aAAS,yBAAyB,SAAS,QAAQ;AAAA,EACrD;AACF;","names":["React"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// src/ToastProvider.tsx
|
|
2
|
+
import React, { useCallback, useRef, useState } from "react";
|
|
3
|
+
|
|
4
|
+
// #style-inject:#style-inject
|
|
5
|
+
function styleInject(css, { insertAt } = {}) {
|
|
6
|
+
if (!css || typeof document === "undefined") return;
|
|
7
|
+
const head = document.head || document.getElementsByTagName("head")[0];
|
|
8
|
+
const style = document.createElement("style");
|
|
9
|
+
style.type = "text/css";
|
|
10
|
+
if (insertAt === "top") {
|
|
11
|
+
if (head.firstChild) {
|
|
12
|
+
head.insertBefore(style, head.firstChild);
|
|
13
|
+
} else {
|
|
14
|
+
head.appendChild(style);
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
head.appendChild(style);
|
|
18
|
+
}
|
|
19
|
+
if (style.styleSheet) {
|
|
20
|
+
style.styleSheet.cssText = css;
|
|
21
|
+
} else {
|
|
22
|
+
style.appendChild(document.createTextNode(css));
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/toast.css
|
|
27
|
+
styleInject(".toast-container {\n position: fixed;\n top: 1rem;\n right: 1rem;\n width: 320px;\n max-height: 80vh;\n overflow-y: auto;\n z-index: 9999;\n display: flex;\n flex-direction: column;\n gap: 0.75rem;\n}\n.toast {\n padding: 1rem 1.5rem;\n border-radius: 8px;\n color: #fff;\n font-size: 1rem;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);\n animation: fadeIn 0.3s;\n word-break: break-word;\n}\n.toast-info {\n background: #2196f3;\n}\n.toast-success {\n background: #43a047;\n}\n.toast-error {\n background: #e53935;\n}\n.toast-warning {\n background: #ffa000;\n color: #222;\n}\n@keyframes fadeIn {\n from {\n opacity: 0;\n transform: translateY(-10px);\n }\n to {\n opacity: 1;\n transform: translateY(0);\n }\n}\n");
|
|
28
|
+
|
|
29
|
+
// src/ToastProvider.tsx
|
|
30
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
31
|
+
var ToastContext = React.createContext(
|
|
32
|
+
void 0
|
|
33
|
+
);
|
|
34
|
+
var useToast = () => {
|
|
35
|
+
const ctx = React.useContext(ToastContext);
|
|
36
|
+
if (!ctx) throw new Error("useToast must be used within ToastProvider");
|
|
37
|
+
return ctx;
|
|
38
|
+
};
|
|
39
|
+
var ToastProvider = ({
|
|
40
|
+
children
|
|
41
|
+
}) => {
|
|
42
|
+
const [toasts, setToasts] = useState([]);
|
|
43
|
+
const idRef = useRef(0);
|
|
44
|
+
const addToast = useCallback(
|
|
45
|
+
(message, type = "info", duration = 4e3, options) => {
|
|
46
|
+
let displayMsg = message;
|
|
47
|
+
if (options?.truncate && message.length > options.truncate) {
|
|
48
|
+
displayMsg = message.slice(0, options.truncate) + "...";
|
|
49
|
+
}
|
|
50
|
+
const id = ++idRef.current;
|
|
51
|
+
setToasts((prev) => [...prev, { id, message: displayMsg, type }]);
|
|
52
|
+
setTimeout(() => {
|
|
53
|
+
setToasts((prev) => prev.filter((t) => t.id !== id));
|
|
54
|
+
}, duration);
|
|
55
|
+
},
|
|
56
|
+
[]
|
|
57
|
+
);
|
|
58
|
+
return /* @__PURE__ */ jsxs(ToastContext.Provider, { value: { addToast }, children: [
|
|
59
|
+
children,
|
|
60
|
+
/* @__PURE__ */ jsx("div", { className: "toast-container", children: toasts.map((toast) => /* @__PURE__ */ jsx("div", { className: `toast toast-${toast.type}`, children: toast.message }, toast.id)) })
|
|
61
|
+
] });
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/showQuoteToast.ts
|
|
65
|
+
async function showQuoteToast(addToast, duration = 4e3, type = "info") {
|
|
66
|
+
try {
|
|
67
|
+
const res = await fetch(
|
|
68
|
+
"https://thequoteshub.com/api/random-quote?format=json"
|
|
69
|
+
);
|
|
70
|
+
const data = await res.json();
|
|
71
|
+
addToast(data.text, type, duration);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
addToast("Failed to fetch quote", "error", duration);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
ToastProvider,
|
|
78
|
+
showQuoteToast,
|
|
79
|
+
useToast
|
|
80
|
+
};
|
|
81
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/ToastProvider.tsx","#style-inject:#style-inject","../src/toast.css","../src/showQuoteToast.ts"],"sourcesContent":["import React, { useCallback, useRef, useState } from \"react\";\r\nimport \"./toast.css\";\r\n\r\nexport type ToastType = \"info\" | \"success\" | \"error\" | \"warning\";\r\n\r\nexport interface Toast {\r\n id: number;\r\n message: string;\r\n type: ToastType;\r\n}\r\n\r\nexport interface ToastOptions {\r\n truncate?: number; // max length of message, if set\r\n}\r\n\r\ninterface ToastContextProps {\r\n addToast: (\r\n message: string,\r\n type?: ToastType,\r\n duration?: number,\r\n options?: ToastOptions,\r\n ) => void;\r\n}\r\n\r\nconst ToastContext = React.createContext<ToastContextProps | undefined>(\r\n undefined,\r\n);\r\n\r\nexport const useToast = () => {\r\n const ctx = React.useContext(ToastContext);\r\n if (!ctx) throw new Error(\"useToast must be used within ToastProvider\");\r\n return ctx;\r\n};\r\n\r\nexport const ToastProvider: React.FC<{ children: React.ReactNode }> = ({\r\n children,\r\n}) => {\r\n const [toasts, setToasts] = useState<Toast[]>([]);\r\n const idRef = useRef(0);\r\n\r\n const addToast = useCallback(\r\n (\r\n message: string,\r\n type: ToastType = \"info\",\r\n duration = 4000,\r\n options?: ToastOptions,\r\n ) => {\r\n let displayMsg = message;\r\n if (options?.truncate && message.length > options.truncate) {\r\n displayMsg = message.slice(0, options.truncate) + \"...\";\r\n }\r\n const id = ++idRef.current;\r\n setToasts((prev) => [...prev, { id, message: displayMsg, type }]);\r\n setTimeout(() => {\r\n setToasts((prev) => prev.filter((t) => t.id !== id));\r\n }, duration);\r\n },\r\n [],\r\n );\r\n\r\n return (\r\n <ToastContext.Provider value={{ addToast }}>\r\n {children}\r\n <div className=\"toast-container\">\r\n {toasts.map((toast) => (\r\n <div key={toast.id} className={`toast toast-${toast.type}`}>\r\n {toast.message}\r\n </div>\r\n ))}\r\n </div>\r\n </ToastContext.Provider>\r\n );\r\n};\r\n","\n export default function styleInject(css, { insertAt } = {}) {\n if (!css || typeof document === 'undefined') return\n \n const head = document.head || document.getElementsByTagName('head')[0]\n const style = document.createElement('style')\n style.type = 'text/css'\n \n if (insertAt === 'top') {\n if (head.firstChild) {\n head.insertBefore(style, head.firstChild)\n } else {\n head.appendChild(style)\n }\n } else {\n head.appendChild(style)\n }\n \n if (style.styleSheet) {\n style.styleSheet.cssText = css\n } else {\n style.appendChild(document.createTextNode(css))\n }\n }\n ","import styleInject from '#style-inject';styleInject(\".toast-container {\\n position: fixed;\\n top: 1rem;\\n right: 1rem;\\n width: 320px;\\n max-height: 80vh;\\n overflow-y: auto;\\n z-index: 9999;\\n display: flex;\\n flex-direction: column;\\n gap: 0.75rem;\\n}\\n.toast {\\n padding: 1rem 1.5rem;\\n border-radius: 8px;\\n color: #fff;\\n font-size: 1rem;\\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);\\n animation: fadeIn 0.3s;\\n word-break: break-word;\\n}\\n.toast-info {\\n background: #2196f3;\\n}\\n.toast-success {\\n background: #43a047;\\n}\\n.toast-error {\\n background: #e53935;\\n}\\n.toast-warning {\\n background: #ffa000;\\n color: #222;\\n}\\n@keyframes fadeIn {\\n from {\\n opacity: 0;\\n transform: translateY(-10px);\\n }\\n to {\\n opacity: 1;\\n transform: translateY(0);\\n }\\n}\\n\")","// useQuoteToast.ts\r\nimport { type ToastType } from \"./ToastProvider\";\r\n\r\nexport async function showQuoteToast(\r\n addToast: (message: string, type: ToastType, duration: number) => void,\r\n duration = 4000,\r\n type: ToastType = \"info\",\r\n) {\r\n try {\r\n const res = await fetch(\r\n \"https://thequoteshub.com/api/random-quote?format=json\",\r\n );\r\n const data = await res.json();\r\n addToast(data.text, type, duration);\r\n } catch (error) {\r\n addToast(\"Failed to fetch quote\", \"error\", duration);\r\n }\r\n}\r\n"],"mappings":";AAAA,OAAO,SAAS,aAAa,QAAQ,gBAAgB;;;ACC5B,SAAR,YAA6B,KAAK,EAAE,SAAS,IAAI,CAAC,GAAG;AAC1D,MAAI,CAAC,OAAO,OAAO,aAAa,YAAa;AAE7C,QAAM,OAAO,SAAS,QAAQ,SAAS,qBAAqB,MAAM,EAAE,CAAC;AACrE,QAAM,QAAQ,SAAS,cAAc,OAAO;AAC5C,QAAM,OAAO;AAEb,MAAI,aAAa,OAAO;AACtB,QAAI,KAAK,YAAY;AACnB,WAAK,aAAa,OAAO,KAAK,UAAU;AAAA,IAC1C,OAAO;AACL,WAAK,YAAY,KAAK;AAAA,IACxB;AAAA,EACF,OAAO;AACL,SAAK,YAAY,KAAK;AAAA,EACxB;AAEA,MAAI,MAAM,YAAY;AACpB,UAAM,WAAW,UAAU;AAAA,EAC7B,OAAO;AACL,UAAM,YAAY,SAAS,eAAe,GAAG,CAAC;AAAA,EAChD;AACF;;;ACvB8B,YAAY,kvBAAkvB;;;AF6DlyB,SAIM,KAJN;AArCJ,IAAM,eAAe,MAAM;AAAA,EACzB;AACF;AAEO,IAAM,WAAW,MAAM;AAC5B,QAAM,MAAM,MAAM,WAAW,YAAY;AACzC,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,4CAA4C;AACtE,SAAO;AACT;AAEO,IAAM,gBAAyD,CAAC;AAAA,EACrE;AACF,MAAM;AACJ,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAkB,CAAC,CAAC;AAChD,QAAM,QAAQ,OAAO,CAAC;AAEtB,QAAM,WAAW;AAAA,IACf,CACE,SACA,OAAkB,QAClB,WAAW,KACX,YACG;AACH,UAAI,aAAa;AACjB,UAAI,SAAS,YAAY,QAAQ,SAAS,QAAQ,UAAU;AAC1D,qBAAa,QAAQ,MAAM,GAAG,QAAQ,QAAQ,IAAI;AAAA,MACpD;AACA,YAAM,KAAK,EAAE,MAAM;AACnB,gBAAU,CAAC,SAAS,CAAC,GAAG,MAAM,EAAE,IAAI,SAAS,YAAY,KAAK,CAAC,CAAC;AAChE,iBAAW,MAAM;AACf,kBAAU,CAAC,SAAS,KAAK,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;AAAA,MACrD,GAAG,QAAQ;AAAA,IACb;AAAA,IACA,CAAC;AAAA,EACH;AAEA,SACE,qBAAC,aAAa,UAAb,EAAsB,OAAO,EAAE,SAAS,GACtC;AAAA;AAAA,IACD,oBAAC,SAAI,WAAU,mBACZ,iBAAO,IAAI,CAAC,UACX,oBAAC,SAAmB,WAAW,eAAe,MAAM,IAAI,IACrD,gBAAM,WADC,MAAM,EAEhB,CACD,GACH;AAAA,KACF;AAEJ;;;AGrEA,eAAsB,eACpB,UACA,WAAW,KACX,OAAkB,QAClB;AACA,MAAI;AACF,UAAM,MAAM,MAAM;AAAA,MAChB;AAAA,IACF;AACA,UAAM,OAAO,MAAM,IAAI,KAAK;AAC5B,aAAS,KAAK,MAAM,MAAM,QAAQ;AAAA,EACpC,SAAS,OAAO;AACd,aAAS,yBAAyB,SAAS,QAAQ;AAAA,EACrD;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "react-inspire-toast",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Inspiring quote toast notifications for React",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsup",
|
|
20
|
+
"dev": "tsup --watch",
|
|
21
|
+
"prepublishOnly": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/react": "^19.2.13",
|
|
28
|
+
"esbuild-style-plugin": "^1.6.3",
|
|
29
|
+
"tsup": "^8.5.1",
|
|
30
|
+
"typescript": "^5.9.3"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git@github-personal:Mohid-Anwar/react-inspire-toast.git"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"react",
|
|
38
|
+
"toast",
|
|
39
|
+
"notifications",
|
|
40
|
+
"quotes",
|
|
41
|
+
"inspiration"
|
|
42
|
+
],
|
|
43
|
+
"author": "Mohid Anwar",
|
|
44
|
+
"license": "MIT"
|
|
45
|
+
}
|