npm_lib_toast 1.1.3
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/README.MD +25 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +11 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +46 -0
- package/rollup.config.js +43 -0
- package/src/components/Notification.css +115 -0
- package/src/components/Notification.tsx +64 -0
- package/src/components/types.ts +27 -0
- package/src/hooks/useNotification.tsx +59 -0
- package/src/index.ts +3 -0
- package/tsconfig.json +20 -0
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "npm_lib_toast",
|
|
3
|
+
"version": "1.1.3",
|
|
4
|
+
"description": "React Toast Popup is a simple and customizable toast notification component for React applications.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.mjs",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"rollup": "rollup -c --bundleConfigAsCjs",
|
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
11
|
+
},
|
|
12
|
+
"author": "",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@rollup/plugin-commonjs": "^26.0.1",
|
|
16
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
17
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
18
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
19
|
+
"@types/react": "^18.3.3",
|
|
20
|
+
"react": "^18.3.1",
|
|
21
|
+
"rollup": "^4.18.1",
|
|
22
|
+
"rollup-plugin-dts": "^6.1.1",
|
|
23
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
24
|
+
"rollup-plugin-postcss": "^4.0.2",
|
|
25
|
+
"tslib": "^2.6.3",
|
|
26
|
+
"typescript": "^5.5.3"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/kumarsantos/npm_lib_first_lib.git"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"react",
|
|
34
|
+
"react-toast",
|
|
35
|
+
"notification",
|
|
36
|
+
"toast",
|
|
37
|
+
"react-component",
|
|
38
|
+
"push",
|
|
39
|
+
"alert"
|
|
40
|
+
],
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@types/uuid": "^10.0.0",
|
|
43
|
+
"react-icons": "^5.2.1",
|
|
44
|
+
"uuid": "^10.0.0"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import resolve from "@rollup/plugin-node-resolve";
|
|
2
|
+
import commonjs from "@rollup/plugin-commonjs";
|
|
3
|
+
import typescript from "@rollup/plugin-typescript";
|
|
4
|
+
import dts from "rollup-plugin-dts";
|
|
5
|
+
import terser from "@rollup/plugin-terser";
|
|
6
|
+
import peerDepsExternal from "rollup-plugin-peer-deps-external";
|
|
7
|
+
|
|
8
|
+
import postcss from "rollup-plugin-postcss";
|
|
9
|
+
|
|
10
|
+
const packageJson = require("./package.json");
|
|
11
|
+
|
|
12
|
+
export default [
|
|
13
|
+
{
|
|
14
|
+
input: "src/index.ts",
|
|
15
|
+
output: [
|
|
16
|
+
{
|
|
17
|
+
file: packageJson.main,
|
|
18
|
+
format: "cjs",
|
|
19
|
+
sourcemap: true,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
file: packageJson.module,
|
|
23
|
+
format: "esm",
|
|
24
|
+
sourcemap: true,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
plugins: [
|
|
28
|
+
peerDepsExternal(),
|
|
29
|
+
resolve(),
|
|
30
|
+
commonjs(),
|
|
31
|
+
typescript({ tsconfig: "./tsconfig.json" }),
|
|
32
|
+
terser(),
|
|
33
|
+
postcss(),
|
|
34
|
+
],
|
|
35
|
+
external: ["react", "react-dom"],
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
input: "src/index.ts",
|
|
39
|
+
output: [{ file: packageJson.types }],
|
|
40
|
+
plugins: [dts.default()],
|
|
41
|
+
external: [/\.css$/],
|
|
42
|
+
},
|
|
43
|
+
];
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
.notification {
|
|
2
|
+
/* A11y */
|
|
3
|
+
outline: none;
|
|
4
|
+
/* A11y */
|
|
5
|
+
padding: 15px;
|
|
6
|
+
margin: 10px;
|
|
7
|
+
color: white;
|
|
8
|
+
display: flex;
|
|
9
|
+
align-items: center;
|
|
10
|
+
border-radius: 5px;
|
|
11
|
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.success {
|
|
15
|
+
background-color: #4caf50;
|
|
16
|
+
}
|
|
17
|
+
.info {
|
|
18
|
+
background-color: #2196f3;
|
|
19
|
+
}
|
|
20
|
+
.warning {
|
|
21
|
+
background-color: #ff9800;
|
|
22
|
+
}
|
|
23
|
+
.error {
|
|
24
|
+
background-color: #f44336;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/* Positioning */
|
|
28
|
+
.top-right {
|
|
29
|
+
position: fixed;
|
|
30
|
+
top: 20px;
|
|
31
|
+
right: 20px;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.top-left {
|
|
35
|
+
position: fixed;
|
|
36
|
+
top: 20px;
|
|
37
|
+
left: 20px;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.bottom-right {
|
|
41
|
+
position: fixed;
|
|
42
|
+
bottom: 20px;
|
|
43
|
+
right: 20px;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
.bottom-left {
|
|
47
|
+
position: fixed;
|
|
48
|
+
bottom: 20px;
|
|
49
|
+
left: 20px;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.notification-container {
|
|
53
|
+
display: flex;
|
|
54
|
+
/* Stack notifications from bottom to top */
|
|
55
|
+
align-items: flex-end; /* Align notifications to the right */
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.notification-container.bottom {
|
|
59
|
+
flex-direction: column-reverse;
|
|
60
|
+
}
|
|
61
|
+
.notification-container.top {
|
|
62
|
+
flex-direction: column;
|
|
63
|
+
}
|
|
64
|
+
/* Positioning */
|
|
65
|
+
|
|
66
|
+
.closeBtn {
|
|
67
|
+
margin-left: 5px;
|
|
68
|
+
background: none;
|
|
69
|
+
border: none;
|
|
70
|
+
padding: 0;
|
|
71
|
+
cursor: pointer;
|
|
72
|
+
display: flex;
|
|
73
|
+
align-items: center;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/* Animations */
|
|
77
|
+
@keyframes fadeIn {
|
|
78
|
+
from {
|
|
79
|
+
opacity: 0;
|
|
80
|
+
}
|
|
81
|
+
to {
|
|
82
|
+
opacity: 1;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@keyframes popup {
|
|
87
|
+
from {
|
|
88
|
+
transform: scale(0.5);
|
|
89
|
+
opacity: 0;
|
|
90
|
+
}
|
|
91
|
+
to {
|
|
92
|
+
transform: scale(1);
|
|
93
|
+
opacity: 1;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@keyframes slideIn {
|
|
98
|
+
from {
|
|
99
|
+
transform: translateY(100%);
|
|
100
|
+
}
|
|
101
|
+
to {
|
|
102
|
+
transform: translateY(0);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.fadeIn {
|
|
107
|
+
animation: fadeIn 0.5s;
|
|
108
|
+
}
|
|
109
|
+
.popup {
|
|
110
|
+
animation: popup 0.5s ease-out;
|
|
111
|
+
}
|
|
112
|
+
.slideIn {
|
|
113
|
+
animation: slideIn 0.5s ease-out;
|
|
114
|
+
}
|
|
115
|
+
/* Animations */
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from "react";
|
|
2
|
+
import {
|
|
3
|
+
AiOutlineCheckCircle,
|
|
4
|
+
AiOutlineInfoCircle,
|
|
5
|
+
AiOutlineWarning,
|
|
6
|
+
AiOutlineCloseCircle,
|
|
7
|
+
AiOutlineClose,
|
|
8
|
+
} from "react-icons/ai";
|
|
9
|
+
import "./Notification.css";
|
|
10
|
+
import { NotificationProps } from "./types";
|
|
11
|
+
|
|
12
|
+
const iconStyles: React.CSSProperties = { marginRight: "10px" };
|
|
13
|
+
const icons: Record<string, JSX.Element> = {
|
|
14
|
+
success: <AiOutlineCheckCircle style={iconStyles} />,
|
|
15
|
+
info: <AiOutlineInfoCircle style={iconStyles} />,
|
|
16
|
+
warning: <AiOutlineWarning style={iconStyles} />,
|
|
17
|
+
error: <AiOutlineCloseCircle style={iconStyles} />,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const animations: Record<string, string> = {
|
|
21
|
+
fade: "fadeIn",
|
|
22
|
+
pop: "popup",
|
|
23
|
+
slide: "slideIn",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const Notification: React.FC<NotificationProps> = ({
|
|
27
|
+
type = "info",
|
|
28
|
+
message,
|
|
29
|
+
onClose,
|
|
30
|
+
animation = "slide",
|
|
31
|
+
}) => {
|
|
32
|
+
// A11y
|
|
33
|
+
const notificationRef = useRef<HTMLDivElement>(null);
|
|
34
|
+
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (notificationRef.current) {
|
|
37
|
+
notificationRef.current.focus();
|
|
38
|
+
}
|
|
39
|
+
}, []);
|
|
40
|
+
|
|
41
|
+
const ariaRole = type === "error" || type === "warning" ? "alert" : "status";
|
|
42
|
+
const ariaLive =
|
|
43
|
+
type === "error" || type === "warning" ? "assertive" : "polite";
|
|
44
|
+
// A11y
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<div
|
|
48
|
+
className={`notification ${type} ${animations[animation]}`}
|
|
49
|
+
// A11y
|
|
50
|
+
role={ariaRole}
|
|
51
|
+
aria-live={ariaLive}
|
|
52
|
+
tabIndex={-1}
|
|
53
|
+
ref={notificationRef}
|
|
54
|
+
// A11y
|
|
55
|
+
>
|
|
56
|
+
{icons[type]} {message}
|
|
57
|
+
<button className="closeBtn" onClick={() => onClose()}>
|
|
58
|
+
<AiOutlineClose color="white" />
|
|
59
|
+
</button>
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export default Notification;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface NotificationProps {
|
|
2
|
+
type: "success" | "info" | "warning" | "error";
|
|
3
|
+
message: string;
|
|
4
|
+
onClose: () => void;
|
|
5
|
+
animation?: "fade" | "pop" | "slide";
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Define the allowed positions
|
|
9
|
+
export type PositionType =
|
|
10
|
+
| "bottom-left"
|
|
11
|
+
| "bottom-right"
|
|
12
|
+
| "top-left"
|
|
13
|
+
| "top-right";
|
|
14
|
+
|
|
15
|
+
// Define the properties of a notification
|
|
16
|
+
export interface NotificationProps {
|
|
17
|
+
type: "success" | "info" | "warning" | "error";
|
|
18
|
+
message: string;
|
|
19
|
+
duration: number;
|
|
20
|
+
animation?: "fade" | "pop" | "slide";
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Define the return type of the hook
|
|
24
|
+
export interface UseNotificationReturn {
|
|
25
|
+
NotificationComponent: JSX.Element;
|
|
26
|
+
triggerNotification: (notificationProps: NotificationProps) => void;
|
|
27
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import React, { useState, useCallback } from "react";
|
|
2
|
+
import Notification from "../components/Notification";
|
|
3
|
+
import { v4 as uuidv4 } from "uuid";
|
|
4
|
+
import {
|
|
5
|
+
NotificationProps,
|
|
6
|
+
PositionType,
|
|
7
|
+
UseNotificationReturn,
|
|
8
|
+
} from "../components/types";
|
|
9
|
+
|
|
10
|
+
const useNotification = (
|
|
11
|
+
position: PositionType = "bottom-right",
|
|
12
|
+
): UseNotificationReturn => {
|
|
13
|
+
const [notifications, setNotifications] = useState<
|
|
14
|
+
(NotificationProps & { id: string })[]
|
|
15
|
+
>([]);
|
|
16
|
+
|
|
17
|
+
const triggerNotification = useCallback(
|
|
18
|
+
(notificationProps: NotificationProps) => {
|
|
19
|
+
const toastId = uuidv4();
|
|
20
|
+
setNotifications((prevNotifications: any) => [
|
|
21
|
+
...prevNotifications,
|
|
22
|
+
{ id: toastId, ...notificationProps },
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
setTimeout(() => {
|
|
26
|
+
setNotifications((prevNotifications: any) =>
|
|
27
|
+
prevNotifications.filter((n: any) => n.id !== toastId),
|
|
28
|
+
);
|
|
29
|
+
}, notificationProps.duration);
|
|
30
|
+
},
|
|
31
|
+
[],
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const handleNotificationClose = (index: number) => {
|
|
35
|
+
setNotifications((prevNotifications: any) => {
|
|
36
|
+
const updatedNotifications = [...prevNotifications];
|
|
37
|
+
updatedNotifications.splice(index, 1);
|
|
38
|
+
return updatedNotifications;
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const NotificationComponent = (
|
|
43
|
+
<div
|
|
44
|
+
className={`notification-container ${position} ${position.split("-")[0]}`}
|
|
45
|
+
>
|
|
46
|
+
{notifications.map((notification: any, index: number) => (
|
|
47
|
+
<Notification
|
|
48
|
+
key={notification.id}
|
|
49
|
+
{...notification}
|
|
50
|
+
onClose={() => handleNotificationClose(index)}
|
|
51
|
+
/>
|
|
52
|
+
))}
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return { NotificationComponent, triggerNotification };
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default useNotification;
|
package/src/index.ts
ADDED
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"lib": ["DOM","DOM.Iterable","ESNext"],
|
|
5
|
+
"allowJs": true,
|
|
6
|
+
"skipLibCheck": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"allowSyntheticDefaultImports": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"noFallthroughCasesInSwitch": true,
|
|
12
|
+
"module": "ESNext",
|
|
13
|
+
"moduleResolution": "Node",
|
|
14
|
+
"resolveJsonModule": true,
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"noEmit": true,
|
|
17
|
+
"jsx": "react-jsx"
|
|
18
|
+
},
|
|
19
|
+
"include": ["src"]
|
|
20
|
+
}
|