1
1
/**
2
2
* Used in the function below to store references of clean-up functions.
3
3
* Used to ensure only one transitionend function exists at any time.
4
- * @type {WeakMap<object, any> }
5
4
*/
6
- const animateStylesCleanupMap = new WeakMap ( ) ;
5
+ const animateStylesCleanupMap : WeakMap < object , any > = new WeakMap ( ) ;
7
6
8
7
/**
9
8
* Animate the css styles of an element using FLIP animation techniques.
10
- * Styles must be an object where the keys are style properties, camelcase, and the values
9
+ * Styles must be an object where the keys are style rule names and the values
11
10
* are an array of two items in the format [initialValue, finalValue]
12
- * @param {Element } element
13
- * @param {Object } styles
14
- * @param {Number } animTime
15
- * @param {Function } onComplete
16
11
*/
17
- function animateStyles ( element , styles , animTime = 400 , onComplete = null ) {
12
+ function animateStyles (
13
+ element : HTMLElement ,
14
+ styles : Record < string , string [ ] > ,
15
+ animTime : number = 400 ,
16
+ onComplete : Function | null = null
17
+ ) : void {
18
18
const styleNames = Object . keys ( styles ) ;
19
19
for ( const style of styleNames ) {
20
- element . style [ style ] = styles [ style ] [ 0 ] ;
20
+ element . style . setProperty ( style , styles [ style ] [ 0 ] ) ;
21
21
}
22
22
23
23
const cleanup = ( ) => {
24
24
for ( const style of styleNames ) {
25
- element . style [ style ] = null ;
25
+ element . style . removeProperty ( style ) ;
26
26
}
27
- element . style . transition = null ;
27
+ element . style . removeProperty ( ' transition' ) ;
28
28
element . removeEventListener ( 'transitionend' , cleanup ) ;
29
29
animateStylesCleanupMap . delete ( element ) ;
30
30
if ( onComplete ) onComplete ( ) ;
@@ -33,7 +33,7 @@ function animateStyles(element, styles, animTime = 400, onComplete = null) {
33
33
setTimeout ( ( ) => {
34
34
element . style . transition = `all ease-in-out ${ animTime } ms` ;
35
35
for ( const style of styleNames ) {
36
- element . style [ style ] = styles [ style ] [ 1 ] ;
36
+ element . style . setProperty ( style , styles [ style ] [ 1 ] ) ;
37
37
}
38
38
39
39
element . addEventListener ( 'transitionend' , cleanup ) ;
@@ -43,9 +43,8 @@ function animateStyles(element, styles, animTime = 400, onComplete = null) {
43
43
44
44
/**
45
45
* Run the active cleanup action for the given element.
46
- * @param {Element } element
47
46
*/
48
- function cleanupExistingElementAnimation ( element ) {
47
+ function cleanupExistingElementAnimation ( element : Element ) {
49
48
if ( animateStylesCleanupMap . has ( element ) ) {
50
49
const oldCleanup = animateStylesCleanupMap . get ( element ) ;
51
50
oldCleanup ( ) ;
@@ -54,30 +53,24 @@ function cleanupExistingElementAnimation(element) {
54
53
55
54
/**
56
55
* Fade in the given element.
57
- * @param {Element } element
58
- * @param {Number } animTime
59
- * @param {Function|null } onComplete
60
56
*/
61
- export function fadeIn ( element , animTime = 400 , onComplete = null ) {
57
+ export function fadeIn ( element : HTMLElement , animTime : number = 400 , onComplete : Function | null = null ) : void {
62
58
cleanupExistingElementAnimation ( element ) ;
63
59
element . style . display = 'block' ;
64
60
animateStyles ( element , {
65
- opacity : [ '0' , '1' ] ,
61
+ ' opacity' : [ '0' , '1' ] ,
66
62
} , animTime , ( ) => {
67
63
if ( onComplete ) onComplete ( ) ;
68
64
} ) ;
69
65
}
70
66
71
67
/**
72
68
* Fade out the given element.
73
- * @param {Element } element
74
- * @param {Number } animTime
75
- * @param {Function|null } onComplete
76
69
*/
77
- export function fadeOut ( element , animTime = 400 , onComplete = null ) {
70
+ export function fadeOut ( element : HTMLElement , animTime : number = 400 , onComplete : Function | null = null ) : void {
78
71
cleanupExistingElementAnimation ( element ) ;
79
72
animateStyles ( element , {
80
- opacity : [ '1' , '0' ] ,
73
+ ' opacity' : [ '1' , '0' ] ,
81
74
} , animTime , ( ) => {
82
75
element . style . display = 'none' ;
83
76
if ( onComplete ) onComplete ( ) ;
@@ -86,20 +79,18 @@ export function fadeOut(element, animTime = 400, onComplete = null) {
86
79
87
80
/**
88
81
* Hide the element by sliding the contents upwards.
89
- * @param {Element } element
90
- * @param {Number } animTime
91
82
*/
92
- export function slideUp ( element , animTime = 400 ) {
83
+ export function slideUp ( element : HTMLElement , animTime : number = 400 ) {
93
84
cleanupExistingElementAnimation ( element ) ;
94
85
const currentHeight = element . getBoundingClientRect ( ) . height ;
95
86
const computedStyles = getComputedStyle ( element ) ;
96
87
const currentPaddingTop = computedStyles . getPropertyValue ( 'padding-top' ) ;
97
88
const currentPaddingBottom = computedStyles . getPropertyValue ( 'padding-bottom' ) ;
98
89
const animStyles = {
99
- maxHeight : [ `${ currentHeight } px` , '0px' ] ,
100
- overflow : [ 'hidden' , 'hidden' ] ,
101
- paddingTop : [ currentPaddingTop , '0px' ] ,
102
- paddingBottom : [ currentPaddingBottom , '0px' ] ,
90
+ 'max-height' : [ `${ currentHeight } px` , '0px' ] ,
91
+ ' overflow' : [ 'hidden' , 'hidden' ] ,
92
+ 'padding-top' : [ currentPaddingTop , '0px' ] ,
93
+ 'padding-bottom' : [ currentPaddingBottom , '0px' ] ,
103
94
} ;
104
95
105
96
animateStyles ( element , animStyles , animTime , ( ) => {
@@ -109,21 +100,19 @@ export function slideUp(element, animTime = 400) {
109
100
110
101
/**
111
102
* Show the given element by expanding the contents.
112
- * @param {Element } element - Element to animate
113
- * @param {Number } animTime - Animation time in ms
114
103
*/
115
- export function slideDown ( element , animTime = 400 ) {
104
+ export function slideDown ( element : HTMLElement , animTime : number = 400 ) {
116
105
cleanupExistingElementAnimation ( element ) ;
117
106
element . style . display = 'block' ;
118
107
const targetHeight = element . getBoundingClientRect ( ) . height ;
119
108
const computedStyles = getComputedStyle ( element ) ;
120
109
const targetPaddingTop = computedStyles . getPropertyValue ( 'padding-top' ) ;
121
110
const targetPaddingBottom = computedStyles . getPropertyValue ( 'padding-bottom' ) ;
122
111
const animStyles = {
123
- maxHeight : [ '0px' , `${ targetHeight } px` ] ,
124
- overflow : [ 'hidden' , 'hidden' ] ,
125
- paddingTop : [ '0px' , targetPaddingTop ] ,
126
- paddingBottom : [ '0px' , targetPaddingBottom ] ,
112
+ 'max-height' : [ '0px' , `${ targetHeight } px` ] ,
113
+ ' overflow' : [ 'hidden' , 'hidden' ] ,
114
+ 'padding-top' : [ '0px' , targetPaddingTop ] ,
115
+ 'padding-bottom' : [ '0px' , targetPaddingBottom ] ,
127
116
} ;
128
117
129
118
animateStyles ( element , animStyles , animTime ) ;
@@ -134,11 +123,8 @@ export function slideDown(element, animTime = 400) {
134
123
* Call with first state, and you'll receive a function in return.
135
124
* Call the returned function in the second state to animate between those two states.
136
125
* If animating to/from 0-height use the slide-up/slide down as easier alternatives.
137
- * @param {Element } element - Element to animate
138
- * @param {Number } animTime - Animation time in ms
139
- * @returns {function } - Function to run in second state to trigger animation.
140
126
*/
141
- export function transitionHeight ( element , animTime = 400 ) {
127
+ export function transitionHeight ( element : HTMLElement , animTime : number = 400 ) : ( ) => void {
142
128
const startHeight = element . getBoundingClientRect ( ) . height ;
143
129
const initialComputedStyles = getComputedStyle ( element ) ;
144
130
const startPaddingTop = initialComputedStyles . getPropertyValue ( 'padding-top' ) ;
@@ -151,10 +137,10 @@ export function transitionHeight(element, animTime = 400) {
151
137
const targetPaddingTop = computedStyles . getPropertyValue ( 'padding-top' ) ;
152
138
const targetPaddingBottom = computedStyles . getPropertyValue ( 'padding-bottom' ) ;
153
139
const animStyles = {
154
- height : [ `${ startHeight } px` , `${ targetHeight } px` ] ,
155
- overflow : [ 'hidden' , 'hidden' ] ,
156
- paddingTop : [ startPaddingTop , targetPaddingTop ] ,
157
- paddingBottom : [ startPaddingBottom , targetPaddingBottom ] ,
140
+ ' height' : [ `${ startHeight } px` , `${ targetHeight } px` ] ,
141
+ ' overflow' : [ 'hidden' , 'hidden' ] ,
142
+ 'padding-top' : [ startPaddingTop , targetPaddingTop ] ,
143
+ 'padding-bottom' : [ startPaddingBottom , targetPaddingBottom ] ,
158
144
} ;
159
145
160
146
animateStyles ( element , animStyles , animTime ) ;
0 commit comments