2019-10-07 20:57:25 +01:00
|
|
|
/**
|
|
|
|
* Used in the function below to store references of clean-up functions.
|
|
|
|
* Used to ensure only one transitionend function exists at any time.
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
const animateStylesCleanupMap: WeakMap<object, any> = new WeakMap();
|
2019-10-07 20:57:25 +01:00
|
|
|
|
2023-04-19 15:20:04 +01:00
|
|
|
/**
|
|
|
|
* Animate the css styles of an element using FLIP animation techniques.
|
2024-10-11 15:19:19 +01:00
|
|
|
* Styles must be an object where the keys are style rule names and the values
|
2023-04-19 15:20:04 +01:00
|
|
|
* are an array of two items in the format [initialValue, finalValue]
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
function animateStyles(
|
|
|
|
element: HTMLElement,
|
|
|
|
styles: Record<string, string[]>,
|
|
|
|
animTime: number = 400,
|
|
|
|
onComplete: Function | null = null
|
|
|
|
): void {
|
2023-04-19 15:20:04 +01:00
|
|
|
const styleNames = Object.keys(styles);
|
|
|
|
for (const style of styleNames) {
|
2024-10-11 15:19:19 +01:00
|
|
|
element.style.setProperty(style, styles[style][0]);
|
2023-04-19 15:20:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
for (const style of styleNames) {
|
2024-10-11 15:19:19 +01:00
|
|
|
element.style.removeProperty(style);
|
2023-04-19 15:20:04 +01:00
|
|
|
}
|
2024-10-11 15:19:19 +01:00
|
|
|
element.style.removeProperty('transition');
|
2023-04-19 15:20:04 +01:00
|
|
|
element.removeEventListener('transitionend', cleanup);
|
|
|
|
animateStylesCleanupMap.delete(element);
|
|
|
|
if (onComplete) onComplete();
|
|
|
|
};
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
element.style.transition = `all ease-in-out ${animTime}ms`;
|
|
|
|
for (const style of styleNames) {
|
2024-10-11 15:19:19 +01:00
|
|
|
element.style.setProperty(style, styles[style][1]);
|
2023-04-19 15:20:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
element.addEventListener('transitionend', cleanup);
|
|
|
|
animateStylesCleanupMap.set(element, cleanup);
|
|
|
|
}, 15);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the active cleanup action for the given element.
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
function cleanupExistingElementAnimation(element: Element) {
|
2023-04-19 15:20:04 +01:00
|
|
|
if (animateStylesCleanupMap.has(element)) {
|
|
|
|
const oldCleanup = animateStylesCleanupMap.get(element);
|
|
|
|
oldCleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:54:34 +00:00
|
|
|
/**
|
|
|
|
* Fade in the given element.
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
export function fadeIn(element: HTMLElement, animTime: number = 400, onComplete: Function | null = null): void {
|
2019-12-07 16:54:34 +00:00
|
|
|
cleanupExistingElementAnimation(element);
|
|
|
|
element.style.display = 'block';
|
|
|
|
animateStyles(element, {
|
2024-10-11 15:19:19 +01:00
|
|
|
'opacity': ['0', '1'],
|
2019-12-07 16:54:34 +00:00
|
|
|
}, animTime, () => {
|
|
|
|
if (onComplete) onComplete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-06 15:08:26 +01:00
|
|
|
/**
|
|
|
|
* Fade out the given element.
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
export function fadeOut(element: HTMLElement, animTime: number = 400, onComplete: Function | null = null): void {
|
2019-10-07 20:57:25 +01:00
|
|
|
cleanupExistingElementAnimation(element);
|
2019-07-06 15:08:26 +01:00
|
|
|
animateStyles(element, {
|
2024-10-11 15:19:19 +01:00
|
|
|
'opacity': ['1', '0'],
|
2019-07-06 15:08:26 +01:00
|
|
|
}, animTime, () => {
|
|
|
|
element.style.display = 'none';
|
|
|
|
if (onComplete) onComplete();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:51:01 +01:00
|
|
|
/**
|
|
|
|
* Hide the element by sliding the contents upwards.
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
export function slideUp(element: HTMLElement, animTime: number = 400) {
|
2019-10-07 20:57:25 +01:00
|
|
|
cleanupExistingElementAnimation(element);
|
2019-06-07 15:51:01 +01:00
|
|
|
const currentHeight = element.getBoundingClientRect().height;
|
|
|
|
const computedStyles = getComputedStyle(element);
|
|
|
|
const currentPaddingTop = computedStyles.getPropertyValue('padding-top');
|
|
|
|
const currentPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
|
|
|
|
const animStyles = {
|
2024-10-11 15:19:19 +01:00
|
|
|
'max-height': [`${currentHeight}px`, '0px'],
|
|
|
|
'overflow': ['hidden', 'hidden'],
|
|
|
|
'padding-top': [currentPaddingTop, '0px'],
|
|
|
|
'padding-bottom': [currentPaddingBottom, '0px'],
|
2019-06-07 15:51:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
animateStyles(element, animStyles, animTime, () => {
|
|
|
|
element.style.display = 'none';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the given element by expanding the contents.
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
export function slideDown(element: HTMLElement, animTime: number = 400) {
|
2019-10-07 20:57:25 +01:00
|
|
|
cleanupExistingElementAnimation(element);
|
2019-06-07 15:51:01 +01:00
|
|
|
element.style.display = 'block';
|
|
|
|
const targetHeight = element.getBoundingClientRect().height;
|
|
|
|
const computedStyles = getComputedStyle(element);
|
|
|
|
const targetPaddingTop = computedStyles.getPropertyValue('padding-top');
|
|
|
|
const targetPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
|
|
|
|
const animStyles = {
|
2024-10-11 15:19:19 +01:00
|
|
|
'max-height': ['0px', `${targetHeight}px`],
|
|
|
|
'overflow': ['hidden', 'hidden'],
|
|
|
|
'padding-top': ['0px', targetPaddingTop],
|
|
|
|
'padding-bottom': ['0px', targetPaddingBottom],
|
2019-06-07 15:51:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
animateStyles(element, animStyles, animTime);
|
|
|
|
}
|
|
|
|
|
2022-05-14 13:32:25 +01:00
|
|
|
/**
|
|
|
|
* Transition the height of the given element between two states.
|
|
|
|
* Call with first state, and you'll receive a function in return.
|
|
|
|
* Call the returned function in the second state to animate between those two states.
|
|
|
|
* If animating to/from 0-height use the slide-up/slide down as easier alternatives.
|
|
|
|
*/
|
2024-10-11 15:19:19 +01:00
|
|
|
export function transitionHeight(element: HTMLElement, animTime: number = 400): () => void {
|
2022-05-14 13:32:25 +01:00
|
|
|
const startHeight = element.getBoundingClientRect().height;
|
|
|
|
const initialComputedStyles = getComputedStyle(element);
|
|
|
|
const startPaddingTop = initialComputedStyles.getPropertyValue('padding-top');
|
|
|
|
const startPaddingBottom = initialComputedStyles.getPropertyValue('padding-bottom');
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
cleanupExistingElementAnimation(element);
|
|
|
|
const targetHeight = element.getBoundingClientRect().height;
|
|
|
|
const computedStyles = getComputedStyle(element);
|
|
|
|
const targetPaddingTop = computedStyles.getPropertyValue('padding-top');
|
|
|
|
const targetPaddingBottom = computedStyles.getPropertyValue('padding-bottom');
|
|
|
|
const animStyles = {
|
2024-10-11 15:19:19 +01:00
|
|
|
'height': [`${startHeight}px`, `${targetHeight}px`],
|
|
|
|
'overflow': ['hidden', 'hidden'],
|
|
|
|
'padding-top': [startPaddingTop, targetPaddingTop],
|
|
|
|
'padding-bottom': [startPaddingBottom, targetPaddingBottom],
|
2022-05-14 13:32:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
animateStyles(element, animStyles, animTime);
|
|
|
|
};
|
|
|
|
}
|