UNPKG

@wix/design-system

Version:

@wix/design-system

34 lines 1.26 kB
import { useLayoutEffect, useRef } from 'react'; export const useStackShift = ({ itemKeys, itemRefs, enabled, duration, easing, }) => { const offsets = useRef(new Map()); useLayoutEffect(() => { const shifts = []; itemRefs.current.forEach((ref, key) => { const node = ref.current; if (!node) { itemRefs.current.delete(key); offsets.current.delete(key); return; } const previous = offsets.current.get(key); const current = node.offsetTop; offsets.current.set(key, current); if (previous === undefined || !enabled || typeof node.animate !== 'function') { return; } const deltaY = previous - current; if (deltaY) { shifts.push({ node, deltaY }); } }); shifts.forEach(({ node, deltaY }) => { node.animate([ { transform: `translateY(${deltaY}px)` }, { transform: 'translateY(0)' }, ], { duration, easing }); }); }, [itemKeys, itemRefs, enabled, duration, easing]); }; //# sourceMappingURL=useStackShift.js.map