d9-toast 1.0.10 → 1.0.11
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/Toast.js +63 -87
- package/dist/ToastContext.js +23 -22
- package/package.json +1 -1
package/dist/Toast.js
CHANGED
|
@@ -6,7 +6,7 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
|
|
|
6
6
|
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
7
7
|
import React, { useEffect, useRef, useState } from "react";
|
|
8
8
|
import Icons from "./Icons";
|
|
9
|
-
import { jsx as _jsx, jsxs as _jsxs
|
|
9
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
10
10
|
var Toast = function Toast(_ref) {
|
|
11
11
|
var id = _ref.id,
|
|
12
12
|
message = _ref.message,
|
|
@@ -28,25 +28,19 @@ var Toast = function Toast(_ref) {
|
|
|
28
28
|
pauseOnHover = _ref$pauseOnHover === void 0 ? true : _ref$pauseOnHover,
|
|
29
29
|
_ref$pauseOnFocusLoss = _ref.pauseOnFocusLoss,
|
|
30
30
|
pauseOnFocusLoss = _ref$pauseOnFocusLoss === void 0 ? true : _ref$pauseOnFocusLoss;
|
|
31
|
-
var intervalRef = useRef(null);
|
|
32
|
-
var start = useRef(Date.now());
|
|
33
|
-
var remaining = useRef(duration);
|
|
31
|
+
var intervalRef = useRef(null);
|
|
32
|
+
var start = useRef(Date.now());
|
|
33
|
+
var remaining = useRef(duration);
|
|
34
34
|
var _useState = useState(100),
|
|
35
35
|
_useState2 = _slicedToArray(_useState, 2),
|
|
36
36
|
progressWidth = _useState2[0],
|
|
37
|
-
setProgressWidth = _useState2[1];
|
|
37
|
+
setProgressWidth = _useState2[1];
|
|
38
38
|
var _useState3 = useState(false),
|
|
39
39
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
40
40
|
isPaused = _useState4[0],
|
|
41
|
-
setPaused = _useState4[1];
|
|
42
|
-
|
|
43
|
-
// Start auto-close timer.
|
|
41
|
+
setPaused = _useState4[1];
|
|
44
42
|
useEffect(function () {
|
|
45
|
-
if (duration !== 0)
|
|
46
|
-
startTimer();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// pause/resume when window focus changes.
|
|
43
|
+
if (duration !== 0) startTimer();
|
|
50
44
|
var handleBlur = function handleBlur() {
|
|
51
45
|
return pauseOnFocusLoss && pauseTimer();
|
|
52
46
|
};
|
|
@@ -65,103 +59,85 @@ var Toast = function Toast(_ref) {
|
|
|
65
59
|
}
|
|
66
60
|
};
|
|
67
61
|
}, [duration, pauseOnFocusLoss]);
|
|
68
|
-
|
|
69
|
-
// --- Helpers Fun ---
|
|
70
|
-
// for start
|
|
71
62
|
var startTimer = function startTimer() {
|
|
72
63
|
intervalRef.current = setInterval(function () {
|
|
73
|
-
// time to passed...
|
|
74
64
|
var elapsed = Date.now() - start.current;
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
setProgressWidth(Math.max(timeToLeft / duration * 100, 0));
|
|
79
|
-
if (timeToLeft <= 0) {
|
|
80
|
-
// stop timer...
|
|
65
|
+
var timeLeft = remaining.current - elapsed;
|
|
66
|
+
setProgressWidth(timeLeft / duration * 100);
|
|
67
|
+
if (timeLeft <= 0) {
|
|
81
68
|
clearInterval(intervalRef.current);
|
|
82
|
-
// remove toast.
|
|
83
69
|
remove();
|
|
84
70
|
}
|
|
85
71
|
}, 50);
|
|
86
72
|
};
|
|
87
|
-
|
|
88
|
-
// for pause
|
|
89
73
|
var pauseTimer = function pauseTimer() {
|
|
90
74
|
if (isPaused) return;
|
|
91
75
|
clearInterval(intervalRef.current);
|
|
92
|
-
remaining.current
|
|
76
|
+
remaining.current = remaining.current - (Date.now() - start.current);
|
|
93
77
|
setPaused(true);
|
|
94
78
|
};
|
|
95
|
-
|
|
96
|
-
// for resume
|
|
97
79
|
var resumeTimer = function resumeTimer() {
|
|
98
80
|
if (!isPaused) return;
|
|
99
81
|
start.current = Date.now();
|
|
100
82
|
setPaused(false);
|
|
101
83
|
startTimer();
|
|
102
84
|
};
|
|
103
|
-
|
|
104
|
-
// Styles
|
|
105
85
|
var colors = {
|
|
106
|
-
success: "bg-green-
|
|
107
|
-
error: "bg-red-
|
|
108
|
-
info: "bg-blue-
|
|
109
|
-
warning: "bg-yellow-
|
|
110
|
-
loading: theme === "light" ? "bg-gray-
|
|
111
|
-
submit: theme === "light" ? "bg-gray-600/20 text-gray-600" : "bg-gray-600/50 text-gray-200"
|
|
86
|
+
success: "bg-green-100 text-green-700",
|
|
87
|
+
error: "bg-red-100 text-red-700",
|
|
88
|
+
info: "bg-blue-100 text-blue-700",
|
|
89
|
+
warning: "bg-yellow-100 text-yellow-700",
|
|
90
|
+
loading: theme === "light" ? "bg-gray-100 text-gray-900" : "bg-gray-900 text-gray-100"
|
|
112
91
|
};
|
|
113
|
-
var
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
className: "
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
className: " hover:text-red-600 cursor-pointer",
|
|
134
|
-
onClick: remove,
|
|
135
|
-
children: /*#__PURE__*/_jsx(Icons, {
|
|
136
|
-
name: "X"
|
|
137
|
-
})
|
|
138
|
-
})]
|
|
139
|
-
}), /*#__PURE__*/_jsx("p", {
|
|
140
|
-
className: "p-1",
|
|
141
|
-
children: message
|
|
142
|
-
}), actions.length > 0 && /*#__PURE__*/_jsx("div", {
|
|
143
|
-
className: "flex justify-end gap-2 mb-2",
|
|
144
|
-
children: actions.map(function (a, idx) {
|
|
145
|
-
return /*#__PURE__*/_jsx("button", {
|
|
146
|
-
onClick: function onClick() {
|
|
147
|
-
var _a$callback;
|
|
148
|
-
return (_a$callback = a.callback) === null || _a$callback === void 0 ? void 0 : _a$callback.call(a, {
|
|
149
|
-
id: id
|
|
150
|
-
});
|
|
151
|
-
},
|
|
152
|
-
className: "px-3 py-1 text-sm rounded ".concat(idx === 1 ? "".concat(colors[type], " bg-transparent") : colors[type], " ").concat(theme === "dark" ? "text-gray-50" : "text-gray-900", " ring cursor-pointer"),
|
|
153
|
-
children: a.text
|
|
154
|
-
}, idx);
|
|
155
|
-
})
|
|
156
|
-
}), progress && duration !== 0 && /*#__PURE__*/_jsx("div", {
|
|
157
|
-
className: "h-1 w-full bg-gray-200 rounded overflow-hidden absolute bottom-0 left-0",
|
|
158
|
-
children: /*#__PURE__*/_jsx("div", {
|
|
159
|
-
className: "h-1 ".concat(colors[type], " transition-all"),
|
|
160
|
-
style: {
|
|
161
|
-
width: "".concat(progressWidth, "%")
|
|
162
|
-
}
|
|
92
|
+
var animation = position.startsWith("top") ? "slideDown 0.3s ease" : position.startsWith("bottom") ? "slideUp 0.3s ease" : "fadeIn 0.3s ease";
|
|
93
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
94
|
+
style: {
|
|
95
|
+
animation: animation
|
|
96
|
+
},
|
|
97
|
+
className: "toast ".concat(colors[type], " shadow-lg rounded-lg p-2 flex flex-col relative"),
|
|
98
|
+
onMouseEnter: pauseOnHover ? pauseTimer : undefined,
|
|
99
|
+
onMouseLeave: pauseOnHover ? resumeTimer : undefined,
|
|
100
|
+
children: [/*#__PURE__*/_jsxs("div", {
|
|
101
|
+
className: "toast-header flex justify-between items-center",
|
|
102
|
+
children: [/*#__PURE__*/_jsxs("span", {
|
|
103
|
+
className: "flex items-center gap-2 font-medium",
|
|
104
|
+
children: [/*#__PURE__*/_jsx(Icons, {
|
|
105
|
+
name: type
|
|
106
|
+
}), " ", type.toUpperCase()]
|
|
107
|
+
}), closable && /*#__PURE__*/_jsx("button", {
|
|
108
|
+
className: "hover:text-red-600",
|
|
109
|
+
onClick: remove,
|
|
110
|
+
children: /*#__PURE__*/_jsx(Icons, {
|
|
111
|
+
name: "X"
|
|
163
112
|
})
|
|
164
113
|
})]
|
|
114
|
+
}), /*#__PURE__*/_jsx("p", {
|
|
115
|
+
className: "toast-message",
|
|
116
|
+
children: message
|
|
117
|
+
}), actions.length > 0 && /*#__PURE__*/_jsx("div", {
|
|
118
|
+
className: "flex justify-end gap-2 mt-1",
|
|
119
|
+
children: actions.map(function (a, i) {
|
|
120
|
+
return /*#__PURE__*/_jsx("button", {
|
|
121
|
+
onClick: function onClick() {
|
|
122
|
+
var _a$callback;
|
|
123
|
+
return (_a$callback = a.callback) === null || _a$callback === void 0 ? void 0 : _a$callback.call(a, {
|
|
124
|
+
id: id
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
className: "px-3 py-1 rounded ".concat(colors[type], " cursor-pointer"),
|
|
128
|
+
children: a.text
|
|
129
|
+
}, i);
|
|
130
|
+
})
|
|
131
|
+
}), progress && duration !== 0 && /*#__PURE__*/_jsx("div", {
|
|
132
|
+
className: "h-1 w-full bg-gray-300 rounded absolute bottom-0 left-0 overflow-hidden",
|
|
133
|
+
children: /*#__PURE__*/_jsx("div", {
|
|
134
|
+
className: "h-1 ".concat(colors[type], " transition-all"),
|
|
135
|
+
style: {
|
|
136
|
+
width: "".concat(progressWidth, "%")
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
}), /*#__PURE__*/_jsx("style", {
|
|
140
|
+
children: "\n @keyframes slideDown { from { transform: translateY(-100%); opacity: 0; } to { transform: translateY(0); opacity:1; } }\n @keyframes slideUp { from { transform: translateY(100%); opacity:0; } to { transform: translateY(0); opacity:1; } }\n @keyframes fadeIn { from { opacity: 0; } to { opacity:1; } }\n "
|
|
165
141
|
})]
|
|
166
142
|
});
|
|
167
143
|
};
|
package/dist/ToastContext.js
CHANGED
|
@@ -21,6 +21,8 @@ var ToastContext = /*#__PURE__*/createContext();
|
|
|
21
21
|
export var useToast = function useToast() {
|
|
22
22
|
return useContext(ToastContext);
|
|
23
23
|
};
|
|
24
|
+
var id = 0; // unique toast id
|
|
25
|
+
|
|
24
26
|
export var ToastProvider = function ToastProvider(_ref) {
|
|
25
27
|
var children = _ref.children,
|
|
26
28
|
_ref$position = _ref.position,
|
|
@@ -29,29 +31,30 @@ export var ToastProvider = function ToastProvider(_ref) {
|
|
|
29
31
|
_useState2 = _slicedToArray(_useState, 2),
|
|
30
32
|
toasts = _useState2[0],
|
|
31
33
|
setToasts = _useState2[1];
|
|
32
|
-
|
|
33
|
-
// Generate unique ID safely
|
|
34
|
-
var generateId = function generateId() {
|
|
35
|
-
return Date.now() + Math.random();
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// Position classes
|
|
39
34
|
var getPositionClasses = function getPositionClasses(position) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
switch (position) {
|
|
36
|
+
case "top-right":
|
|
37
|
+
return "top-4 right-4 flex-col";
|
|
38
|
+
case "top-left":
|
|
39
|
+
return "top-4 left-4 flex-col";
|
|
40
|
+
case "bottom-right":
|
|
41
|
+
return "bottom-4 right-4 flex-col-reverse";
|
|
42
|
+
case "bottom-left":
|
|
43
|
+
return "bottom-4 left-4 flex-col-reverse";
|
|
44
|
+
case "center":
|
|
45
|
+
return "top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 flex-col";
|
|
46
|
+
case "center-top":
|
|
47
|
+
return "top-4 left-1/2 -translate-x-1/2 flex-col";
|
|
48
|
+
case "center-bottom":
|
|
49
|
+
return "bottom-4 left-1/2 -translate-x-1/2 flex-col-reverse";
|
|
50
|
+
default:
|
|
51
|
+
return "top-4 right-4 flex-col";
|
|
52
|
+
}
|
|
49
53
|
};
|
|
50
|
-
|
|
51
|
-
// Show toast
|
|
52
54
|
var showToast = useCallback(function (toast) {
|
|
55
|
+
id++;
|
|
53
56
|
var newToast = _objectSpread({
|
|
54
|
-
id:
|
|
57
|
+
id: id
|
|
55
58
|
}, toast);
|
|
56
59
|
setToasts(function (prev) {
|
|
57
60
|
return [].concat(_toConsumableArray(prev), [newToast]);
|
|
@@ -62,8 +65,6 @@ export var ToastProvider = function ToastProvider(_ref) {
|
|
|
62
65
|
}, toast.duration || 5000);
|
|
63
66
|
}
|
|
64
67
|
}, []);
|
|
65
|
-
|
|
66
|
-
// Remove toast
|
|
67
68
|
var removeToast = useCallback(function (id) {
|
|
68
69
|
setToasts(function (prev) {
|
|
69
70
|
return prev.filter(function (t) {
|
|
@@ -77,7 +78,7 @@ export var ToastProvider = function ToastProvider(_ref) {
|
|
|
77
78
|
removeToast: removeToast
|
|
78
79
|
},
|
|
79
80
|
children: [children, /*#__PURE__*/_jsx("div", {
|
|
80
|
-
className: getPositionClasses(position),
|
|
81
|
+
className: "fixed z-50 flex gap-3 ".concat(getPositionClasses(position)),
|
|
81
82
|
children: toasts.map(function (toast) {
|
|
82
83
|
return /*#__PURE__*/_jsx(Toast, _objectSpread(_objectSpread({}, toast), {}, {
|
|
83
84
|
position: position,
|