notifications: missing modules

This commit is contained in:
jean-emmanuel 2019-01-04 12:02:55 +01:00
parent d53a977f93
commit d6aea12c52
2 changed files with 136 additions and 0 deletions

View file

@ -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`
<div class="toast ${data.class || ''}">
<i class="fa fa-fw fa-${data.icon || 'bell'}"></i>
<div class="content">
${data.message}
</div>
</div>
`
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()

View file

@ -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)}
}