11/**
22 * Used in the function below to store references of clean-up functions.
33 * Used to ensure only one transitionend function exists at any time.
4- * @type {WeakMap<object, any> }
54 */
6- const animateStylesCleanupMap = new WeakMap ( ) ;
5+ const animateStylesCleanupMap : WeakMap < object , any > = new WeakMap ( ) ;
76
87/**
98 * 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
1110 * 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
1611 */
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 {
1818 const styleNames = Object . keys ( styles ) ;
1919 for ( const style of styleNames ) {
20- element . style [ style ] = styles [ style ] [ 0 ] ;
20+ element . style . setProperty ( style , styles [ style ] [ 0 ] ) ;
2121 }
2222
2323 const cleanup = ( ) => {
2424 for ( const style of styleNames ) {
25- element . style [ style ] = null ;
25+ element . style . removeProperty ( style ) ;
2626 }
27- element . style . transition = null ;
27+ element . style . removeProperty ( ' transition' ) ;
2828 element . removeEventListener ( 'transitionend' , cleanup ) ;
2929 animateStylesCleanupMap . delete ( element ) ;
3030 if ( onComplete ) onComplete ( ) ;
@@ -33,7 +33,7 @@ function animateStyles(element, styles, animTime = 400, onComplete = null) {
3333 setTimeout ( ( ) => {
3434 element . style . transition = `all ease-in-out ${ animTime } ms` ;
3535 for ( const style of styleNames ) {
36- element . style [ style ] = styles [ style ] [ 1 ] ;
36+ element . style . setProperty ( style , styles [ style ] [ 1 ] ) ;
3737 }
3838
3939 element . addEventListener ( 'transitionend' , cleanup ) ;
@@ -43,9 +43,8 @@ function animateStyles(element, styles, animTime = 400, onComplete = null) {
4343
4444/**
4545 * Run the active cleanup action for the given element.
46- * @param {Element } element
4746 */
48- function cleanupExistingElementAnimation ( element ) {
47+ function cleanupExistingElementAnimation ( element : Element ) {
4948 if ( animateStylesCleanupMap . has ( element ) ) {
5049 const oldCleanup = animateStylesCleanupMap . get ( element ) ;
5150 oldCleanup ( ) ;
@@ -54,30 +53,24 @@ function cleanupExistingElementAnimation(element) {
5453
5554/**
5655 * Fade in the given element.
57- * @param {Element } element
58- * @param {Number } animTime
59- * @param {Function|null } onComplete
6056 */
61- export function fadeIn ( element , animTime = 400 , onComplete = null ) {
57+ export function fadeIn ( element : HTMLElement , animTime : number = 400 , onComplete : Function | null = null ) : void {
6258 cleanupExistingElementAnimation ( element ) ;
6359 element . style . display = 'block' ;
6460 animateStyles ( element , {
65- opacity : [ '0' , '1' ] ,
61+ ' opacity' : [ '0' , '1' ] ,
6662 } , animTime , ( ) => {
6763 if ( onComplete ) onComplete ( ) ;
6864 } ) ;
6965}
7066
7167/**
7268 * Fade out the given element.
73- * @param {Element } element
74- * @param {Number } animTime
75- * @param {Function|null } onComplete
7669 */
77- export function fadeOut ( element , animTime = 400 , onComplete = null ) {
70+ export function fadeOut ( element : HTMLElement , animTime : number = 400 , onComplete : Function | null = null ) : void {
7871 cleanupExistingElementAnimation ( element ) ;
7972 animateStyles ( element , {
80- opacity : [ '1' , '0' ] ,
73+ ' opacity' : [ '1' , '0' ] ,
8174 } , animTime , ( ) => {
8275 element . style . display = 'none' ;
8376 if ( onComplete ) onComplete ( ) ;
@@ -86,20 +79,18 @@ export function fadeOut(element, animTime = 400, onComplete = null) {
8679
8780/**
8881 * Hide the element by sliding the contents upwards.
89- * @param {Element } element
90- * @param {Number } animTime
9182 */
92- export function slideUp ( element , animTime = 400 ) {
83+ export function slideUp ( element : HTMLElement , animTime : number = 400 ) {
9384 cleanupExistingElementAnimation ( element ) ;
9485 const currentHeight = element . getBoundingClientRect ( ) . height ;
9586 const computedStyles = getComputedStyle ( element ) ;
9687 const currentPaddingTop = computedStyles . getPropertyValue ( 'padding-top' ) ;
9788 const currentPaddingBottom = computedStyles . getPropertyValue ( 'padding-bottom' ) ;
9889 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' ] ,
10394 } ;
10495
10596 animateStyles ( element , animStyles , animTime , ( ) => {
@@ -109,21 +100,19 @@ export function slideUp(element, animTime = 400) {
109100
110101/**
111102 * Show the given element by expanding the contents.
112- * @param {Element } element - Element to animate
113- * @param {Number } animTime - Animation time in ms
114103 */
115- export function slideDown ( element , animTime = 400 ) {
104+ export function slideDown ( element : HTMLElement , animTime : number = 400 ) {
116105 cleanupExistingElementAnimation ( element ) ;
117106 element . style . display = 'block' ;
118107 const targetHeight = element . getBoundingClientRect ( ) . height ;
119108 const computedStyles = getComputedStyle ( element ) ;
120109 const targetPaddingTop = computedStyles . getPropertyValue ( 'padding-top' ) ;
121110 const targetPaddingBottom = computedStyles . getPropertyValue ( 'padding-bottom' ) ;
122111 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 ] ,
127116 } ;
128117
129118 animateStyles ( element , animStyles , animTime ) ;
@@ -134,11 +123,8 @@ export function slideDown(element, animTime = 400) {
134123 * Call with first state, and you'll receive a function in return.
135124 * Call the returned function in the second state to animate between those two states.
136125 * 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.
140126 */
141- export function transitionHeight ( element , animTime = 400 ) {
127+ export function transitionHeight ( element : HTMLElement , animTime : number = 400 ) : ( ) => void {
142128 const startHeight = element . getBoundingClientRect ( ) . height ;
143129 const initialComputedStyles = getComputedStyle ( element ) ;
144130 const startPaddingTop = initialComputedStyles . getPropertyValue ( 'padding-top' ) ;
@@ -151,10 +137,10 @@ export function transitionHeight(element, animTime = 400) {
151137 const targetPaddingTop = computedStyles . getPropertyValue ( 'padding-top' ) ;
152138 const targetPaddingBottom = computedStyles . getPropertyValue ( 'padding-bottom' ) ;
153139 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 ] ,
158144 } ;
159145
160146 animateStyles ( element , animStyles , animTime ) ;
0 commit comments