wix-style-react
Version:
96 lines (81 loc) • 2.87 kB
JavaScript
export var includes = function includes(val, arr) {
return arr.includes ? arr.includes(val) : !!arr.filter(function (item) {
return item === val;
}).length;
};
var wrapAroundValue = function wrapAroundValue(val, max) {
return (val % max + max) % max;
};
var hardBoundedValue = function hardBoundedValue(val, max) {
return Math.max(0, Math.min(max, val));
};
export var 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);
};
export var values = Object.values || function (obj) {
return Object.keys(obj).map(function (key) {
return obj[key];
});
};
export var nop = function nop() {};
export var easeOutQuint = function easeOutQuint(t) {
var n = t;
return 1 + --n * Math.pow(n, 4);
};
var fakeChild = {
getBoundingClientRect: function getBoundingClientRect() {
return {};
}
};
export var isWhollyInView = function isWhollyInView(parent) {
return function () {
var child = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : fakeChild;
var _child$getBoundingCli = child.getBoundingClientRect(),
cLeft = _child$getBoundingCli.left,
cRight = _child$getBoundingCli.right;
var _parent$getBoundingCl = parent.getBoundingClientRect(),
pLeft = _parent$getBoundingCl.left,
pRight = _parent$getBoundingCl.right; // 5px threshold
return cLeft >= pLeft - 5 && cRight <= pRight + 5;
};
};
export var animate = function animate(el) {
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
_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(function (res) {
if (!delta) {
return res();
}
var initialVal = el[prop];
if (immediate) {
el[prop] = initialVal + delta;
return res();
}
var startTime = null;
var step = function 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);
});
};