d9-toast 1.4.34 → 1.4.35
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 +20 -20
- package/dist/Toast.js +55 -37
- package/dist/ToastContext.js +1 -6
- package/dist/toast.css +5 -5
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ A **lightweight, fully typed, customizable toast notification library** for Reac
|
|
|
28
28
|
|
|
29
29
|
## 📺 Demo
|
|
30
30
|
|
|
31
|
-
[](https://
|
|
31
|
+
[](https://psathul073.github.io/d9-toast-docs/docs/examples/basic)
|
|
32
32
|
|
|
33
33
|
---
|
|
34
34
|
|
|
@@ -51,8 +51,8 @@ yarn add d9-toast
|
|
|
51
51
|
### 1. Wrap your app with `ToastProvider`
|
|
52
52
|
|
|
53
53
|
```jsx
|
|
54
|
-
import "d9-toast/dist/toast.css";
|
|
55
54
|
import { ToastProvider } from "d9-toast";
|
|
55
|
+
import "d9-toast/toast.css";
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
export default function Root() {
|
|
@@ -171,24 +171,24 @@ audio?: {
|
|
|
171
171
|
|
|
172
172
|
### Example
|
|
173
173
|
|
|
174
|
-
```jsx
|
|
175
|
-
const { sounds, showToast } = useToast();
|
|
176
|
-
|
|
177
|
-
showToast({
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
});
|
|
187
|
-
```
|
|
174
|
+
```jsx
|
|
175
|
+
const { sounds, showToast } = useToast();
|
|
176
|
+
|
|
177
|
+
showToast({
|
|
178
|
+
message: "Message sent",
|
|
179
|
+
type: "success",
|
|
180
|
+
audio: {
|
|
181
|
+
enabled: true,
|
|
182
|
+
volume: 0.8,
|
|
183
|
+
cooldown: 500,
|
|
184
|
+
audioFile: sounds.success,
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
188
|
|
|
189
|
-
✔ Prevents sound spam
|
|
190
|
-
✔ Per-toast control
|
|
191
|
-
✔ Custom audio file supported
|
|
189
|
+
✔ Prevents sound spam
|
|
190
|
+
✔ Per-toast control
|
|
191
|
+
✔ Custom audio file supported
|
|
192
192
|
|
|
193
193
|
---
|
|
194
194
|
|
|
@@ -198,7 +198,7 @@ showToast({
|
|
|
198
198
|
Import the default styles once in your app:
|
|
199
199
|
|
|
200
200
|
```js
|
|
201
|
-
import "d9-toast/
|
|
201
|
+
import "d9-toast/toast.css";
|
|
202
202
|
```
|
|
203
203
|
|
|
204
204
|
### Tailwind / Custom Styling
|
package/dist/Toast.js
CHANGED
|
@@ -21,7 +21,7 @@ var Toast = function Toast(_ref) {
|
|
|
21
21
|
_ref$className = _ref.className,
|
|
22
22
|
className = _ref$className === void 0 ? "" : _ref$className,
|
|
23
23
|
_ref$duration = _ref.duration,
|
|
24
|
-
duration = _ref$duration === void 0 ?
|
|
24
|
+
duration = _ref$duration === void 0 ? 4000 : _ref$duration,
|
|
25
25
|
_ref$actions = _ref.actions,
|
|
26
26
|
actions = _ref$actions === void 0 ? [] : _ref$actions,
|
|
27
27
|
remove = _ref.remove,
|
|
@@ -37,21 +37,24 @@ var Toast = function Toast(_ref) {
|
|
|
37
37
|
pauseOnHover = _ref$pauseOnHover === void 0 ? true : _ref$pauseOnHover,
|
|
38
38
|
_ref$pauseOnFocusLoss = _ref.pauseOnFocusLoss,
|
|
39
39
|
pauseOnFocusLoss = _ref$pauseOnFocusLoss === void 0 ? true : _ref$pauseOnFocusLoss;
|
|
40
|
-
var intervalRef = useRef(null); // stores the interval ID
|
|
41
|
-
var start = useRef(Date.now()); // stores the moment timer started
|
|
42
|
-
var remaining = useRef(duration); // how much time is left
|
|
43
40
|
var _useState = useState(100),
|
|
44
41
|
_useState2 = _slicedToArray(_useState, 2),
|
|
45
42
|
progressWidth = _useState2[0],
|
|
46
|
-
setProgressWidth = _useState2[1];
|
|
43
|
+
setProgressWidth = _useState2[1];
|
|
47
44
|
var _useState3 = useState(false),
|
|
48
45
|
_useState4 = _slicedToArray(_useState3, 2),
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
exiting = _useState4[0],
|
|
47
|
+
setExiting = _useState4[1];
|
|
51
48
|
var _useState5 = useState(false),
|
|
52
49
|
_useState6 = _slicedToArray(_useState5, 2),
|
|
53
|
-
|
|
54
|
-
|
|
50
|
+
isPausedState = _useState6[0],
|
|
51
|
+
setIsPausedState = _useState6[1]; // Only for UI/State checks..
|
|
52
|
+
var intervalRef = useRef(null);
|
|
53
|
+
var timeoutRef = useRef(null);
|
|
54
|
+
var initialDuration = useRef(duration);
|
|
55
|
+
var start = useRef(Date.now());
|
|
56
|
+
var remaining = useRef(duration);
|
|
57
|
+
var isPausedRef = useRef(false); // Use for internal logic to avoid loops..
|
|
55
58
|
|
|
56
59
|
// Styles [Animation]
|
|
57
60
|
var _useMemo = useMemo(function () {
|
|
@@ -69,49 +72,63 @@ var Toast = function Toast(_ref) {
|
|
|
69
72
|
exitAnim = _useMemo.exitAnim;
|
|
70
73
|
|
|
71
74
|
// --- Helpers Fun ---
|
|
75
|
+
|
|
76
|
+
// for exit
|
|
77
|
+
var triggerExit = useCallback(function () {
|
|
78
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
79
|
+
setExiting(true);
|
|
80
|
+
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
81
|
+
timeoutRef.current = setTimeout(function () {
|
|
82
|
+
return remove();
|
|
83
|
+
}, 250); // Set and match exit animation duration.
|
|
84
|
+
}, [remove]);
|
|
85
|
+
|
|
72
86
|
// for start
|
|
73
87
|
var startTimer = useCallback(function () {
|
|
74
|
-
if (!autoClose) return;
|
|
88
|
+
if (!autoClose || initialDuration.current === 0) return;
|
|
89
|
+
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
90
|
+
|
|
91
|
+
// Update the start point every time we (re)start the interval.
|
|
92
|
+
start.current = Date.now();
|
|
75
93
|
intervalRef.current = setInterval(function () {
|
|
94
|
+
// If we are paused, we don't do anything, but the interval stays alive.
|
|
95
|
+
if (isPausedRef.current) return;
|
|
96
|
+
|
|
76
97
|
// time to passed...
|
|
77
98
|
var elapsed = Date.now() - start.current;
|
|
78
99
|
// time to left...
|
|
79
100
|
var timeToLeft = remaining.current - elapsed;
|
|
80
101
|
// set shrink bar...
|
|
81
|
-
|
|
102
|
+
var percent = timeToLeft / initialDuration.current * 100;
|
|
103
|
+
setProgressWidth(Math.max(0, percent));
|
|
82
104
|
if (timeToLeft <= 0) {
|
|
83
105
|
// stop timer...
|
|
84
106
|
clearInterval(intervalRef.current);
|
|
85
107
|
// remove toast.
|
|
86
108
|
triggerExit();
|
|
87
109
|
}
|
|
88
|
-
},
|
|
89
|
-
}, [autoClose,
|
|
110
|
+
}, 50); // Add 50ms is smoother for progress bars..
|
|
111
|
+
}, [autoClose, triggerExit]);
|
|
90
112
|
|
|
91
113
|
// for pause
|
|
92
114
|
var pauseTimer = useCallback(function () {
|
|
93
|
-
if (
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
115
|
+
if (!autoClose || isPausedRef.current) return;
|
|
116
|
+
|
|
117
|
+
// Calculate exactly what was left at the moment of pause..
|
|
118
|
+
var elapsed = Date.now() - start.current;
|
|
119
|
+
remaining.current = remaining.current - elapsed;
|
|
120
|
+
isPausedRef.current = true;
|
|
121
|
+
setIsPausedState(true);
|
|
122
|
+
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
123
|
+
}, [autoClose]);
|
|
98
124
|
|
|
99
125
|
// for resume
|
|
100
126
|
var resumeTimer = useCallback(function () {
|
|
101
|
-
if (!
|
|
102
|
-
|
|
103
|
-
|
|
127
|
+
if (!autoClose || !isPausedRef.current) return;
|
|
128
|
+
isPausedRef.current = false;
|
|
129
|
+
setIsPausedState(false);
|
|
104
130
|
startTimer();
|
|
105
|
-
}, [
|
|
106
|
-
|
|
107
|
-
// for exit
|
|
108
|
-
var triggerExit = useCallback(function () {
|
|
109
|
-
setExiting(true);
|
|
110
|
-
clearInterval(intervalRef.current);
|
|
111
|
-
setTimeout(function () {
|
|
112
|
-
return remove();
|
|
113
|
-
}, 250); // Set and match exit animation duration.
|
|
114
|
-
}, [remove]);
|
|
131
|
+
}, [autoClose, startTimer]);
|
|
115
132
|
|
|
116
133
|
// Toast actions...
|
|
117
134
|
var actionButtons = useMemo(function () {
|
|
@@ -138,9 +155,8 @@ var Toast = function Toast(_ref) {
|
|
|
138
155
|
|
|
139
156
|
// Start auto-close timer.
|
|
140
157
|
useEffect(function () {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
}
|
|
158
|
+
startTimer();
|
|
159
|
+
|
|
144
160
|
// pause/resume when window focus changes.
|
|
145
161
|
var handleBlur = function handleBlur() {
|
|
146
162
|
return pauseOnFocusLoss && pauseTimer();
|
|
@@ -153,13 +169,14 @@ var Toast = function Toast(_ref) {
|
|
|
153
169
|
window.addEventListener("focus", handleFocus);
|
|
154
170
|
}
|
|
155
171
|
return function () {
|
|
156
|
-
clearInterval(intervalRef.current);
|
|
172
|
+
if (intervalRef.current) clearInterval(intervalRef.current);
|
|
173
|
+
if (timeoutRef.current) clearTimeout(timeoutRef.current);
|
|
157
174
|
if (pauseOnFocusLoss) {
|
|
158
175
|
window.removeEventListener("blur", handleBlur);
|
|
159
176
|
window.removeEventListener("focus", handleFocus);
|
|
160
177
|
}
|
|
161
178
|
};
|
|
162
|
-
}, [
|
|
179
|
+
}, []);
|
|
163
180
|
return /*#__PURE__*/_jsx(_Fragment, {
|
|
164
181
|
children: /*#__PURE__*/_jsxs("div", {
|
|
165
182
|
style: {
|
|
@@ -222,7 +239,8 @@ var Toast = function Toast(_ref) {
|
|
|
222
239
|
children: /*#__PURE__*/_jsx("div", {
|
|
223
240
|
className: "toast-progress ".concat(type),
|
|
224
241
|
style: {
|
|
225
|
-
width: "".concat(progressWidth, "%")
|
|
242
|
+
width: "".concat(progressWidth, "%"),
|
|
243
|
+
transition: isPausedState ? "none" : "width 50ms linear"
|
|
226
244
|
}
|
|
227
245
|
})
|
|
228
246
|
})]
|
package/dist/ToastContext.js
CHANGED
|
@@ -98,12 +98,7 @@ export var ToastProvider = function ToastProvider(_ref) {
|
|
|
98
98
|
volume: audio.volume
|
|
99
99
|
});
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
if (shouldAutoClose) {
|
|
103
|
-
setTimeout(function () {
|
|
104
|
-
return removeToast(newToast.id);
|
|
105
|
-
}, toast.duration || 5000);
|
|
106
|
-
}
|
|
101
|
+
;
|
|
107
102
|
return newToast === null || newToast === void 0 ? void 0 : newToast.id;
|
|
108
103
|
}, [playAudio]);
|
|
109
104
|
|
package/dist/toast.css
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
--loading-color: #8736ca;
|
|
23
23
|
--loading-bg: rgba(124, 58, 237, 0.1);
|
|
24
24
|
|
|
25
|
-
--border-color:
|
|
25
|
+
--border-color: rgb(17, 24, 39);
|
|
26
26
|
|
|
27
27
|
--line-height-base: 1.5;
|
|
28
28
|
--font-size-base: 14px;
|
|
@@ -143,14 +143,14 @@
|
|
|
143
143
|
--loading-color: #6900ca;
|
|
144
144
|
--loading-bg: rgba(106, 31, 176, 0.1);
|
|
145
145
|
|
|
146
|
-
--border-color:
|
|
146
|
+
--border-color: rgb(229, 231, 235);
|
|
147
147
|
|
|
148
148
|
/* background-color: #ffffff;
|
|
149
149
|
color: #1e2939; */
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
.dark {
|
|
153
|
-
|
|
153
|
+
--primary-bg: #060606;
|
|
154
154
|
--primary-color: #ffffff;
|
|
155
155
|
|
|
156
156
|
--success-color: #00df5f;
|
|
@@ -168,7 +168,7 @@
|
|
|
168
168
|
--loading-color: #c586ff;
|
|
169
169
|
--loading-bg: rgba(106, 31, 176, 0.1);
|
|
170
170
|
|
|
171
|
-
--border-color:
|
|
171
|
+
--border-color: rgb(17, 24, 39);
|
|
172
172
|
|
|
173
173
|
/* background-color: #1e2939;
|
|
174
174
|
color: #ffffff; */
|
|
@@ -321,7 +321,7 @@
|
|
|
321
321
|
|
|
322
322
|
.progress-container {
|
|
323
323
|
position: absolute;
|
|
324
|
-
bottom:
|
|
324
|
+
bottom: -1px;
|
|
325
325
|
left: 0;
|
|
326
326
|
width: 100%;
|
|
327
327
|
height: 0.25rem;
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "d9-toast",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.35",
|
|
4
4
|
"description": "Customizable toast notifications for React",
|
|
5
|
-
"homepage": "https://
|
|
5
|
+
"homepage": "https://psathul073.github.io/d9-toast-docs/",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/psathul073/d9-toast.git"
|