wix-style-react
Version:
wix-style-react
73 lines (72 loc) • 2.38 kB
JavaScript
;
exports.__esModule = true;
exports.values = exports.normalizeIndex = exports.nop = exports.isWhollyInView = exports.includes = exports.easeOutQuint = exports.animate = void 0;
var includes = (val, arr) => arr.includes ? arr.includes(val) : !!arr.filter(item => item === val).length;
exports.includes = includes;
var wrapAroundValue = (val, max) => (val % max + max) % max;
var hardBoundedValue = (val, max) => Math.max(0, Math.min(max, val));
var normalizeIndex = exports.normalizeIndex = function normalizeIndex(idx, len) {
var wrap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return wrap ? wrapAroundValue(idx, len) : hardBoundedValue(idx, len - 1);
};
var values = exports.values = Object.values || (obj => Object.keys(obj).map(key => obj[key]));
var nop = () => {};
exports.nop = nop;
var easeOutQuint = t => {
var n = t;
return 1 + --n * n ** 4;
};
exports.easeOutQuint = easeOutQuint;
var fakeChild = {
getBoundingClientRect: () => ({})
};
var isWhollyInView = parent => function () {
var child = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : fakeChild;
var {
left: cLeft,
right: cRight
} = child.getBoundingClientRect();
var {
left: pLeft,
right: pRight
} = parent.getBoundingClientRect();
// 5px threshold
return cLeft >= pLeft - 5 && cRight <= pRight + 5;
};
exports.isWhollyInView = isWhollyInView;
var animate = exports.animate = function animate(el) {
var {
delta = 0,
immediate = false,
duration = 500,
easing = easeOutQuint,
prop = 'scrollTop'
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new Promise(res => {
if (!delta) {
return res();
}
var initialVal = el[prop];
if (immediate) {
el[prop] = initialVal + delta;
return res();
}
var startTime = null;
var step = timestamp => {
if (!startTime) {
startTime = timestamp;
}
var progressTime = timestamp - startTime;
var 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