From 516aab9de3d6ad5b52da75666ce9f6531c913be6 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Sun, 22 Sep 2024 17:51:57 +0800 Subject: feat(utils): return empty Animation, not null for DOMRect --- src/dumbymap.mjs | 15 ++++++--------- src/utils.mjs | 4 ++-- 2 files changed, 8 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/dumbymap.mjs b/src/dumbymap.mjs index 7079286..1db8437 100644 --- a/src/dumbymap.mjs +++ b/src/dumbymap.mjs @@ -267,8 +267,7 @@ export const generateMaps = async (container, callback) => { const placeholder = htmlHolder.querySelector(`[data-placeholder="${target.id}"]`) if (!placeholder) throw Error(`Cannot fine placeholder for map "${target.id}"`) - // animation from Showcase to placeholder - const animation = animateRectTransition(target, placeholder.getBoundingClientRect(), { duration: 300 }) + .catch(afterAnimation) // Consider animation may fail, write callback const afterAnimation = () => { @@ -276,13 +275,11 @@ export const generateMaps = async (container, callback) => { target.style = placeholder.style.cssText placeholder.remove() } - if (animation) { - animation.finished - .then(afterAnimation) - .catch(afterAnimation) - } else { - afterAnimation() - } + + // animation from Showcase to placeholder + animateRectTransition(target, placeholder.getBoundingClientRect(), { duration: 300 }) + .finished + .finally(afterAnimation) } }) // }}} diff --git a/src/utils.mjs b/src/utils.mjs index eeedb81..41b772e 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -29,7 +29,7 @@ export const onRemove = (element, callback) => { * @param {Object} options * @param {Boolean} options.resume If true, transition starts from rect to DOMRect of element * @param {Number} options.duration Duration of animation in milliseconds - * @returns {Animation|null} https://developer.mozilla.org/en-US/docs/Web/API/Animation + * @returns {Animation} https://developer.mozilla.org/en-US/docs/Web/API/Animation */ export const animateRectTransition = (element, rect, options = {}) => { if (!element.parentElement) throw new Error("The node must already be attached"); @@ -43,7 +43,7 @@ export const animateRectTransition = (element, rect, options = {}) => { const dy = y1 - y2; if (dx === 0 && dy === 0 || rw === Infinity || rh === Infinity) { - return null; + return new Animation() } const transform1 = `translate(0, 0) scale(1, 1)`; -- cgit v1.2.3-70-g09d2