@usefy/use-throttle 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +56 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +14 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for useThrottle hook
|
|
3
|
+
*/
|
|
4
|
+
interface UseThrottleOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether to update the throttled value on the leading edge
|
|
7
|
+
* @default true
|
|
8
|
+
*/
|
|
9
|
+
leading?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to update the throttled value on the trailing edge
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
trailing?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Throttles a value by limiting updates to at most once per specified interval.
|
|
18
|
+
* This is implemented using useDebounce with maxWait set to the interval.
|
|
19
|
+
*
|
|
20
|
+
* @template T - The type of the value to throttle
|
|
21
|
+
* @param value - The value to throttle
|
|
22
|
+
* @param delay - The interval in milliseconds (default: 500ms)
|
|
23
|
+
* @param options - Additional options for controlling throttle behavior
|
|
24
|
+
* @returns The throttled value
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* function ScrollComponent() {
|
|
29
|
+
* const [scrollY, setScrollY] = useState(0);
|
|
30
|
+
* const throttledScrollY = useThrottle(scrollY, 100);
|
|
31
|
+
*
|
|
32
|
+
* useEffect(() => {
|
|
33
|
+
* const handleScroll = () => setScrollY(window.scrollY);
|
|
34
|
+
* window.addEventListener("scroll", handleScroll);
|
|
35
|
+
* return () => window.removeEventListener("scroll", handleScroll);
|
|
36
|
+
* }, []);
|
|
37
|
+
*
|
|
38
|
+
* return <div>Scroll Y: {throttledScrollY}</div>;
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* // With leading edge only
|
|
45
|
+
* const throttledValue = useThrottle(value, 300, { leading: true, trailing: false });
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* // With trailing edge only
|
|
51
|
+
* const throttledValue = useThrottle(value, 300, { leading: false, trailing: true });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare function useThrottle<T>(value: T, delay?: number, options?: UseThrottleOptions): T;
|
|
55
|
+
|
|
56
|
+
export { type UseThrottleOptions, useThrottle };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for useThrottle hook
|
|
3
|
+
*/
|
|
4
|
+
interface UseThrottleOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether to update the throttled value on the leading edge
|
|
7
|
+
* @default true
|
|
8
|
+
*/
|
|
9
|
+
leading?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Whether to update the throttled value on the trailing edge
|
|
12
|
+
* @default true
|
|
13
|
+
*/
|
|
14
|
+
trailing?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Throttles a value by limiting updates to at most once per specified interval.
|
|
18
|
+
* This is implemented using useDebounce with maxWait set to the interval.
|
|
19
|
+
*
|
|
20
|
+
* @template T - The type of the value to throttle
|
|
21
|
+
* @param value - The value to throttle
|
|
22
|
+
* @param delay - The interval in milliseconds (default: 500ms)
|
|
23
|
+
* @param options - Additional options for controlling throttle behavior
|
|
24
|
+
* @returns The throttled value
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* function ScrollComponent() {
|
|
29
|
+
* const [scrollY, setScrollY] = useState(0);
|
|
30
|
+
* const throttledScrollY = useThrottle(scrollY, 100);
|
|
31
|
+
*
|
|
32
|
+
* useEffect(() => {
|
|
33
|
+
* const handleScroll = () => setScrollY(window.scrollY);
|
|
34
|
+
* window.addEventListener("scroll", handleScroll);
|
|
35
|
+
* return () => window.removeEventListener("scroll", handleScroll);
|
|
36
|
+
* }, []);
|
|
37
|
+
*
|
|
38
|
+
* return <div>Scroll Y: {throttledScrollY}</div>;
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```tsx
|
|
44
|
+
* // With leading edge only
|
|
45
|
+
* const throttledValue = useThrottle(value, 300, { leading: true, trailing: false });
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* // With trailing edge only
|
|
51
|
+
* const throttledValue = useThrottle(value, 300, { leading: false, trailing: true });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
declare function useThrottle<T>(value: T, delay?: number, options?: UseThrottleOptions): T;
|
|
55
|
+
|
|
56
|
+
export { type UseThrottleOptions, useThrottle };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
useThrottle: () => useThrottle
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/useThrottle.ts
|
|
28
|
+
var import_use_debounce = require("@usefy/use-debounce");
|
|
29
|
+
function useThrottle(value, delay = 500, options = {}) {
|
|
30
|
+
const { leading = true, trailing = true } = options;
|
|
31
|
+
return (0, import_use_debounce.useDebounce)(value, delay, {
|
|
32
|
+
leading,
|
|
33
|
+
maxWait: delay,
|
|
34
|
+
trailing
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
useThrottle
|
|
40
|
+
});
|
|
41
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/useThrottle.ts"],"sourcesContent":["export { useThrottle, type UseThrottleOptions } from \"./useThrottle\";\r\n","import { useDebounce, type UseDebounceOptions } from \"@usefy/use-debounce\";\r\n\r\n/**\r\n * Options for useThrottle hook\r\n */\r\nexport interface UseThrottleOptions {\r\n /**\r\n * Whether to update the throttled value on the leading edge\r\n * @default true\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to update the throttled value on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Throttles a value by limiting updates to at most once per specified interval.\r\n * This is implemented using useDebounce with maxWait set to the interval.\r\n *\r\n * @template T - The type of the value to throttle\r\n * @param value - The value to throttle\r\n * @param delay - The interval in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling throttle behavior\r\n * @returns The throttled value\r\n *\r\n * @example\r\n * ```tsx\r\n * function ScrollComponent() {\r\n * const [scrollY, setScrollY] = useState(0);\r\n * const throttledScrollY = useThrottle(scrollY, 100);\r\n *\r\n * useEffect(() => {\r\n * const handleScroll = () => setScrollY(window.scrollY);\r\n * window.addEventListener(\"scroll\", handleScroll);\r\n * return () => window.removeEventListener(\"scroll\", handleScroll);\r\n * }, []);\r\n *\r\n * return <div>Scroll Y: {throttledScrollY}</div>;\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge only\r\n * const throttledValue = useThrottle(value, 300, { leading: true, trailing: false });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With trailing edge only\r\n * const throttledValue = useThrottle(value, 300, { leading: false, trailing: true });\r\n * ```\r\n */\r\nexport function useThrottle<T>(\r\n value: T,\r\n delay: number = 500,\r\n options: UseThrottleOptions = {}\r\n): T {\r\n const { leading = true, trailing = true } = options;\r\n\r\n // Throttle is implemented using debounce with maxWait set to delay\r\n return useDebounce(value, delay, {\r\n leading,\r\n maxWait: delay,\r\n trailing,\r\n });\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,0BAAqD;AAwD9C,SAAS,YACd,OACA,QAAgB,KAChB,UAA8B,CAAC,GAC5B;AACH,QAAM,EAAE,UAAU,MAAM,WAAW,KAAK,IAAI;AAG5C,aAAO,iCAAY,OAAO,OAAO;AAAA,IAC/B;AAAA,IACA,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// src/useThrottle.ts
|
|
2
|
+
import { useDebounce } from "@usefy/use-debounce";
|
|
3
|
+
function useThrottle(value, delay = 500, options = {}) {
|
|
4
|
+
const { leading = true, trailing = true } = options;
|
|
5
|
+
return useDebounce(value, delay, {
|
|
6
|
+
leading,
|
|
7
|
+
maxWait: delay,
|
|
8
|
+
trailing
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
useThrottle
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/useThrottle.ts"],"sourcesContent":["import { useDebounce, type UseDebounceOptions } from \"@usefy/use-debounce\";\r\n\r\n/**\r\n * Options for useThrottle hook\r\n */\r\nexport interface UseThrottleOptions {\r\n /**\r\n * Whether to update the throttled value on the leading edge\r\n * @default true\r\n */\r\n leading?: boolean;\r\n /**\r\n * Whether to update the throttled value on the trailing edge\r\n * @default true\r\n */\r\n trailing?: boolean;\r\n}\r\n\r\n/**\r\n * Throttles a value by limiting updates to at most once per specified interval.\r\n * This is implemented using useDebounce with maxWait set to the interval.\r\n *\r\n * @template T - The type of the value to throttle\r\n * @param value - The value to throttle\r\n * @param delay - The interval in milliseconds (default: 500ms)\r\n * @param options - Additional options for controlling throttle behavior\r\n * @returns The throttled value\r\n *\r\n * @example\r\n * ```tsx\r\n * function ScrollComponent() {\r\n * const [scrollY, setScrollY] = useState(0);\r\n * const throttledScrollY = useThrottle(scrollY, 100);\r\n *\r\n * useEffect(() => {\r\n * const handleScroll = () => setScrollY(window.scrollY);\r\n * window.addEventListener(\"scroll\", handleScroll);\r\n * return () => window.removeEventListener(\"scroll\", handleScroll);\r\n * }, []);\r\n *\r\n * return <div>Scroll Y: {throttledScrollY}</div>;\r\n * }\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With leading edge only\r\n * const throttledValue = useThrottle(value, 300, { leading: true, trailing: false });\r\n * ```\r\n *\r\n * @example\r\n * ```tsx\r\n * // With trailing edge only\r\n * const throttledValue = useThrottle(value, 300, { leading: false, trailing: true });\r\n * ```\r\n */\r\nexport function useThrottle<T>(\r\n value: T,\r\n delay: number = 500,\r\n options: UseThrottleOptions = {}\r\n): T {\r\n const { leading = true, trailing = true } = options;\r\n\r\n // Throttle is implemented using debounce with maxWait set to delay\r\n return useDebounce(value, delay, {\r\n leading,\r\n maxWait: delay,\r\n trailing,\r\n });\r\n}\r\n"],"mappings":";AAAA,SAAS,mBAA4C;AAwD9C,SAAS,YACd,OACA,QAAgB,KAChB,UAA8B,CAAC,GAC5B;AACH,QAAM,EAAE,UAAU,MAAM,WAAW,KAAK,IAAI;AAG5C,SAAO,YAAY,OAAO,OAAO;AAAA,IAC/B;AAAA,IACA,SAAS;AAAA,IACT;AAAA,EACF,CAAC;AACH;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@usefy/use-throttle",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "A React hook for throttling values",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@usefy/use-debounce": "0.0.2"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
27
|
+
"@testing-library/react": "^16.3.1",
|
|
28
|
+
"@testing-library/user-event": "^14.6.1",
|
|
29
|
+
"@types/react": "^19.0.0",
|
|
30
|
+
"jsdom": "^27.3.0",
|
|
31
|
+
"react": "^19.0.0",
|
|
32
|
+
"rimraf": "^6.0.1",
|
|
33
|
+
"tsup": "^8.0.0",
|
|
34
|
+
"typescript": "^5.0.0",
|
|
35
|
+
"vitest": "^4.0.16"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "https://github.com/geon0529/usefy.git",
|
|
43
|
+
"directory": "packages/use-throttle"
|
|
44
|
+
},
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"keywords": [
|
|
47
|
+
"react",
|
|
48
|
+
"hooks",
|
|
49
|
+
"throttle",
|
|
50
|
+
"rate-limit"
|
|
51
|
+
],
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsup",
|
|
54
|
+
"dev": "tsup --watch",
|
|
55
|
+
"test": "vitest run",
|
|
56
|
+
"test:watch": "vitest",
|
|
57
|
+
"typecheck": "tsc --noEmit",
|
|
58
|
+
"clean": "rimraf dist"
|
|
59
|
+
}
|
|
60
|
+
}
|