-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMdRipple.vue
176 lines (160 loc) · 4.36 KB
/
MdRipple.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<template>
<div
:class="['md-ripple', rippleClasses]"
@touchstart.passive="event => mdEventTrigger && touchStartCheck(event)"
@touchmove.passive="event => mdEventTrigger && touchMoveCheck(event)"
@mousedown.passive="event => mdEventTrigger && startRipple(event)">
<slot />
<div v-if="!isDisabled">
<md-wave v-for="ripple in ripples" :key="ripple.uuid" :class="['md-ripple-wave', waveClasses]" :style="ripple.waveStyles" @md-end="clearWave(ripple.uuid)" />
</div>
</div>
</template>
<script>
import raf from 'raf'
import MdComponent from 'core/MdComponent'
import uuid from 'core/utils/MdUuid'
import MdWave from './MdWave'
export default new MdComponent({
name: 'MdRipple',
components: {
MdWave
},
props: {
mdActive: null,
mdDisabled: Boolean,
mdCentered: Boolean,
mdEventTrigger: {
type: Boolean,
default: true
}
},
data: () => ({
ripples: [],
touchTimeout: null,
eventType: null
}),
computed: {
isDisabled () {
return !this.$material.ripple || this.mdDisabled
},
rippleClasses () {
return {
'md-disabled': this.isDisabled
}
},
waveClasses () {
return {
'md-centered': this.mdCentered
}
}
},
watch: {
mdActive (active) {
const isBoolean = typeof active === 'boolean'
const isEvent = active instanceof MouseEvent
if (isBoolean && this.mdCentered && active) {
this.startRipple({
type: 'mousedown'
})
} else if (isEvent) {
this.startRipple(active)
}
this.$emit('update:mdActive', false)
}
},
methods: {
touchMoveCheck () {
window.clearTimeout(this.touchTimeout)
},
touchStartCheck ($event) {
this.touchTimeout = window.setTimeout(() => {
this.startRipple($event)
}, 100)
},
startRipple ($event) {
raf(() => {
const { eventType, isDisabled, mdCentered } = this
if (!isDisabled && (!eventType || eventType === $event.type)) {
let size = this.getSize()
let position = null
if (mdCentered) {
position = this.getCenteredPosition(size)
} else {
position = this.getHitPosition($event, size)
}
this.eventType = $event.type
this.ripples.push({
waveStyles: this.applyStyles(position, size),
uuid: uuid()
})
}
})
},
applyStyles (position, size) {
size += 'px'
return {
...position,
width: size,
height: size
}
},
clearWave (uuid) {
uuid ? this.ripples = this.ripples.filter(ripple => ripple.uuid !== uuid) : this.ripples = []
},
getSize () {
const { offsetWidth, offsetHeight } = this.$el
return Math.round(Math.max(offsetWidth, offsetHeight))
},
getCenteredPosition (size) {
const halfSize = -size / 2 + 'px'
return {
'margin-top': halfSize,
'margin-left': halfSize
}
},
getHitPosition ($event, elementSize) {
const rect = this.$el.getBoundingClientRect()
let top = $event.pageY
let left = $event.pageX
if ($event.type === 'touchstart') {
top = $event.changedTouches[0].pageY
left = $event.changedTouches[0].pageX
}
return {
top: top - rect.top - elementSize / 2 - document.documentElement.scrollTop + 'px',
left: left - rect.left - elementSize / 2 - document.documentElement.scrollLeft + 'px'
}
}
}
})
</script>
<style lang="scss">
@import "~components/MdAnimation/variables";
.md-ripple {
width: 100%;
height: 100%;
position: relative;
z-index: 10;
overflow: hidden;
-webkit-mask-image: radial-gradient(circle, #fff 100%, #000 100%);
}
.md-ripple-wave {
position: absolute;
z-index: 1;
pointer-events: none;
background: currentColor;
border-radius: 50%;
opacity: 0;
transform: scale(2) translateZ(0);
&.md-centered {
animation-duration: 1.2s;
top: 50%;
left: 50%;
}
~ *:not(.md-ripple-wave) {
position: relative;
z-index: 2;
}
}
</style>