@wix/design-system
Version:
@wix/design-system
74 lines (73 loc) • 2.13 kB
JavaScript
;
exports.__esModule = true;
exports.normalizeIndex = exports.nop = exports.isWhollyInView = exports.easeOutQuint = exports.animate = void 0;
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 nop = () => {};
exports.nop = nop;
var easeOutQuint = t => {
var n = t;
return 1 + --n * n ** 4;
};
exports.easeOutQuint = easeOutQuint;
var fakeChild = {
getBoundingClientRect: () => ({
left: -Infinity,
right: -Infinity
})
};
var isWhollyInView = parent => function () {
var child = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : fakeChild;
var {
left: childLeft,
right: childRight
} = child.getBoundingClientRect();
var {
left: parentLeft,
right: parentRight
} = parent.getBoundingClientRect();
// 5px threshold
return childLeft >= parentLeft - 5 && childRight <= parentRight + 5;
};
exports.isWhollyInView = isWhollyInView;
var animate = (el, _ref) => {
var {
delta = 0,
immediate = false,
duration = 500,
easing = easeOutQuint,
prop = 'scrollTop'
} = _ref;
return new Promise(res => {
if (!delta) {
return res();
}
var initialVal = el[prop];
if (immediate) {
el[prop] = initialVal + delta;
return res();
}
var startTime;
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);
});
};
exports.animate = animate;
//# sourceMappingURL=utils.js.map