index.vue
3.21 KB
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
<template>
<button class="zui-button" :class="classRender" :style="styleRender" v-on="bindEvents">
<slot></slot>
</button>
</template>
<script>
import color from 'color';
export default {
name: 'Button',
props: {
type: {
type: String,
default: 'default',
},
size: {
type: String,
default: 'md',
},
block: {
type: Boolean,
default: false,
},
round: Boolean,
square: Boolean,
disabled: Boolean,
theme: String,
color: String,
hoverClass: {
type: String,
default: 'button-hover',
},
hoverStartTime: {
type: Number,
default: 0,
},
hoverStayTime: {
type: Number,
default: 70,
},
},
data() {
return {
active: false,
};
},
computed: {
classRender() {
return {
[this.size]: true,
[this.type]: true,
[this.theme]: !!this.theme,
[this.hoverClass]: this.active,
disabled: this.disabled,
block: this.block,
round: this.round,
square: this.square,
custom: !!this.color,
};
},
styleRender() {
if (this.color) {
if (this.type === 'primary') {
return {
backgroundColor: this.active ? color(this.color).lighten(0.2) : this.color,
borderColor: this.active ? color(this.color).lighten(0.2) : this.color,
};
} else if (this.type === 'secondary') {
return {
backgroundColor: this.active ? color(this.color).lightness(90) : color(this.color).lightness(95),
color: this.color,
};
} else if (this.type === 'plain') {
return {
borderColor: this.color,
color: this.color,
backgroundColor: this.active ? color(this.color).lightness(90) : color(this.color).lightness(95),
};
} else if (this.type === 'ghost') {
return {
borderColor: this.color,
color: this.color,
backgroundColor: this.active ? color(this.color).lightness(95) : undefined,
};
} else {
return {
color: this.active ? color(this.color).lighten(0.2) : this.color,
};
}
}
return {};
},
bindEvents() {
const events = this.$listeners || {};
const activeEvents = ['mousedown', 'touchstart'];
const deactiveEvents = ['mouseup', 'mousemove', 'touchend', 'touchmove'];
let safeEvents = { ...events };
activeEvents.forEach(key => {
safeEvents[key] = e => {
const originEvent = events[key];
originEvent && originEvent(e);
if (this.hoverStartTime) {
setTimeout(() => {
this.active = true;
}, this.hoverStartTime);
} else {
this.active = true;
}
};
});
deactiveEvents.forEach(key => {
safeEvents[key] = e => {
const originEvent = events[key];
originEvent && originEvent(e);
setTimeout(() => {
this.active = false;
}, this.hoverStayTime);
};
});
return safeEvents;
},
},
};
</script>
<style lang="scss">
@import './index.scss';
</style>