"use strict";
/*
 *  This file is part of CoCalc: Copyright © 2022 Sagemath, Inc.
 *  License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
 */
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useScrollY = void 0;
var lodash_1 = require("lodash");
var react_1 = require("react");
// dynamically get the Y position of the window scroll
function useScrollY() {
    var _a = __read((0, react_1.useState)(0), 2), scrollY = _a[0], setScrollY = _a[1];
    // callback is important, since we want to reain the same reference
    var onScroll = (0, react_1.useCallback)((0, lodash_1.throttle)(function (_event) {
        setScrollY(window.pageYOffset);
    }, 100, { trailing: true }), []);
    (0, react_1.useEffect)(function () {
        // passive: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#improving_scrolling_performance_with_passive_listeners
        window.addEventListener("scroll", onScroll, { passive: true });
        return function () { return window.removeEventListener("scroll", onScroll); };
    }, []);
    return scrollY;
}
exports.useScrollY = useScrollY;
//# sourceMappingURL=use-scroll-y.js.map