@@ -19,6 +19,62 @@ import {
19
19
20
20
import { setParentMaxMeasures } from './measurements.js' ;
21
21
22
+ /**
23
+ * Contains the default value for each custom option.
24
+ * Those values can be overwritten by the user by calling jsCssAnimations.config()
25
+ * and passing new default values for all the animations.
26
+ * @type {Object.<string,any> }
27
+ */
28
+ const configurations = {
29
+ default : Object . freeze ( {
30
+ trigger : `.${ CLASS_NAMES . trigger } ` ,
31
+ targetSelector : undefined ,
32
+ staggerDelay : undefined ,
33
+ start : undefined ,
34
+ complete : undefined ,
35
+ keepSpace : false ,
36
+ dimensionsTransition : true ,
37
+ widthTransition : undefined ,
38
+ heightTransition : undefined ,
39
+ overflowHidden : true ,
40
+ } ) ,
41
+ } ;
42
+
43
+ /**
44
+ * ProxyHandler passed to the 'CONFIG' object to ensure that
45
+ * if an option is not customized by the user, the default value set
46
+ * in 'configurations.default' will be returned instead.
47
+ * @see {@link CONFIG }
48
+ * @see {@link configurations }
49
+ */
50
+ const configHandler = {
51
+ /**
52
+ * @param {Object.<string, Function> } configurations - Contains the configuration options
53
+ * @param {string } option - Key name of the configuration option
54
+ */
55
+ get ( configurations , option ) {
56
+ if ( ! ( option in configurations ) ) return configurations . default [ option ] ;
57
+ else return configurations [ option ] ;
58
+ } ,
59
+ /**
60
+ * @param {Object.<string, Function> } configurations - Contains the configuration options
61
+ * @param {string } option - Key name of the configuration option
62
+ * @param {any } value - Configuration option value
63
+ */
64
+ set ( configurations , option , value ) {
65
+ configurations [ option ] = value ;
66
+ return true ;
67
+ } ,
68
+ } ;
69
+
70
+ /**
71
+ * Object that handles configurations, either customized by the user
72
+ * or default values defined in 'configurations.default' object
73
+ * @type {Object.<string,any> }
74
+ * @see {@link configurations }
75
+ */
76
+ const CONFIG = new Proxy ( configurations , configHandler ) ;
77
+
22
78
/** Matches duration or delay CSS properties values */
23
79
const DURATION_REGEX = Object . freeze ( new RegExp ( / ( \d ? \. \d + | \d + ) ( m s | s ) ? / ) ) ;
24
80
@@ -40,14 +96,14 @@ const CALLBACK_TRACKER = Object.freeze({
40
96
* Initiates the tracker
41
97
* @param {string } trigger - A CSS selector representing the element which triggered the animation
42
98
*/
43
- init : function ( trigger ) {
99
+ init : function ( trigger ) {
44
100
CALLBACK_TRACKER . executing [ trigger ] = { } ;
45
101
} ,
46
102
/**
47
103
* Removes 'trigger' from the tracker
48
104
* @param {string } trigger - A CSS selector representing the element which triggered the animation
49
105
*/
50
- remove : function ( trigger ) {
106
+ remove : function ( trigger ) {
51
107
delete this . executing [ trigger ] ;
52
108
} ,
53
109
} ) ;
@@ -64,15 +120,15 @@ const TARGETS_STACK = {
64
120
* @param {HTMLElement } elem - Element being animated
65
121
* @param {string } trigger - CSS selector for the element that triggered the animation
66
122
*/
67
- add : function ( elem , trigger ) {
123
+ add : function ( elem , trigger ) {
68
124
if ( ! ( trigger in this . stack ) ) this . stack [ trigger ] = [ ] ;
69
125
this . stack [ trigger ] . push ( elem ) ;
70
126
} ,
71
127
/**
72
128
* Removes from the stack all the elements animated by the same trigger button
73
129
* @param {string } trigger - CSS selector for the element that triggered the animation
74
130
*/
75
- remove : function ( trigger ) {
131
+ remove : function ( trigger ) {
76
132
if ( ! ( trigger in this . stack ) ) return ;
77
133
delete this . stack [ trigger ] ;
78
134
} ,
@@ -81,7 +137,7 @@ const TARGETS_STACK = {
81
137
* @param {string } trigger - CSS selector for the element that triggered the animation
82
138
* @returns An array of elements that have been animated by the same trigger button
83
139
*/
84
- get : function ( trigger ) {
140
+ get : function ( trigger ) {
85
141
if ( ! ( trigger in this . stack ) ) return ;
86
142
return this . stack [ trigger ] ;
87
143
} ,
@@ -98,6 +154,31 @@ export const removeCustomCssProperties = element => {
98
154
} ) ;
99
155
} ;
100
156
157
+ /**
158
+ * Customize the default animations configurations by overwriting
159
+ * the 'CONFIG' values
160
+ * @param {Object } opts - All the options customized by the user
161
+ * @see {@link CONFIG }
162
+ */
163
+ const updateDefaultConfig = opts => {
164
+ for ( let option in CONFIG . default ) {
165
+ if ( opts [ option ] !== undefined ) {
166
+ CONFIG [ option ] = opts [ option ] ;
167
+ }
168
+ }
169
+ } ;
170
+
171
+ /**
172
+ * Reset the configurations to its default values
173
+ * by removing from 'CONFIG' all options customized by the user
174
+ * @see {@link CONFIG }
175
+ */
176
+ const resetDefaultConfig = ( ) => {
177
+ for ( let option in CONFIG . default ) {
178
+ delete CONFIG [ option ] ;
179
+ }
180
+ } ;
181
+
101
182
/**
102
183
* Sets an inline CSS property
103
184
* @param {HTMLElement } element - The DOM element which will receive the property
@@ -116,7 +197,7 @@ export const setCssProperty = (element, property, value) => {
116
197
*/
117
198
const updateCssProperties = ( element , opts ) => {
118
199
removeCustomCssProperties ( element ) ;
119
- removeInlineTransition ( element ) ;
200
+ if ( element !== document . documentElement ) removeInlineTransition ( element ) ;
120
201
CUSTOM_CSS_PROPERTIES . forEach ( prop => {
121
202
if ( typeof opts [ prop ] === 'string' || typeof opts [ prop ] === 'number' ) {
122
203
if ( typeof opts [ prop ] === 'number' ) {
@@ -328,13 +409,15 @@ const animate = (element, action, id, opts = {}) => {
328
409
const {
329
410
animType,
330
411
trigger,
331
- start,
332
- complete,
333
- keepSpace,
334
- dimensionsTransition = keepSpace || isMotion ( animType ) ? false : true ,
335
- widthTransition = dimensionsTransition ,
336
- heightTransition = dimensionsTransition ,
337
- overflowHidden = true ,
412
+ start = CONFIG . start ,
413
+ complete = CONFIG . complete ,
414
+ keepSpace = CONFIG . keepSpace ,
415
+ dimensionsTransition = keepSpace || isMotion ( animType )
416
+ ? false
417
+ : CONFIG . dimensionsTransition ,
418
+ widthTransition = CONFIG . widthTransition ?? dimensionsTransition ,
419
+ heightTransition = CONFIG . heightTransition ?? dimensionsTransition ,
420
+ overflowHidden = CONFIG . overflowHidden ,
338
421
} = opts ;
339
422
const { duration, delay } = getTotalAnimTime ( element ) ;
340
423
const OPPOSITE_ACTION = Object . freeze ( {
@@ -487,7 +570,9 @@ const preset = (el, args) => {
487
570
*/
488
571
const eventHandler = ( el , animationId , opts ) => {
489
572
return ( /** @type {Event } */ e ) => {
490
- e . stopPropagation ( ) ;
573
+ const { stopPropagation = true , preventDefault = true } = opts ;
574
+ if ( stopPropagation ) e . stopPropagation ( ) ;
575
+ if ( preventDefault ) e . preventDefault ( ) ;
491
576
492
577
const action = getAction ( el , opts . animType ) ;
493
578
if ( ! action )
@@ -513,7 +598,11 @@ const eventHandler = (el, animationId, opts) => {
513
598
* @see {@link module:globals.MOTION_ANIMS_ID }
514
599
*/
515
600
const init = ( animationId , opts = { } , eventType = 'click' ) => {
516
- const { trigger = `.${ CLASS_NAMES . trigger } ` , targetSelector, cursor } = opts ;
601
+ const {
602
+ trigger = CONFIG . trigger ,
603
+ targetSelector = CONFIG . targetSelector ,
604
+ cursor,
605
+ } = opts ;
517
606
518
607
document . querySelectorAll ( trigger ) . forEach ( btn => {
519
608
btn . classList . add ( CLASS_NAMES . btnCursor ) ;
@@ -540,4 +629,12 @@ const init = (animationId, opts = {}, eventType = 'click') => {
540
629
} ) ;
541
630
} ;
542
631
543
- export { init , animate , preset , isEnabled } ;
632
+ export {
633
+ init ,
634
+ animate ,
635
+ preset ,
636
+ isEnabled ,
637
+ updateCssProperties ,
638
+ updateDefaultConfig ,
639
+ resetDefaultConfig ,
640
+ } ;
0 commit comments