Skip to content

Commit

Permalink
Merge pull request #124 from paulgv/keep-on-hover
Browse files Browse the repository at this point in the history
Add keepOnHover option
  • Loading branch information
shakee93 committed May 17, 2019
2 parents 8a631e8 + 275961c commit 2905c01
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 @@ -466,7 +469,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 @@ -492,6 +496,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 2905c01

Please sign in to comment.