/
TAlert.vue
150 lines (133 loc) · 4.08 KB
/
TAlert.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<template>
<transition name="alert">
<!--Alert Container-->
<div
v-if="showAlertBox"
:class="styleClass()"
>
<!--Inline Line-->
<div v-if="design.includes('line')"
id="line"
:class="[design === 'inline' ? 'alert-inline-line' : 'alert-outline-line']"
/>
<!--Alert Icon-->
<div v-if="$slots['icon'] && design !== 'elegant'"
class="alert-icon"
>
<slot name="icon"></slot>
</div>
<!--Alert Content-->
<div class="flex justify-start items-center flex-grow whitespace-normal py-2 px-4">
<!--Elegant Title-->
<div v-if="title && design === 'elegant'" id="elegant-title">
{{ title }}
</div>
<div class="grid grid-cols-1">
<!--General Title (except elegant style)-->
<span v-if="title && design !== 'elegant'" class="font-bold text-lg">{{ title }}</span>
<!--Content Slot-->
<div>
<slot></slot>
</div>
</div>
</div>
<!--Close Icon-->
<span v-if="closeable" class="alert-close" @click="close">
<t-x-circle-icon class="h-6 w-6"/>
</span>
<!--Countdown Line-->
<div v-if="timer" class="alert-countdown">
<div id="countdown" :style="{width: countDownCounter+'%'}"></div>
</div>
</div>
</transition>
</template>
<script>
import {defineComponent, ref, toRefs} from 'vue'
import TXCircleIcon from "@/Components/Icon/TXCircleIcon";
export default defineComponent({
name: "TAlert",
components: {TXCircleIcon},
props: {
id: {
type: String
},
closeable: {
type: Boolean,
default: false
},
timer: {
type: Number,
required: false
},
title: {
type: String,
default: null
},
radius: {
type: Number,
default: 3
},
design: {
type: String,
default: 'filled'
},
color: {
type: String,
default: 'white'
}
},
setup(props, {slots, emit}) {
/*Definitions*/
const {design, color, radius, timer, id} = toRefs(props)
const showAlertBox = ref(true)
/*Generating Style Classes*/
const styleClass = () => {
return 'alert ' +
'alert-' + design.value + '-' + color.value + ' '+
'alert-' + design.value + '-base' + ' '+
'radius-'+radius.value
}
/*Close Function*/
const close = () => {
showAlertBox.value = false;
emit('destroy', id.value)
}
/*Timer*/
const timerCounter = ref(0)
const countDownCounter = ref(0)
if (timer.value) {
/*Timer Function*/
let timerFn = setTimeout(() => {
showAlertBox.value = false;
emit('destroy', id.value)
}, timer.value)
/*Count Down Function*/
let countDownFn = setInterval(() => {
if (timer.value >= timerCounter.value) {
countDownCounter.value = 100 - (timerCounter.value / timer.value) * 100
timerCounter.value += 4
} else {
clearInterval(countDownFn)
}
}, 1)
}
/*Slot Check*/
const hasSlot = name => !!slots[name]
return {showAlertBox, styleClass, countDownCounter, timerCounter, hasSlot, close}
}
})
</script>
<style scoped>
.alert-enter-active, .alert-leave-active {
transition: opacity ease-out .75s;
}
.alert-enter, .alert-leave-to {
opacity: 0;
height: revert;
}
.alert-enter-to, .alert-leave {
opacity: 1;
height: revert;
}
</style>