Skip to content

Commit a325dcc

Browse files
committed
Stop the timeout if the window is not focused (apvarun#112)
1 parent e53fb50 commit a325dcc

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Toastify({
8383
gravity: "top", // `top` or `bottom`
8484
position: "left", // `left`, `center` or `right`
8585
stopOnFocus: true, // Prevents dismissing of toast on hover
86+
stopOnWindowBlur: true, // Prevents dismissing of toast if the window is not focused
8687
style: {
8788
background: "linear-gradient(to right, #00b09b, #96c93d)",
8889
},
@@ -148,6 +149,7 @@ If `gravity` is equals to `bottom`, it will be pushed from bottom.
148149
| avatar | URL string | Image/icon to be shown before text | |
149150
| className | string | Ability to provide custom class name for further customization | |
150151
| stopOnFocus | boolean | To stop timer when hovered over the toast (Only if duration is set) | true |
152+
| stopOnWindowBlur | boolean | To stop timer if window is not focused (Only if duration is set) | true |
151153
| callback | Function | Invoked when the toast is dismissed | |
152154
| onClick | Function | Invoked when the toast is clicked | |
153155
| offset | Object | Ability to add some offset to axis | |

src/toastify-es.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @property {url} avatar - Image/icon to be shown before text
2323
* @property {string} className - Ability to provide custom class name for further customization
2424
* @property {boolean} stopOnFocus - To stop timer when hovered over the toast (Only if duration is set)
25+
* @property {boolean} stopOnWindowBlur - To stop timer if window is not focused (Only if duration is set)
2526
* @property {Function} callback - Invoked when the toast is dismissed
2627
* @property {Function} onClick - Invoked when the toast is clicked
2728
* @property {Object} offset - Ability to add some offset to axis
@@ -50,6 +51,7 @@ class Toastify {
5051
avatar: "",
5152
className: "",
5253
stopOnFocus: true,
54+
stopOnWindowBlur: true,
5355
onClick: function() {},
5456
offset: { x: 0, y: 0 },
5557
escapeMarkup: true,
@@ -158,6 +160,7 @@ class Toastify {
158160
* @param {url} [options.avatar] - Image/icon to be shown before text
159161
* @param {string} [options.className] - Ability to provide custom class name for further customization
160162
* @param {boolean} [options.stopOnFocus] - To stop timer when hovered over the toast (Only if duration is set)
163+
* @param {boolean} [options.stopOnWindowBlur] - To stop timer if window is not focused (Only if duration is set)
161164
* @param {Function} [options.callback] - Invoked when the toast is dismissed
162165
* @param {Function} [options.onClick] - Invoked when the toast is clicked
163166
* @param {Object} [options.offset] - Ability to add some offset to axis
@@ -300,6 +303,28 @@ class Toastify {
300303
)
301304
}
302305

306+
// Clear timeout while window is not focused
307+
if (this.options.stopOnWindowBlur && this.options.duration > 0) {
308+
document.addEventListener(
309+
"visibilitychange",
310+
(event) => {
311+
if (document.visibilityState === 'visible') {
312+
// add back the timeout
313+
divElement.timeOutValue = window.setTimeout(
314+
() => {
315+
// Remove the toast from DOM
316+
this._removeElement(divElement);
317+
},
318+
this.options.duration
319+
)
320+
} else {
321+
// stop countdown
322+
window.clearTimeout(divElement.timeOutValue);
323+
}
324+
}
325+
)
326+
}
327+
303328
// Adding an on-click destination path
304329
if (typeof this.options.destination !== "undefined") {
305330
divElement.addEventListener(

src/toastify.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
avatar: "",
4040
className: "",
4141
stopOnFocus: true,
42+
stopOnWindowBlur: true,
4243
onClick: function () {
4344
},
4445
offset: {x: 0, y: 0},
@@ -81,6 +82,7 @@
8182
this.options.avatar = options.avatar || Toastify.defaults.avatar; // img element src - url or a path
8283
this.options.className = options.className || Toastify.defaults.className; // additional class names for the toast
8384
this.options.stopOnFocus = options.stopOnFocus === undefined ? Toastify.defaults.stopOnFocus : options.stopOnFocus; // stop timeout on focus
85+
this.options.stopOnWindowBlur = options.stopOnWindowBlur === undefined ? Toastify.defaults.stopOnWindowBlur : options.stopOnWindowBlur;
8486
this.options.onClick = options.onClick || Toastify.defaults.onClick; // Callback after click
8587
this.options.offset = options.offset || Toastify.defaults.offset; // toast offset
8688
this.options.escapeMarkup = options.escapeMarkup !== undefined ? options.escapeMarkup : Toastify.defaults.escapeMarkup;
@@ -222,6 +224,29 @@
222224
)
223225
}
224226

227+
// Clear timeout while window is not focused
228+
if (this.options.stopOnWindowBlur && this.options.duration > 0) {
229+
var self = this;
230+
document.addEventListener(
231+
"visibilitychange",
232+
function() {
233+
if (document.visibilityState === 'visible') {
234+
// add back the timeout
235+
divElement.timeOutValue = window.setTimeout(
236+
function() {
237+
// Remove the toast from DOM
238+
self.removeElement(divElement);
239+
},
240+
self.options.duration
241+
)
242+
} else {
243+
// stop countdown
244+
window.clearTimeout(divElement.timeOutValue);
245+
}
246+
}
247+
)
248+
}
249+
225250
// Adding an on-click destination path
226251
if (typeof this.options.destination !== "undefined") {
227252
divElement.addEventListener(

0 commit comments

Comments
 (0)