@wix/design-system
Version:
@wix/design-system
75 lines (74 loc) • 2.59 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 _child$getBoundingCli = child.getBoundingClientRect(),
childLeft = _child$getBoundingCli.left,
childRight = _child$getBoundingCli.right;
var _parent$getBoundingCl = parent.getBoundingClientRect(),
parentLeft = _parent$getBoundingCl.left,
parentRight = _parent$getBoundingCl.right;
// 5px threshold
return childLeft >= parentLeft - 5 && childRight <= parentRight + 5;
};
exports.isWhollyInView = isWhollyInView;
var animate = (el, _ref) => {
var _ref$delta = _ref.delta,
delta = _ref$delta === void 0 ? 0 : _ref$delta,
_ref$immediate = _ref.immediate,
immediate = _ref$immediate === void 0 ? false : _ref$immediate,
_ref$duration = _ref.duration,
duration = _ref$duration === void 0 ? 500 : _ref$duration,
_ref$easing = _ref.easing,
easing = _ref$easing === void 0 ? easeOutQuint : _ref$easing,
_ref$prop = _ref.prop,
prop = _ref$prop === void 0 ? 'scrollTop' : _ref$prop;
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