UNPKG

wix-style-react

Version:
122 lines (93 loc) 3.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.animate = exports.isWhollyInView = exports.easeOutQuint = exports.nop = exports.values = exports.normalizeIndex = exports.includes = void 0; var includes = function includes(val, arr) { return arr.includes ? arr.includes(val) : !!arr.filter(function (item) { return item === val; }).length; }; exports.includes = includes; 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)); }; 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); }; exports.normalizeIndex = normalizeIndex; var values = Object.values || function (obj) { return Object.keys(obj).map(function (key) { return obj[key]; }); }; exports.values = values; var nop = function nop() {}; exports.nop = nop; var easeOutQuint = function easeOutQuint(t) { var n = t; return 1 + --n * Math.pow(n, 4); }; exports.easeOutQuint = easeOutQuint; var fakeChild = { getBoundingClientRect: function getBoundingClientRect() { return {}; } }; 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; }; }; exports.isWhollyInView = isWhollyInView; 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); }); }; exports.animate = animate;