2 * Used in the function below to store references of clean-up functions.
3 * Used to ensure only one transitionend function exists at any time.
5 const animateStylesCleanupMap: WeakMap<object, any> = new WeakMap();
8 * Animate the css styles of an element using FLIP animation techniques.
9 * Styles must be an object where the keys are style rule names and the values
10 * are an array of two items in the format [initialValue, finalValue]
12 function animateStyles(
14 styles: Record<string, string[]>,
15 animTime: number = 400,
16 onComplete: Function | null = null
18 const styleNames = Object.keys(styles);
19 for (const style of styleNames) {
20 element.style.setProperty(style, styles[style][0]);
23 const cleanup = () => {
24 for (const style of styleNames) {
25 element.style.removeProperty(style);
27 element.style.removeProperty('transition');
28 element.removeEventListener('transitionend', cleanup);
29 animateStylesCleanupMap.delete(element);
30 if (onComplete) onComplete();
34 element.style.transition = `all ease-in-out ${animTime}ms`;
35 for (const style of styleNames) {
36 element.style.setProperty(style, styles[style][1]);
39 element.addEventListener('transitionend', cleanup);
40 animateStylesCleanupMap.set(element, cleanup);
45 * Run the active cleanup action for the given element.
47 function cleanupExistingElementAnimation(element: Element) {
48 if (animateStylesCleanupMap.has(element)) {
49 const oldCleanup = animateStylesCleanupMap.get(element);
55 * Fade in the given element.
57 export function fadeIn(element: HTMLElement, animTime: number = 400, onComplete: Function | null = null): void {
58 cleanupExistingElementAnimation(element);
59 element.style.display = 'block';
60 animateStyles(element, {
61 'opacity': ['0', '1'],
63 if (onComplete) onComplete();
68 * Fade out the given element.
70 export function fadeOut(element: HTMLElement, animTime: number = 400, onComplete: Function | null = null): void {
71 cleanupExistingElementAnimation(element);
72 animateStyles(element, {
73 'opacity': ['1', '0'],
75 element.style.display = 'none';
76 if (onComplete) onComplete();
81 * Hide the element by sliding the contents upwards.
83 export function slideUp(element: HTMLElement, animTime: number = 400) {
84 cleanupExistingElementAnimation(element);
85 const currentHeight = element.getBoundingClientRect().height;
86 const computedStyles = getComputedStyle(element);
87 const currentPaddingTop = computedStyles.getPropertyValue('padding-top');
88 const currentPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
90 'max-height': [`${currentHeight}px`, '0px'],
91 'overflow': ['hidden', 'hidden'],
92 'padding-top': [currentPaddingTop, '0px'],
93 'padding-bottom': [currentPaddingBottom, '0px'],
96 animateStyles(element, animStyles, animTime, () => {
97 element.style.display = 'none';
102 * Show the given element by expanding the contents.
104 export function slideDown(element: HTMLElement, animTime: number = 400) {
105 cleanupExistingElementAnimation(element);
106 element.style.display = 'block';
107 const targetHeight = element.getBoundingClientRect().height;
108 const computedStyles = getComputedStyle(element);
109 const targetPaddingTop = computedStyles.getPropertyValue('padding-top');
110 const targetPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
112 'max-height': ['0px', `${targetHeight}px`],
113 'overflow': ['hidden', 'hidden'],
114 'padding-top': ['0px', targetPaddingTop],
115 'padding-bottom': ['0px', targetPaddingBottom],
118 animateStyles(element, animStyles, animTime);
122 * Transition the height of the given element between two states.
123 * Call with first state, and you'll receive a function in return.
124 * Call the returned function in the second state to animate between those two states.
125 * If animating to/from 0-height use the slide-up/slide down as easier alternatives.
127 export function transitionHeight(element: HTMLElement, animTime: number = 400): () => void {
128 const startHeight = element.getBoundingClientRect().height;
129 const initialComputedStyles = getComputedStyle(element);
130 const startPaddingTop = initialComputedStyles.getPropertyValue('padding-top');
131 const startPaddingBottom = initialComputedStyles.getPropertyValue('padding-bottom');
134 cleanupExistingElementAnimation(element);
135 const targetHeight = element.getBoundingClientRect().height;
136 const computedStyles = getComputedStyle(element);
137 const targetPaddingTop = computedStyles.getPropertyValue('padding-top');
138 const targetPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
140 'height': [`${startHeight}px`, `${targetHeight}px`],
141 'overflow': ['hidden', 'hidden'],
142 'padding-top': [startPaddingTop, targetPaddingTop],
143 'padding-bottom': [startPaddingBottom, targetPaddingBottom],
146 animateStyles(element, animStyles, animTime);