@arolariu/components
Version:
🎨 70+ beautiful, accessible React components built on Base UI. TypeScript-first, CSS Modules styling, tree-shakeable, SSR-ready. Perfect for modern web apps, design systems & rapid prototyping. Zero config, maximum flexibility! ⚡
34 lines (33 loc) • 1.23 kB
JavaScript
"use client";
import * as __rspack_external_react from "react";
function useThrottle(callback, delay) {
const lastRunRef = __rspack_external_react.useRef(0);
const timeoutRef = __rspack_external_react.useRef(void 0);
const callbackRef = __rspack_external_react.useRef(callback);
__rspack_external_react.useEffect(()=>{
callbackRef.current = callback;
}, [
callback
]);
__rspack_external_react.useEffect(()=>()=>{
if (timeoutRef.current) globalThis.clearTimeout(timeoutRef.current);
}, []);
return __rspack_external_react.useCallback((...args)=>{
const now = Date.now();
const timeSinceLastRun = now - lastRunRef.current;
if (timeSinceLastRun >= delay) {
callbackRef.current(...args);
lastRunRef.current = now;
} else {
if (timeoutRef.current) globalThis.clearTimeout(timeoutRef.current);
timeoutRef.current = globalThis.setTimeout(()=>{
callbackRef.current(...args);
lastRunRef.current = Date.now();
}, delay - timeSinceLastRun);
}
}, [
delay
]);
}
export { useThrottle };
//# sourceMappingURL=useThrottle.js.map