-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathripple.js
More file actions
255 lines (219 loc) · 7.3 KB
/
ripple.js
File metadata and controls
255 lines (219 loc) · 7.3 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* This is the only function that provides a ripple effect (hover or click)
* on elements with classes 'ripple-hover' and 'ripple-click'.
* The function is self-invoked. To make it work include this
* file in the page's HTML content directly.
* You can also import it into your other script file
* like this const ripple = require('path to this file').
*
* Script require also CSS styles to be included into page,
* othwerwise you will not see effects.
*
* @author Outcomer <773021792e@gmail.com>
*
* @return void
*/
const rippleFn = () => {
/**
* @param {WeakMap}
*/
const elementDataMap = new WeakMap()
/**
* Save anything regarding to element into element itself.
*
* @param {Element} element HTML element.
* @param {Object} data Data to save.
*/
const setElementData = (element, data) => {
elementDataMap.set(element, data)
}
/**
* Get saved data from an element.
*
* @param {Element} element HTML element.
*
* @return {Object} Saved data or an empty object.
*/
const getElementData = (element) => {
if (typeof elementDataMap.get(element, 'data') === 'undefined') {
setElementData(element, {})
}
return elementDataMap.get(element, 'data')
}
/**
* Bases on event coordinates and target size
* function calculates size of circle and it's
* position so that this circle cover target.
*
* @param {MouseEvent} event Browser event.
* @param {Element} target HTML element.
*
* @return {Object} A set of calculated coordinates for ripple.
*/
const prepareRipple = function (event, target) {
// Get width and height that will 100% cover target on ripple.
const client = target.getBoundingClientRect()
const x = event.clientX
const y = event.clientY
const toLeft = x - client.left
const toRight = client.right - x
const toTop = y - client.top
const toBottom = client.bottom - y
const maxX = Math.max(toLeft, toRight)
const maxY = Math.max(toTop, toBottom)
const deltaRadius = Math.hypot(maxX, maxY) - Math.max(maxX, maxY)
const buttonWidth = (Math.max(maxX, maxY) + deltaRadius) * 2
const buttonHeight = buttonWidth
const ripple = document.createElement('span')
ripple.className = 'ripple'
ripple.style.height = `${buttonHeight}px`
ripple.style.width = `${buttonWidth}px`
ripple.style.left = `${event.pageX - target.getBoundingClientRect().left - buttonWidth / 2}px`
ripple.style.top = `${event.pageY - (target.getBoundingClientRect().top + document.documentElement.scrollTop) - buttonHeight / 2}px`
return ripple
}
/**
* Ripple runner.
* Inject ripple element into DOM and apply
* CSS styles and classes needed for animation.
*
* @param {MouseEvent} event Browser event.
* @param {Element} target HTML element.
*
* @return {Promise} Promise will be resolved once animation finished.
*/
const doRippleClick = async function (event, target) {
setElementData(target, { licketySplitDoRipleClick: true })
const ripples = target.querySelector('.ripple')
if (ripples !== null) {
ripples.remove()
}
const rippleHTML = prepareRipple(event, target)
rippleHTML.classList.add('ripple', 'ripple-effect-click')
target.appendChild(rippleHTML)
// eslint-disable-next-line no-unused-vars
return await new Promise((resolve, reject) => {
const transition = window.getComputedStyle(rippleHTML).transitionDuration
const delay =
Math.max.apply(Math, transition.split(',').map(parseFloat)) * 1000
setTimeout(
function (timerTarget) {
setElementData(target, { licketySplitDoRipleClick: false })
rippleHTML.remove()
resolve([event, timerTarget])
},
delay,
target,
rippleHTML
)
})
}
/**
* Callback for hover IN event.
* Inject ripple element into DOM and apply
* CSS styles and classes needed for animation.
*
* @param {MouseEvent} event Browser event.
*
* @return {Promise} Promise resolved immediately.
*/
const doRippleHoverIn = async function (event) {
const rippleElement = event.target.closest('.ripple-hover')
const relatedTarget = event.relatedTarget
? event.relatedTarget.closest('.ripple-hover')
: null
if (!rippleElement || rippleElement === relatedTarget) {
return
}
clearTimeout(getElementData(rippleElement).rippleHoverOutTimer)
let rippleHTML = prepareRipple(event, rippleElement)
const currentRipple = rippleElement.querySelector('.ripple')
rippleHTML.classList.add('ripple', 'ripple-effect-hover-in')
if (currentRipple === null) {
rippleElement.appendChild(rippleHTML)
} else {
currentRipple.style.height = rippleHTML.style.height
currentRipple.style.width = rippleHTML.style.width
currentRipple.style.left = rippleHTML.style.left
currentRipple.style.top = rippleHTML.style.top
currentRipple.classList.remove('ripple-effect-hover-out')
currentRipple.classList.add('ripple-effect-hover-in')
rippleHTML = currentRipple
}
return Promise.resolve([event, rippleElement])
}
/**
* Callback for hover OUT event.
* Inject ripple element into DOM and apply
* CSS styles and classes needed for animation.
*
* @param {MouseEvent} event Browser event.
*
* @return {Promise} Promise will be resolved once animation finished.
*/
const doRippleHoverOut = async function (event) {
const rippleElement = event.target.closest('.ripple-hover')
const relatedTarget = event.relatedTarget
? event.relatedTarget.closest('.ripple-hover')
: null
if (!rippleElement || rippleElement === relatedTarget) {
return
}
let rippleHTML = prepareRipple(event, rippleElement)
const currentRipple = rippleElement.querySelector('.ripple')
if (currentRipple === null) {
rippleElement.appendChild(rippleHTML)
} else {
currentRipple.style.height = rippleHTML.style.height
currentRipple.style.width = rippleHTML.style.width
currentRipple.style.left = rippleHTML.style.left
currentRipple.style.top = rippleHTML.style.top
currentRipple.classList.remove('ripple-effect-hover-in')
currentRipple.classList.add('ripple-effect-hover-out')
rippleHTML = currentRipple
}
// eslint-disable-next-line no-unused-vars
return await new Promise((resolve, reject) => {
const transition = window.getComputedStyle(rippleHTML).transitionDuration
const delay = Math.max.apply(Math, transition.split(',').map(parseFloat)) * 1000
const rippleHoverOutTimer = setTimeout(
(timerTarget) => {
rippleHTML.remove()
resolve([event, timerTarget])
},
delay,
rippleElement,
rippleHTML
)
setElementData(rippleElement, { rippleHoverOutTimer })
})
}
/**
* Callback for click event.
* Validates that no ripple currently in progress
* and if no - run new.
*
* @param {MouseEvent} event Browser event.
*/
const rippleClick = function (event) {
if (!event.target.closest('.ripple-click')) {
return
}
if (getElementData(event.target).licketySplitDoRipleClick) {
return
}
doRippleClick(event, event.target).then(function (args) {
const eventNew = new Event('licketySplitClickRipleDone')
args[1].dispatchEvent(eventNew)
})
}
const run = () => {
document.querySelector('body').addEventListener('click', rippleClick)
if ('ontouchstart' in window === false) {
document.body.addEventListener('mouseover', doRippleHoverIn)
document.body.addEventListener('mouseout', doRippleHoverOut)
}
}
document.addEventListener('DOMContentLoaded', run)
}
export default rippleFn