-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs-css-animations.js.html
414 lines (376 loc) · 12.8 KB
/
js-css-animations.js.html
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: js-css-animations.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: js-css-animations.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* Builds the animation API that will be exported to the final user
* @module js-css-animations
*/
import {
init,
end,
animate,
preset,
isEnabled,
updateCssProperties,
setCssProperty,
updateDefaultConfig,
resetDefaultConfig,
} from './animate.js';
import { VISIBILITY_ANIMS_ID, MOTION_ANIMS_ID } from './globals.js';
/**
* If 'selector' is a string containing a valid CSS selector,
* it will be used to perform a querySelector(),
* If 'selector' is already an HTMLElement it will be returned as it is.
* @param {Element|string} selector - If it's an HTMLElement, 'selector' will be returned as it is. If it's a string, it should be a valid CSS selector
* @returns An HTMLElement
*/
const selectElement = selector => {
const el =
selector instanceof HTMLElement
? selector
: typeof selector === 'string'
? document.querySelector(selector)
: null;
if (!el)
throw new ReferenceError(
`Invalid element: '${selector}' Expected HTMLElement or a valid CSS selector`
);
return el;
};
/**
* Returns a NodeList with all elements that match 'selector'
* @param {string} selector - A valid CSS selector to be passed to querySelectorAll()
* @returns A NodeList containing all elements matched by the 'selector'
*/
const selectAllElements = selector => {
const elementList = document.querySelectorAll(selector);
if (!elementList)
throw new ReferenceError(
`Invalid element: '${selector}' Expected a valid CSS selector`
);
return elementList;
};
/**
* Gets the element(s) to be animated. The user can pass either an HTMLElement or a CSS selector as a target to the animation
* @param {HTMLElement|string} selector - An HTMLElement or a valid CSS selector to be passed to querySelectorAll()
* @returns An array containing a single HTMLElement or a NodeList with all the elements matching the CSS selector in 'selector'
*/
const getTargets = selector => {
return selector instanceof HTMLElement
? [selector]
: selectAllElements(selector);
};
/**
* Default values used by all animations are set by the user by
* overriding default animations properties and options
* @param {Object} opts - All custom animation properties and options.
* @see {@link module:globals.PROPERTY_NAMES}
* @see {@link module:animate~configurations}
*/
const config = opts => {
updateDefaultConfig(opts);
updateCssProperties(document.documentElement, opts);
if (opts.cursor)
setCssProperty(document.documentElement, 'cursor', opts.cursor);
};
/**
* Resets default animations values by
* removing default values customized by the user
*/
const reset = () => {
config({});
resetDefaultConfig();
};
/**
* Toggles between two animations.
*
* If 'animA' and 'animB' have the same name,
* it will toggle between the 'hide' state and the 'show' state,
* although this is only applicable to visibility animations.
* @see {@link module:globals.VISIBILITY_ANIMS_ID}
* @param {HTMLElement|string} selector - The DOM element or a valid CSS selector with the element(s) to be animated
* @param {string} animA - The initial animation name
* @param {string} animB - The next animation name
* @param {{
* duration: number|string|undefined,
* delay: number|string|undefined,
* staggerDelay: number|string|undefined,
* easing: string|undefined,
* blur: string|undefined,
* angle: string|undefined,
* iteration: string|undefined,
* maintainSpace: boolean|undefined,
* overflowHidden: boolean|undefined,
* dimensionsTransition: boolean|undefined,
* widthTransition: boolean|undefined,
* heightTransition: boolean|undefined
* }|{}} opts - All options that can be passed by the user to customize the animation
*/
const toggle = (selector, animA, animB, opts = {}) => {
const args = {};
[
'duration',
'delay',
'staggerDelay',
'easing',
'blur',
'angle',
'iteration',
'direction',
'transfOrigin',
'maintainSpace',
'overflowHidden',
'dimensionsTransition',
'widthTransition',
'heightTransition',
'start',
'complete',
].forEach(prop => (args[prop] = opts[prop]));
/**
* The current animation attribute will only be set in the first element that
* matches the 'selector' passed, but the animation will apply to all elements
* matched by 'selector'
*/
const element = selectElement(selector);
const currentAnim = element.getAttribute('js-css-animation--current');
const newAnim =
!currentAnim || ![animA, animB].includes(currentAnim)
? animA
: currentAnim === animA
? animB
: animA;
const animType =
MOTION_ANIMS_ID[newAnim] !== undefined ? 'motion' : 'visibility';
element.setAttribute('js-css-animation--current', newAnim);
if (animType === 'visibility') {
checkVisibility(element, 'hidden')
? jsCssAnimations.show[newAnim](selector, args)
: jsCssAnimations.hide[newAnim](selector, args);
} else if (animType === 'motion') {
const animFn = jsCssAnimations[newAnim];
if (typeof animFn === 'function') animFn(selector, args);
}
};
/**
* An object containing all the animations functions.
*
* Visibility animations functions are under
* animationFunctions.hide and animationFunctions.show
*
* All other keys of animationFunctions are Motion animations functions
* @see {@link module:globals.VISIBILITY_ANIMS_ID}
* @see {@link module:globals.MOTION_ANIMS_ID}
* @type {Object}
*/
const animationFunctions = (function () {
const handlers = {};
['show', 'hide', 'move'].forEach(action => {
const { animIds, animType } =
action === 'move'
? { animIds: MOTION_ANIMS_ID, animType: 'motion' }
: { animIds: VISIBILITY_ANIMS_ID, animType: 'visibility' };
for (const [name, id] of Object.entries(animIds)) {
const handler = (target, opts = {}) => {
const args = { animType };
[
'start',
'complete',
'maintainSpace',
'overflowHidden',
'staggerDelay',
'widthTransition',
'heightTransition',
'dimensionsTransition',
].forEach(opt => (args[opt] = opts[opt]));
getTargets(target).forEach((element, i) => {
opts.animType = animType;
opts.queryIndex = i;
preset(element, {
opts,
animationId: id,
});
if (isEnabled(element)) animate(element, action, id, args);
});
};
if (action === 'move') {
handlers[name] = handler;
} else {
if (!handlers[action]) handlers[action] = {};
handlers[action][name] = handler;
}
}
});
const blink = (target, opts = {}) => {
jsCssAnimations.show.fade(target, {
iteration: 'infinite',
direction: 'alternate',
...opts,
});
};
const pulsate = (target, opts = {}) => {
jsCssAnimations.scale(target, {
finalScale: '1.2',
iteration: 'infinite',
direction: 'alternate',
...opts,
});
};
handlers.blink = blink;
handlers.pulsate = pulsate;
return handlers;
})();
/**
* An object containing animations functions wich are triggered by an event (like 'click')
* @type {Object.<string, Function>}
*/
// @ts-ignore
const eventBoundAnimations = (() => {
const animations = {};
[
{ animIds: VISIBILITY_ANIMS_ID, animType: 'visibility' },
{ animIds: MOTION_ANIMS_ID, animType: 'motion' },
].forEach(({ animIds, animType }) => {
Object.keys(animIds).forEach(animName => {
/**
* Initiate the event listener with the animation to be performed
* @param {Object.<string, any>} opts - Contains all options passed by the user to customize the animation
*/
animations[animName] = opts => {
init(animIds[animName], { animType, ...opts });
};
});
});
return animations;
})();
/**
* Verifies if an element is out of its original orientation or scale.
*
* Note that if the element has CSS property 'transform: rotate(0deg)',
* isTransformed() will still return False, as the element is not
* out of its original orientation.
* @param {HTMLElement|string} selector - An element or a valid CSS selector corresponding to the element
* @returns True if the element was rotated from its original orientation. False if it maintains the original orientation.
*/
const isTransformed = selector => {
const el = selectElement(selector);
const transform = getComputedStyle(el).transform;
return transform !== 'none' && transform !== 'matrix(1, 0, 0, 1, 0, 0)';
};
/**
* Verifies if a given element is hidden or visible
* @param {Element|string} selector - An element or a valid CSS selector corresponding to the element
* @param {string} mode - Either 'visible' or 'hidden'
* @returns {boolean} True or False depending if the element is visible or hidden, according to the 'mode' passed
*/
const checkVisibility = (selector, mode) => {
const el = selectElement(selector);
let result = false;
if (mode === 'hidden') {
result =
getComputedStyle(el).visibility === 'hidden' ||
getComputedStyle(el).display === 'none';
} else if (mode === 'visible') {
result = !checkVisibility(selector, 'hidden');
}
return result;
};
/**
* Will throw an ReferenceError if the animation name does not corresponds to any animation function
* @type {ProxyHandler}
*/
const verifyAnimationName = {
/**
* @param {Object.<string, Function>} animations - Object containing animation functions
* @param {string} name - Name of the animation
*/
get(animations, name) {
if (!(name in animations))
throw new ReferenceError(`${name} is not a valid animation`);
return animations[name];
},
};
/**
* An API encapsulating all the functions that can be used by the user,
* like all the animations functions and auxiliary functions like:
* isTransformed(), isVisible() and isHidden()
* @type {Object.<string, Function|Object>}
*/
const jsCssAnimations = (function () {
/**
* Encapsulates eventBoundAnimations(), adding animation name validation
* @see eventBoundAnimations
* @type {Object.<string, Function>}
*/
const eventAnimations = new Proxy(eventBoundAnimations, verifyAnimationName);
/**
* Encapsulates animationFunctions.show, adding animation name validation
* @see animationFunctions
* @type {Object.<string, Function>}
*/
const showVisibilityAnim = new Proxy(
animationFunctions.show,
verifyAnimationName
);
/**
* Encapsulates animationFunctions.hide, adding animation name validation
* @see animationFunctions
* @type {Object.<string, Function>}
*/
const hideVisibilityAnim = new Proxy(
animationFunctions.hide,
verifyAnimationName
);
const animationsHandler = Object.freeze({
config,
reset,
end,
init: eventAnimations,
...animationFunctions,
show: showVisibilityAnim,
hide: hideVisibilityAnim,
toggle,
isTransformed,
/**
* @param {Element|string} selector - Dom element or a valid CSS selector
* @returns True if the element is visible, False otherwise
*/
isVisible: selector => checkVisibility(selector, 'visible'),
/**
* @param {Element|string} selector - Dom element or a valid CSS selector
* @returns True if the element is hidden, False otherwise
*/
isHidden: selector => checkVisibility(selector, 'hidden'),
});
return new Proxy(animationsHandler, verifyAnimationName);
})();
export default jsCssAnimations;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">JS-CSS Animations</a></h2><h3>Modules</h3><ul><li><a href="module-animate.html">animate</a></li><li><a href="module-globals.html">globals</a></li><li><a href="module-js-css-animations.html">js-css-animations</a></li><li><a href="module-measurements.html">measurements</a></li><li><a href="module-resize-parent.html">resize-parent</a></li><li><a href="module-transitions.html">transitions</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.0</a> on Wed Dec 07 2022 15:22:18 GMT-0300 (Horário Padrão de Brasília)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>