From 1c7e77b8546ac32b8176ab54dd06ede6ba7deb55 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Wed, 2 Oct 2024 10:12:46 +0800 Subject: style: switch to standardjs --- src/utils.mjs | 67 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 34 deletions(-) (limited to 'src/utils.mjs') diff --git a/src/utils.mjs b/src/utils.mjs index 3824672..d9ed2d7 100644 --- a/src/utils.mjs +++ b/src/utils.mjs @@ -5,21 +5,21 @@ * @param {Function} callback */ export const onRemove = (element, callback) => { - const parent = element.parentNode; - if (!parent) throw new Error('The node must already be attached'); + const parent = element.parentNode + if (!parent) throw new Error('The node must already be attached') - const obs = new MutationObserver(mutations => { + const obs = new window.MutationObserver(mutations => { for (const mutation of mutations) { for (const el of mutation.removedNodes) { if (el === element) { - obs.disconnect(); - callback(); + obs.disconnect() + callback() } } } - }); - obs.observe(parent, { childList: true }); -}; + }) + obs.observe(parent, { childList: true }) +} /** * Animate transition of DOMRect, with Web Animation API @@ -32,39 +32,38 @@ export const onRemove = (element, callback) => { * @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'); + if (!element.parentElement) { throw new Error('The node must already be attached') } - const { width: w1, height: h1, left: x1, top: y1 } = rect; + const { width: w1, height: h1, left: x1, top: y1 } = rect const { width: w2, height: h2, left: x2, - top: y2, - } = element.getBoundingClientRect(); + top: y2 + } = element.getBoundingClientRect() - const rw = (w1 ?? w2) / w2; - const rh = (h1 ?? h2) / h2; - const dx = x1 - x2; - const dy = y1 - y2; + const rw = (w1 ?? w2) / w2 + const rh = (h1 ?? h2) / h2 + const dx = x1 - x2 + const dy = y1 - y2 if ((dx === 0 && dy === 0) || !isFinite(rw) || !isFinite(rh)) { - return element.animate([], { duration: 0 }); + return element.animate([], { duration: 0 }) } - const transform1 = `translate(0, 0) scale(1, 1)`; - const transform2 = `translate(${dx}px, ${dy}px) scale(${rw}, ${rh})`; + const transform1 = 'translate(0, 0) scale(1, 1)' + const transform2 = `translate(${dx}px, ${dy}px) scale(${rw}, ${rh})` const keyframes = [ { transform: transform1, opacity: 1 }, - { transform: transform2, opacity: 0.3 }, - ]; - if (options.resume === true) keyframes.reverse(); + { transform: transform2, opacity: 0.3 } + ] + if (options.resume === true) keyframes.reverse() return element.animate(keyframes, { duration: options.duration ?? 500, - easing: 'ease-in-out', - }); -}; + easing: 'ease-in-out' + }) +} /** * Throttle for function call @@ -73,18 +72,18 @@ export const animateRectTransition = (element, rect, options = {}) => { * @param {Number} delay milliseconds * @returns {Any} return value of function call, or null if throttled */ -export function throttle(func, delay) { - let timerFlag = null; +export function throttle (func, delay) { + let timerFlag = null return function (...args) { - const context = this; - if (timerFlag !== null) return null; + const context = this + if (timerFlag !== null) return null timerFlag = setTimeout( () => (timerFlag = null), - typeof delay === 'function' ? delay.call(context) : delay, - ); + typeof delay === 'function' ? delay.call(context) : delay + ) - return func.call(context, ...args); - }; + return func.call(context, ...args) + } } -- cgit v1.2.3-70-g09d2