diff --git a/src/client/app/ui/notifications.js b/src/client/app/ui/notifications.js new file mode 100644 index 00000000..ac165ab8 --- /dev/null +++ b/src/client/app/ui/notifications.js @@ -0,0 +1,83 @@ +var html = require('nanohtml'), + keyboardJS = require('keyboardjs') + +var DURATION = 3000 + +class Notifications { + + constructor() { + + this.container = DOM.get('#notifications')[0] + this.toasts = [] + + this.loop = null + + keyboardJS.bind('k', ()=>{ + this.add({ + message: 'bla bla bla bla' + }) + }) + + + } + + add(data) { + + var toast = html` +
+ +
+ ${data.message} +
+
+ ` + toast.date = Date.now() + this.container.appendChild(toast) + toast.style.height = toast.offsetHeight + 'px' + this.toasts.push(toast) + + this.startLoop() + + } + + remove(toast) { + + this.toasts.splice(this.toasts.indexOf(toast), 1) + toast.classList.add('remove') + toast.classList.add('remove') + setTimeout(()=>{ + this.container.removeChild(toast) + }, 200) + + if (this.toasts.length === 0) this.stopLoop() + + } + + startLoop() { + + if (this.loop) return + + this.loop = setInterval(()=>{ + for (var i = this.toasts.length - 1; i > -1; i--) { + if (Date.now() - this.toasts[i].date > DURATION) { + this.remove(this.toasts[i]) + } + } + + },100) + + } + + stopLoop() { + + if (!this.loop) return + + clearInterval(this.loop) + this.loop = null + + } + + +} + +module.exports = new Notifications() diff --git a/src/scss/notifications.scss b/src/scss/notifications.scss new file mode 100644 index 00000000..f9fd77e1 --- /dev/null +++ b/src/scss/notifications.scss @@ -0,0 +1,53 @@ +#notifications { + position: fixed; + bottom:0; + right:0; + display: flex; + flex-direction: column-reverse; + z-index: 9999999; + padding:10rem; + width:300rem; + .toast { + animation: toast_fade-in 0.2s forwards; + transform-origin: bottom; + background: var(--color-notification); + transition: height 0.2s; + overflow: hidden; + margin-top: 10rem; + display: flex; + flex-direction: row; + align-items: center; + &.remove { + animation: toast_fade-out 0.2s forwards; + height: 0!important; + } + i { + margin-left: 10rem; + font-size: 14rem; + min-width: 30rem; + color: var(--color-accent); + } + &.error { + i { + color: red; + } + } + .content { + padding: 10rem; + white-space: normal; + text-transform: none; + line-height: normal; + } + } + +} + +@keyframes toast_fade-in { + from {opacity: 0; transform: rotateX(90deg)} + to {opacity: 1; transform: rotateX(0)} +} + +@keyframes toast_fade-out { + from {opacity: 1; transform: rotateX(0)} + to {opacity: 0; transform: rotateX(90deg)} +}