Skip to content

Commit

Permalink
fix: stop and resume timer progress bar (#1806)
Browse files Browse the repository at this point in the history
  • Loading branch information
limonte committed Nov 11, 2019
1 parent 4c9ffe1 commit a8cf8c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/instanceMethods/_main.js
Expand Up @@ -133,8 +133,7 @@ const setupTimer = (globalState, innerParams, dismissWith) => {
if (innerParams.timerProgressBar) {
dom.show(timerProgressBar)
setTimeout(() => {
timerProgressBar.style.transition = `width ${innerParams.timer / 1000}s`
timerProgressBar.style.width = '0%'
dom.animateTimerProgressBar(innerParams.timer)
})
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/staticMethods/timer.js
@@ -1,3 +1,4 @@
import { animateTimerProgressBar, stopTimerProgressBar } from '../utils/dom/domUtils.js'
import globalState from '../globalState.js'

/**
Expand All @@ -13,15 +14,22 @@ export const getTimerLeft = () => {
* If `timer` parameter isn't set, returns undefined.
*/
export const stopTimer = () => {
return globalState.timeout && globalState.timeout.stop()
if (globalState.timeout) {
stopTimerProgressBar()
return globalState.timeout.stop()
}
}

/**
* Resume timer. Returns number of milliseconds of timer remained.
* If `timer` parameter isn't set, returns undefined.
*/
export const resumeTimer = () => {
return globalState.timeout && globalState.timeout.start()
if (globalState.timeout) {
const remaining = globalState.timeout.start()
animateTimerProgressBar(remaining)
return remaining
}
}

/**
Expand All @@ -30,7 +38,7 @@ export const resumeTimer = () => {
*/
export const toggleTimer = () => {
const timer = globalState.timeout
return timer && (timer.running ? timer.stop() : timer.start())
return timer && (timer.running ? stopTimer() : resumeTimer())
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/utils/dom/domUtils.js
@@ -1,3 +1,4 @@
import { getTimerProgressBar } from './getters.js'
import { swalClasses, iconTypes } from '../classes.js'
import { toArray, objectValues, warn } from '../utils.js'

Expand Down Expand Up @@ -139,3 +140,22 @@ export const contains = (haystack, needle) => {
return haystack.contains(needle)
}
}

export const animateTimerProgressBar = (timer) => {
const timerProgressBar = getTimerProgressBar()
if (isVisible(timerProgressBar)) {
timerProgressBar.style.transition = `width ${timer / 1000}s linear`
timerProgressBar.style.width = '0%'
}
}

export const stopTimerProgressBar = () => {
const timerProgressBar = getTimerProgressBar()
const timerProgressBarWidth = parseInt(window.getComputedStyle(timerProgressBar).width)
timerProgressBar.style.removeProperty('transition')
timerProgressBar.style.width = '100%'
const timerProgressBarFullWidth = parseInt(window.getComputedStyle(timerProgressBar).width)
const timerProgressBarPercent = parseInt(timerProgressBarWidth / timerProgressBarFullWidth * 100)
timerProgressBar.style.removeProperty('transition')
timerProgressBar.style.width = `${timerProgressBarPercent}%`
}

0 comments on commit a8cf8c5

Please sign in to comment.