-
Notifications
You must be signed in to change notification settings - Fork 9
/
u-toast.vue
113 lines (100 loc) · 2.46 KB
/
u-toast.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<template>
<div v-if="showToast" class="u-toast">
<div class="content">
<span :class="['icon', type]">!</span> <span :class="['text', type]">{{ text }}</span>
</div>
</div>
</template>
<script>
import Vue from 'vue'
const Toast = {
name: 'u-toast',
props: {
text: { type: String, default: '' },
type: {
type: String,
default: 'success',
validator: value => ['success', 'error'].indexOf(value) !== -1
}
},
data() {
return {
showToast: false,
timer: ''
}
},
mounted() {
document.body.appendChild(this.$el)
},
methods: {
show(text = '', type = 'success', delay = 3500) {
this.showToast = true
if (!this.$el) this.$mount(document.createElement('div'))
this.type = type
this.text = text
// 指定时间后关闭
this.timer && clearTimeout(this.timer)
this.timer = setTimeout(() => (this.showToast = false), delay)
}
}
}
Toast.toast = (...options) => {
const Ctor = Vue.component('UToast')
if (!Ctor) return
let instance = new Ctor()
instance.show(...options)
}
export default Toast
</script>
<style lang="scss" scoped>
.u-toast {
position: fixed;
top: 56px;
left: 0;
right: 0;
margin: auto;
min-width: 300px;
width: 300px;
height: 100px;
z-index: 199;
.content {
display: flex;
justify-content: center;
flex-direction: column;
padding: 17px 40px;
background: #eff4fa;
border-radius: 2px;
.icon {
display: inline-block;
position: absolute;
width: 16px;
height: 16px;
line-height: 16px;
margin-right: 8px;
border-radius: 16px;
vertical-align: bottom;
text-align: center;
color: #fefefe;
font-size: 12px;
font-weight: bold;
&.success {
background: $success-color;
}
&.error {
background: $danger-color;
}
}
.text {
max-width: 196px;
margin-left: 24px;
font-size: 14px;
&.success {
color: $success-color;
}
&.error {
color: $danger-color;
}
}
}
}
</style>