@reactuses/core 1.0.1 → 1.0.3

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.
Files changed (6) hide show
  1. package/LICENSE +24 -24
  2. package/README.md +81 -73
  3. package/index.cjs +152 -0
  4. package/index.d.ts +546 -445
  5. package/index.mjs +151 -1
  6. package/package.json +2 -2
package/LICENSE CHANGED
@@ -1,24 +1,24 @@
1
- This is free and unencumbered software released into the public domain.
2
-
3
- Anyone is free to copy, modify, publish, use, compile, sell, or
4
- distribute this software, either in source code form or as a compiled
5
- binary, for any purpose, commercial or non-commercial, and by any
6
- means.
7
-
8
- In jurisdictions that recognize copyright laws, the author or authors
9
- of this software dedicate any and all copyright interest in the
10
- software to the public domain. We make this dedication for the benefit
11
- of the public at large and to the detriment of our heirs and
12
- successors. We intend this dedication to be an overt act of
13
- relinquishment in perpetuity of all present and future rights to this
14
- software under copyright law.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
- IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
- OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
- OTHER DEALINGS IN THE SOFTWARE.
23
-
24
- For more information, please refer to <http://unlicense.org/>
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
package/README.md CHANGED
@@ -1,73 +1,81 @@
1
- <div align = "center">
2
- <h1 align = "center">
3
- reactuse
4
- </h1>
5
- </div>
6
-
7
- <p align="center">
8
- <a href="https://bundlephobia.com/result?p=@reactuses/core">
9
- <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@reactuses/core?style=for-the-badge&labelColor=24292e">
10
- </a>
11
- <a aria-label="Types" href="https://www.npmjs.com/package/@reactuses/core">
12
- <img alt="Types" src="https://img.shields.io/npm/types/react-use-system-color-mode?style=for-the-badge&labelColor=24292e">
13
- </a>
14
- <a aria-label="NPM version" href="https://www.npmjs.com/package/@reactuses/core">
15
- <img alt="NPM Version" src="https://img.shields.io/npm/v/@reactuses/core?style=for-the-badge&labelColor=24292e">
16
- </a>
17
- <a aria-label="License" href="https://jaredlunde.mit-license.org/">
18
- <img alt="UnLicense" src="https://img.shields.io/npm/l/@reactuses/core?style=for-the-badge&labelColor=24292e">
19
- </a>
20
- </p>
21
-
22
- <pre align="center">npm i @reactuses/core</pre>
23
-
24
- <p align="center">
25
- Collection of essential React Hooks Utilities.
26
- </p>
27
-
28
- <hr>
29
-
30
- ## QuickStart
31
-
32
- ```tsx harmony
33
- import { useToggle } from "@reactuses/core";
34
-
35
- const Demo = () => {
36
- const [on, toggle] = useToggle(true);
37
-
38
- return (
39
- <div>
40
- <div>{on ? "ON" : "OFF"}</div>
41
- <button onClick={toggle}>Toggle</button>
42
- <button onClick={() => toggle(true)}>set ON</button>
43
- <button onClick={() => toggle(false)}>set OFF</button>
44
- </div>
45
- );
46
- };
47
- ```
48
-
49
- Refer to [documentations](https://reactuse.com/) for more details.
50
-
51
- <hr>
52
-
53
- ## Documentation & Live Examples
54
-
55
- * [Documentation](https://reactuse.com/)
56
-
57
- <hr/>
58
-
59
- ## Contribute
60
-
61
- See the [**Contributing Guide**](CONTRIBUTING.md)
62
-
63
- <hr/>
64
-
65
- ## Thanks
66
-
67
- This project is heavily inspired by the following awesome projects.
68
-
69
- * [streamich/react-use](https://github.com/streamich/react-use)
70
- * [ahooks](https://github.com/alibaba/hooks)
71
- * [vueuse](https://github.com/vueuse/vueuse)
72
-
73
- <hr/>
1
+ <div align = "center">
2
+ <h1 align = "center">
3
+ reactuse
4
+ </h1>
5
+ </div>
6
+
7
+ <p align="center">
8
+ <a href="https://bundlephobia.com/result?p=@reactuses/core">
9
+ <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@reactuses/core?style=for-the-badge&labelColor=24292e">
10
+ </a>
11
+ <a aria-label="Types" href="https://www.npmjs.com/package/@reactuses/core">
12
+ <img alt="Types" src="https://img.shields.io/npm/types/react-use-system-color-mode?style=for-the-badge&labelColor=24292e">
13
+ </a>
14
+ <a aria-label="NPM version" href="https://www.npmjs.com/package/@reactuses/core">
15
+ <img alt="NPM Version" src="https://img.shields.io/npm/v/@reactuses/core?style=for-the-badge&labelColor=24292e">
16
+ </a>
17
+ <a aria-label="License" href="https://jaredlunde.mit-license.org/">
18
+ <img alt="UnLicense" src="https://img.shields.io/npm/l/@reactuses/core?style=for-the-badge&labelColor=24292e">
19
+ </a>
20
+ </p>
21
+
22
+ <pre align="center">npm i @reactuses/core</pre>
23
+
24
+ <p align="center">
25
+ Collection of essential React Hooks Utilities.
26
+ </p>
27
+
28
+ <hr>
29
+
30
+ ## QuickStart
31
+
32
+ ```tsx harmony
33
+ import { useToggle } from "@reactuses/core";
34
+
35
+ const Demo = () => {
36
+ const [on, toggle] = useToggle(true);
37
+
38
+ return (
39
+ <div>
40
+ <div>{on ? "ON" : "OFF"}</div>
41
+ <button onClick={toggle}>Toggle</button>
42
+ <button onClick={() => toggle(true)}>set ON</button>
43
+ <button onClick={() => toggle(false)}>set OFF</button>
44
+ </div>
45
+ );
46
+ };
47
+ ```
48
+
49
+ Refer to [documentations](https://reactuse.com/) for more details.
50
+
51
+ <hr>
52
+
53
+ ## Documentation & Live Examples
54
+
55
+ * [Documentation](https://reactuse.com/)
56
+
57
+ <hr/>
58
+
59
+ ## Contribute
60
+
61
+ See the [**Contributing Guide**](CONTRIBUTING.md)
62
+
63
+ <hr/>
64
+
65
+ ## Idea
66
+
67
+ We want to keep a single hook function as simple as possible.
68
+
69
+ * For data fetch, we recommend use [swr](https://www.npmjs.com/package/swr)
70
+
71
+ * For keyboard shortcuts, we recommend use [react-hotkeys-hook](https://www.npmjs.com/package/react-hotkeys-hook)
72
+
73
+ ## Thanks
74
+
75
+ This project is heavily inspired by the following awesome projects.
76
+
77
+ * [streamich/react-use](https://github.com/streamich/react-use)
78
+ * [ahooks](https://github.com/alibaba/hooks)
79
+ * [vueuse](https://github.com/vueuse/vueuse)
80
+
81
+ <hr/>
package/index.cjs CHANGED
@@ -2148,6 +2148,156 @@ function useActiveElement() {
2148
2148
  return active;
2149
2149
  }
2150
2150
 
2151
+ function useDraggable(target, options = {}) {
2152
+ var _a, _b;
2153
+ const draggingElement = getTargetElement(options.draggingElement, window);
2154
+ const draggingHandle = getTargetElement((_a = options.handle) != null ? _a : target);
2155
+ const [position, setPositon] = react.useState(
2156
+ (_b = options.initialValue) != null ? _b : { x: 0, y: 0 }
2157
+ );
2158
+ const [pressedDelta, setPressedDelta] = react.useState();
2159
+ const filterEvent = (e) => {
2160
+ if (options.pointerTypes) {
2161
+ return options.pointerTypes.includes(e.pointerType);
2162
+ }
2163
+ return true;
2164
+ };
2165
+ const handleEvent = (e) => {
2166
+ if (options.preventDefault) {
2167
+ e.preventDefault();
2168
+ }
2169
+ if (options.stopPropagation) {
2170
+ e.stopPropagation();
2171
+ }
2172
+ };
2173
+ const start = (e) => {
2174
+ var _a2;
2175
+ const element = getTargetElement(target);
2176
+ if (!filterEvent(e) || !element) {
2177
+ return;
2178
+ }
2179
+ if (options.exact && e.target !== element) {
2180
+ return;
2181
+ }
2182
+ const rect = element.getBoundingClientRect();
2183
+ const pos = {
2184
+ x: e.pageX - rect.left,
2185
+ y: e.pageY - rect.top
2186
+ };
2187
+ if (((_a2 = options.onStart) == null ? void 0 : _a2.call(options, pos, e)) === false) {
2188
+ return;
2189
+ }
2190
+ setPressedDelta(pos);
2191
+ handleEvent(e);
2192
+ };
2193
+ const move = (e) => {
2194
+ var _a2;
2195
+ if (!filterEvent(e)) {
2196
+ return;
2197
+ }
2198
+ if (!pressedDelta) {
2199
+ return;
2200
+ }
2201
+ setPositon({
2202
+ x: e.pageX - pressedDelta.x,
2203
+ y: e.pageY - pressedDelta.y
2204
+ });
2205
+ (_a2 = options.onMove) == null ? void 0 : _a2.call(options, position, e);
2206
+ handleEvent(e);
2207
+ };
2208
+ const end = (e) => {
2209
+ var _a2;
2210
+ if (!filterEvent(e)) {
2211
+ return;
2212
+ }
2213
+ if (!pressedDelta) {
2214
+ return;
2215
+ }
2216
+ setPressedDelta(void 0);
2217
+ (_a2 = options.onEnd) == null ? void 0 : _a2.call(options, position, e);
2218
+ handleEvent(e);
2219
+ };
2220
+ useEventListener("pointerdown", start, draggingHandle, true);
2221
+ useEventListener("pointermove", move, draggingElement, true);
2222
+ useEventListener("pointerup", end, draggingElement, true);
2223
+ return [position.x, position.y, !!pressedDelta];
2224
+ }
2225
+
2226
+ function useElementBounding(target, options = {}) {
2227
+ const {
2228
+ reset = true,
2229
+ windowResize = true,
2230
+ windowScroll = true,
2231
+ immediate = true
2232
+ } = options;
2233
+ const [height, setHeight] = react.useState(0);
2234
+ const [bottom, setBottom] = react.useState(0);
2235
+ const [left, setLeft] = react.useState(0);
2236
+ const [right, setRight] = react.useState(0);
2237
+ const [top, setTop] = react.useState(0);
2238
+ const [width, setWidth] = react.useState(0);
2239
+ const [x, setX] = react.useState(0);
2240
+ const [y, setY] = react.useState(0);
2241
+ const update = useEvent(() => {
2242
+ const element = getTargetElement(target);
2243
+ if (!element) {
2244
+ if (reset) {
2245
+ setHeight(0);
2246
+ setBottom(0);
2247
+ setLeft(0);
2248
+ setRight(0);
2249
+ setTop(0);
2250
+ setWidth(0);
2251
+ setX(0);
2252
+ setY(0);
2253
+ }
2254
+ return;
2255
+ }
2256
+ const rect = element.getBoundingClientRect();
2257
+ setHeight(rect.height);
2258
+ setBottom(rect.bottom);
2259
+ setLeft(rect.left);
2260
+ setRight(rect.right);
2261
+ setTop(rect.top);
2262
+ setWidth(rect.width);
2263
+ setX(rect.x);
2264
+ setY(rect.y);
2265
+ });
2266
+ useResizeObserver(target, update);
2267
+ react.useEffect(() => {
2268
+ if (immediate) {
2269
+ update();
2270
+ }
2271
+ }, [immediate, update]);
2272
+ react.useEffect(() => {
2273
+ if (windowScroll) {
2274
+ window.addEventListener("scroll", update, { passive: true });
2275
+ }
2276
+ if (windowResize) {
2277
+ window.addEventListener("resize", update, { passive: true });
2278
+ }
2279
+ return () => {
2280
+ if (windowScroll) {
2281
+ window.removeEventListener("scroll", update);
2282
+ }
2283
+ if (windowResize) {
2284
+ window.removeEventListener("resize", update);
2285
+ }
2286
+ };
2287
+ }, [update, windowResize, windowScroll]);
2288
+ return {
2289
+ height,
2290
+ bottom,
2291
+ left,
2292
+ right,
2293
+ top,
2294
+ width,
2295
+ x,
2296
+ y,
2297
+ update
2298
+ };
2299
+ }
2300
+
2151
2301
  exports.useActiveElement = useActiveElement;
2152
2302
  exports.useCounter = useCounter;
2153
2303
  exports.useCustomCompareEffect = useCustomCompareEffect;
@@ -2156,7 +2306,9 @@ exports.useDebounce = useDebounce;
2156
2306
  exports.useDebounceFn = useDebounceFn;
2157
2307
  exports.useDeepCompareEffect = useDeepCompareEffect;
2158
2308
  exports.useDocumentVisibility = useDocumentVisibility;
2309
+ exports.useDraggable = useDraggable;
2159
2310
  exports.useDropZone = useDropZone;
2311
+ exports.useElementBounding = useElementBounding;
2160
2312
  exports.useElementSize = useElementSize;
2161
2313
  exports.useEvent = useEvent;
2162
2314
  exports.useEventEmitter = useEventEmitter;