masonic
Version:
<hr> <div align="center"> <h1 align="center"> 🧱 masonic </h1>
53 lines (44 loc) • 3.23 kB
JavaScript
exports.__esModule = true;
exports.useScroller = useScroller;
var _requestTimeout = /*#__PURE__*/require("@essentials/request-timeout");
var _windowScroll = /*#__PURE__*/_interopRequireDefault( /*#__PURE__*/require("@react-hook/window-scroll"));
var React = /*#__PURE__*/_interopRequireWildcard( /*#__PURE__*/require("react"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* A hook for tracking whether the `window` is currently being scrolled and it's scroll position on
* the y-axis. These values are used for determining which grid cells to render and when
* to add styles to the masonry container that maximize scroll performance.
*
* @param offset - The vertical space in pixels between the top of the grid container and the top
* of the browser `document.documentElement`.
* @param fps - This determines how often (in frames per second) to update the scroll position of the
* browser `window` in state, and as a result the rate the masonry grid recalculates its visible cells.
* The default value of `12` has been very reasonable in my own testing, but if you have particularly
* heavy `render` components it may be prudent to reduce this number.
*/
function useScroller(offset = 0, fps = 12) {
const scrollTop = (0, _windowScroll.default)(fps);
const [isScrolling, setIsScrolling] = React.useState(false);
const didMount = React.useRef(0);
React.useEffect(() => {
if (didMount.current === 1) setIsScrolling(true);
let didUnsubscribe = false;
const to = (0, _requestTimeout.requestTimeout)(() => {
if (didUnsubscribe) return; // This is here to prevent premature bail outs while maintaining high resolution
// unsets. Without it there will always bee a lot of unnecessary DOM writes to style.
setIsScrolling(false);
}, 40 + 1000 / fps);
didMount.current = 1;
return () => {
didUnsubscribe = true;
(0, _requestTimeout.clearRequestTimeout)(to);
};
}, [fps, scrollTop]);
return {
scrollTop: Math.max(0, scrollTop - offset),
isScrolling
};
}
;