Files
NapCatQQ/.pnpm-store/v3/files/e3/0b9c35f9684e53030d3f8b2742b15e3ebe27dc0bd24b1f4e7effb094634ad101a02648f987dfc4bd4a66c023045bc7c9a3829eef6db3b1c397967e202cc5af
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

79 lines
2.8 KiB
Plaintext

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
useScrollPosition: () => useScrollPosition
});
module.exports = __toCommonJS(src_exports);
var import_react = require("react");
var isBrowser = typeof window !== "undefined";
function getScrollPosition(element) {
if (!isBrowser)
return { x: 0, y: 0 };
if (!element) {
return { x: window.scrollX, y: window.scrollY };
}
return { x: element.scrollLeft, y: element.scrollTop };
}
var useScrollPosition = (props) => {
const { elementRef, delay = 30, callback, isEnabled } = props;
const position = (0, import_react.useRef)(
isEnabled ? getScrollPosition(elementRef == null ? void 0 : elementRef.current) : { x: 0, y: 0 }
);
const throttleTimeout = (0, import_react.useRef)(null);
const handler = (0, import_react.useCallback)(() => {
const currPos = getScrollPosition(elementRef == null ? void 0 : elementRef.current);
if (typeof callback === "function") {
callback({ prevPos: position.current, currPos });
}
position.current = currPos;
throttleTimeout.current = null;
}, [callback, elementRef]);
(0, import_react.useEffect)(() => {
if (!isEnabled)
return;
const handleScroll = () => {
if (delay) {
if (throttleTimeout.current) {
clearTimeout(throttleTimeout.current);
}
throttleTimeout.current = setTimeout(handler, delay);
} else {
handler();
}
};
const target = (elementRef == null ? void 0 : elementRef.current) || window;
target.addEventListener("scroll", handleScroll);
return () => {
target.removeEventListener("scroll", handleScroll);
if (throttleTimeout.current) {
clearTimeout(throttleTimeout.current);
throttleTimeout.current = null;
}
};
}, [elementRef == null ? void 0 : elementRef.current, delay, handler, isEnabled]);
return position.current;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useScrollPosition
});