forked from area17/twill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotification.vue
92 lines (88 loc) · 2.2 KB
/
Notification.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
<transition name="move_down_notif">
<div v-if="show" :class="notifClasses" role="alert" aria-live="polite" aria-atomic="true">
<div class="notif__inner">
<button v-if="!important" type="button" class="notif__close" data-dismiss="alert" aria-label="alertClose" @click.stop.prevent="closeNotif" >
<span v-svg symbol="close_modal"></span>
</button>
<span v-html="message"></span>
</div>
</div>
</transition>
</template>
<script>
import { NOTIFICATION } from '@/store/mutations'
export default {
name: 'A17Notification',
props: {
variant: {
type: String,
default: 'success'
},
duration: {
type: Number,
default: 3000
},
important: {
type: Boolean,
default: true
},
autoHide: {
type: Boolean,
default: true
}
},
data: function () {
return {
closed: false,
timer: null,
css: 'notif'
}
},
computed: {
message: function () {
return this.$store.getters.notifByVariant(this.variant)
},
variantClass: function () {
return `notif--${this.variant}`
},
notifClasses: function () {
return this.css && Array.isArray(this.css)
? [...this.css, this.variantClass]
: ['notif', this.variantClass]
},
show: function () {
return !this.closed && !!this.message
}
},
methods: {
closeNotif: function () {
this.closed = true
this.clearNotification()
if (this.timer) {
clearTimeout(this.timer)
this.timer = null
}
},
clearNotification: function () {
this.$store.commit(NOTIFICATION.CLEAR_NOTIF, this.variant)
},
autoClose: function () {
if (this.timer === null) {
this.timer = setTimeout(() => {
this.closeNotif()
}, this.duration)
}
}
},
watch: {
message: function () {
if (this.message) {
// if we have a message, let's show it
this.closed = false
if (this.autoHide) this.autoClose()
}
}
}
}
</script>