Skip to content

Commit

Permalink
Add keepOnHover option
Browse files Browse the repository at this point in the history
When enabled, hovering on a visible toast prevents it from being closed by
its timer. When the mouse leaves the element, the timer is resumed.
  • Loading branch information
paulgv committed Apr 22, 2019
1 parent d9ac39c commit 275961c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -181,6 +181,7 @@ below are the options you can pass to create a toast
-----|-----|-----|-----
position|String|'top-right'|Position of the toast container <br> **['top-right', 'top-center', 'top-left', 'bottom-right', 'bottom-center', 'bottom-left']**
duration|Number|null|Display time of the toast in millisecond
keepOnHover|Boolean|false|When mouse is over a toast's element, the corresponding `duration` timer is paused until the cursor leaves the element
action|Object, Array|null|Add single or multiple actions to toast [explained here](#actions--fire)
fullWidth|Boolean|false|Enable Full Width
fitToScreen|Boolean|false|Fits to Screen on Full Width
Expand Down
18 changes: 17 additions & 1 deletion src/js/show.js
Expand Up @@ -43,6 +43,9 @@ const parseOptions = function (options) {
// toast duration
options.duration = options.duration || null;

// keep toast open on mouse over
options.keepOnHover = options.keepOnHover || false;

// normal type will allow the basic color
options.theme = options.theme || "toasted-primary";

Expand Down Expand Up @@ -474,7 +477,8 @@ export default function (instance, message, options) {
let timeLeft = options.duration;
let counterInterval;
if (timeLeft !== null) {
counterInterval = setInterval(function () {

const createInterval = () => setInterval(function () {
if (newToast.parentNode === null)
window.clearInterval(counterInterval);

Expand All @@ -500,6 +504,18 @@ export default function (instance, message, options) {
window.clearInterval(counterInterval);
}
}, 20);

counterInterval = createInterval();

// Toggle interval on hover
if (options.keepOnHover) {
newToast.addEventListener('mouseover', () => {
window.clearInterval(counterInterval);
});
newToast.addEventListener('mouseout', () => {
counterInterval = createInterval();
});
}
}

return toastObject(newToast, _instance);
Expand Down

0 comments on commit 275961c

Please sign in to comment.