@wix/design-system
Version:
@wix/design-system
45 lines • 1.68 kB
JavaScript
const wrapAroundValue = (val, max) => ((val % max) + max) % max;
const hardBoundedValue = (val, max) => Math.max(0, Math.min(max, val));
export const normalizeIndex = (idx, len, wrap = false) => wrap ? wrapAroundValue(idx, len) : hardBoundedValue(idx, len - 1);
export const nop = () => { };
export const easeOutQuint = (t) => {
let n = t;
return 1 + --n * n ** 4;
};
const fakeChild = {
getBoundingClientRect: () => ({ left: -Infinity, right: -Infinity }),
};
export const isWhollyInView = (parent) => (child = fakeChild) => {
const { left: childLeft, right: childRight } = child.getBoundingClientRect();
const { left: parentLeft, right: parentRight } = parent.getBoundingClientRect();
// 5px threshold
return childLeft >= parentLeft - 5 && childRight <= parentRight + 5;
};
export const animate = (el, { delta = 0, immediate = false, duration = 500, easing = easeOutQuint, prop = 'scrollTop', }) => new Promise(res => {
if (!delta) {
return res();
}
const initialVal = el[prop];
if (immediate) {
el[prop] = initialVal + delta;
return res();
}
let startTime;
const step = (timestamp) => {
if (!startTime) {
startTime = timestamp;
}
const progressTime = timestamp - startTime;
const progressRatio = easing(progressTime / duration);
el[prop] = initialVal + delta * progressRatio;
if (progressTime < duration) {
window.requestAnimationFrame(step);
}
else {
el[prop] = initialVal + delta;
res();
}
};
window.requestAnimationFrame(step);
});
//# sourceMappingURL=utils.js.map