@reactuses/core 1.0.2 → 1.0.4
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/README.md +8 -0
- package/index.cjs +106 -0
- package/index.d.ts +43 -1
- package/index.mjs +104 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,6 +62,14 @@ See the [**Contributing Guide**](CONTRIBUTING.md)
|
|
|
62
62
|
|
|
63
63
|
<hr/>
|
|
64
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
|
+
|
|
65
73
|
## Thanks
|
|
66
74
|
|
|
67
75
|
This project is heavily inspired by the following awesome projects.
|
package/index.cjs
CHANGED
|
@@ -2223,6 +2223,109 @@ function useDraggable(target, options = {}) {
|
|
|
2223
2223
|
return [position.x, position.y, !!pressedDelta];
|
|
2224
2224
|
}
|
|
2225
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
|
+
|
|
2301
|
+
function useElementVisibility(target, options = {}) {
|
|
2302
|
+
const [visible, setVisible] = react.useState(false);
|
|
2303
|
+
const callback = react.useCallback((entries) => {
|
|
2304
|
+
const rect = entries[0].boundingClientRect;
|
|
2305
|
+
setVisible(
|
|
2306
|
+
rect.top <= (window.innerHeight || document.documentElement.clientHeight) && rect.left <= (window.innerWidth || document.documentElement.clientWidth) && rect.bottom >= 0 && rect.right >= 0
|
|
2307
|
+
);
|
|
2308
|
+
}, []);
|
|
2309
|
+
const stop = useIntersectionObserver(target, callback, options);
|
|
2310
|
+
return [visible, stop];
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
function useWindowsFocus() {
|
|
2314
|
+
const [focused, setFocused] = react.useState(() => {
|
|
2315
|
+
if (!window) {
|
|
2316
|
+
return false;
|
|
2317
|
+
}
|
|
2318
|
+
return window.document.hasFocus();
|
|
2319
|
+
});
|
|
2320
|
+
useEventListener("blur", () => {
|
|
2321
|
+
setFocused(false);
|
|
2322
|
+
});
|
|
2323
|
+
useEventListener("focus", () => {
|
|
2324
|
+
setFocused(true);
|
|
2325
|
+
});
|
|
2326
|
+
return focused;
|
|
2327
|
+
}
|
|
2328
|
+
|
|
2226
2329
|
exports.useActiveElement = useActiveElement;
|
|
2227
2330
|
exports.useCounter = useCounter;
|
|
2228
2331
|
exports.useCustomCompareEffect = useCustomCompareEffect;
|
|
@@ -2233,7 +2336,9 @@ exports.useDeepCompareEffect = useDeepCompareEffect;
|
|
|
2233
2336
|
exports.useDocumentVisibility = useDocumentVisibility;
|
|
2234
2337
|
exports.useDraggable = useDraggable;
|
|
2235
2338
|
exports.useDropZone = useDropZone;
|
|
2339
|
+
exports.useElementBounding = useElementBounding;
|
|
2236
2340
|
exports.useElementSize = useElementSize;
|
|
2341
|
+
exports.useElementVisibility = useElementVisibility;
|
|
2237
2342
|
exports.useEvent = useEvent;
|
|
2238
2343
|
exports.useEventEmitter = useEventEmitter;
|
|
2239
2344
|
exports.useEventListener = useEventListener;
|
|
@@ -2289,3 +2394,4 @@ exports.useUpdate = useUpdate;
|
|
|
2289
2394
|
exports.useUpdateEffect = useUpdateEffect;
|
|
2290
2395
|
exports.useUpdateLayoutEffect = useUpdateLayoutEffect;
|
|
2291
2396
|
exports.useVirtualList = useVirtualList;
|
|
2397
|
+
exports.useWindowsFocus = useWindowsFocus;
|
package/index.d.ts
CHANGED
|
@@ -648,4 +648,46 @@ interface UseDraggableOptions {
|
|
|
648
648
|
}
|
|
649
649
|
declare function useDraggable(target: BasicTarget<HTMLElement | SVGElement>, options?: UseDraggableOptions): readonly [number, number, boolean];
|
|
650
650
|
|
|
651
|
-
|
|
651
|
+
interface UseElementBoundingOptions {
|
|
652
|
+
/**
|
|
653
|
+
* Reset values to 0 on component unmounted
|
|
654
|
+
*
|
|
655
|
+
* @default true
|
|
656
|
+
*/
|
|
657
|
+
reset?: boolean;
|
|
658
|
+
/**
|
|
659
|
+
* Listen to window resize event
|
|
660
|
+
*
|
|
661
|
+
* @default true
|
|
662
|
+
*/
|
|
663
|
+
windowResize?: boolean;
|
|
664
|
+
/**
|
|
665
|
+
* Listen to window scroll event
|
|
666
|
+
*
|
|
667
|
+
* @default true
|
|
668
|
+
*/
|
|
669
|
+
windowScroll?: boolean;
|
|
670
|
+
/**
|
|
671
|
+
* Immediately call update on component mounted
|
|
672
|
+
*
|
|
673
|
+
* @default true
|
|
674
|
+
*/
|
|
675
|
+
immediate?: boolean;
|
|
676
|
+
}
|
|
677
|
+
declare function useElementBounding(target: BasicTarget, options?: UseElementBoundingOptions): {
|
|
678
|
+
readonly height: number;
|
|
679
|
+
readonly bottom: number;
|
|
680
|
+
readonly left: number;
|
|
681
|
+
readonly right: number;
|
|
682
|
+
readonly top: number;
|
|
683
|
+
readonly width: number;
|
|
684
|
+
readonly x: number;
|
|
685
|
+
readonly y: number;
|
|
686
|
+
readonly update: () => void;
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
declare function useElementVisibility(target: BasicTarget<HTMLElement | SVGElement>, options?: IntersectionObserverInit): readonly [any, () => void];
|
|
690
|
+
|
|
691
|
+
declare function useWindowsFocus(): boolean;
|
|
692
|
+
|
|
693
|
+
export { useActiveElement, useCounter, useCustomCompareEffect, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useFavicon, useFileDialog, useFirstMountState, _default as useFps, useFullscreen, useGeolocation, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLongPress, useMarkdown, _default$1 as useMediaDevices, useMediaQuery, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnline, useOrientation, usePageLeave, usePermission, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useResizeObserver, useScriptTag, useScroll, useScrollLock, useSessionStorage, useTextDirection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, _default$3 as useUpdateEffect, _default$2 as useUpdateLayoutEffect, useVirtualList, useWindowsFocus };
|
package/index.mjs
CHANGED
|
@@ -2219,4 +2219,107 @@ function useDraggable(target, options = {}) {
|
|
|
2219
2219
|
return [position.x, position.y, !!pressedDelta];
|
|
2220
2220
|
}
|
|
2221
2221
|
|
|
2222
|
-
|
|
2222
|
+
function useElementBounding(target, options = {}) {
|
|
2223
|
+
const {
|
|
2224
|
+
reset = true,
|
|
2225
|
+
windowResize = true,
|
|
2226
|
+
windowScroll = true,
|
|
2227
|
+
immediate = true
|
|
2228
|
+
} = options;
|
|
2229
|
+
const [height, setHeight] = useState(0);
|
|
2230
|
+
const [bottom, setBottom] = useState(0);
|
|
2231
|
+
const [left, setLeft] = useState(0);
|
|
2232
|
+
const [right, setRight] = useState(0);
|
|
2233
|
+
const [top, setTop] = useState(0);
|
|
2234
|
+
const [width, setWidth] = useState(0);
|
|
2235
|
+
const [x, setX] = useState(0);
|
|
2236
|
+
const [y, setY] = useState(0);
|
|
2237
|
+
const update = useEvent(() => {
|
|
2238
|
+
const element = getTargetElement(target);
|
|
2239
|
+
if (!element) {
|
|
2240
|
+
if (reset) {
|
|
2241
|
+
setHeight(0);
|
|
2242
|
+
setBottom(0);
|
|
2243
|
+
setLeft(0);
|
|
2244
|
+
setRight(0);
|
|
2245
|
+
setTop(0);
|
|
2246
|
+
setWidth(0);
|
|
2247
|
+
setX(0);
|
|
2248
|
+
setY(0);
|
|
2249
|
+
}
|
|
2250
|
+
return;
|
|
2251
|
+
}
|
|
2252
|
+
const rect = element.getBoundingClientRect();
|
|
2253
|
+
setHeight(rect.height);
|
|
2254
|
+
setBottom(rect.bottom);
|
|
2255
|
+
setLeft(rect.left);
|
|
2256
|
+
setRight(rect.right);
|
|
2257
|
+
setTop(rect.top);
|
|
2258
|
+
setWidth(rect.width);
|
|
2259
|
+
setX(rect.x);
|
|
2260
|
+
setY(rect.y);
|
|
2261
|
+
});
|
|
2262
|
+
useResizeObserver(target, update);
|
|
2263
|
+
useEffect(() => {
|
|
2264
|
+
if (immediate) {
|
|
2265
|
+
update();
|
|
2266
|
+
}
|
|
2267
|
+
}, [immediate, update]);
|
|
2268
|
+
useEffect(() => {
|
|
2269
|
+
if (windowScroll) {
|
|
2270
|
+
window.addEventListener("scroll", update, { passive: true });
|
|
2271
|
+
}
|
|
2272
|
+
if (windowResize) {
|
|
2273
|
+
window.addEventListener("resize", update, { passive: true });
|
|
2274
|
+
}
|
|
2275
|
+
return () => {
|
|
2276
|
+
if (windowScroll) {
|
|
2277
|
+
window.removeEventListener("scroll", update);
|
|
2278
|
+
}
|
|
2279
|
+
if (windowResize) {
|
|
2280
|
+
window.removeEventListener("resize", update);
|
|
2281
|
+
}
|
|
2282
|
+
};
|
|
2283
|
+
}, [update, windowResize, windowScroll]);
|
|
2284
|
+
return {
|
|
2285
|
+
height,
|
|
2286
|
+
bottom,
|
|
2287
|
+
left,
|
|
2288
|
+
right,
|
|
2289
|
+
top,
|
|
2290
|
+
width,
|
|
2291
|
+
x,
|
|
2292
|
+
y,
|
|
2293
|
+
update
|
|
2294
|
+
};
|
|
2295
|
+
}
|
|
2296
|
+
|
|
2297
|
+
function useElementVisibility(target, options = {}) {
|
|
2298
|
+
const [visible, setVisible] = useState(false);
|
|
2299
|
+
const callback = useCallback((entries) => {
|
|
2300
|
+
const rect = entries[0].boundingClientRect;
|
|
2301
|
+
setVisible(
|
|
2302
|
+
rect.top <= (window.innerHeight || document.documentElement.clientHeight) && rect.left <= (window.innerWidth || document.documentElement.clientWidth) && rect.bottom >= 0 && rect.right >= 0
|
|
2303
|
+
);
|
|
2304
|
+
}, []);
|
|
2305
|
+
const stop = useIntersectionObserver(target, callback, options);
|
|
2306
|
+
return [visible, stop];
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
function useWindowsFocus() {
|
|
2310
|
+
const [focused, setFocused] = useState(() => {
|
|
2311
|
+
if (!window) {
|
|
2312
|
+
return false;
|
|
2313
|
+
}
|
|
2314
|
+
return window.document.hasFocus();
|
|
2315
|
+
});
|
|
2316
|
+
useEventListener("blur", () => {
|
|
2317
|
+
setFocused(false);
|
|
2318
|
+
});
|
|
2319
|
+
useEventListener("focus", () => {
|
|
2320
|
+
setFocused(true);
|
|
2321
|
+
});
|
|
2322
|
+
return focused;
|
|
2323
|
+
}
|
|
2324
|
+
|
|
2325
|
+
export { useActiveElement, useCounter, useCustomCompareEffect, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useFavicon, useFileDialog, useFirstMountState, useFps$1 as useFps, useFullscreen, useGeolocation, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLongPress, useMarkdown, useMediaDevices$1 as useMediaDevices, useMediaQuery, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnline, useOrientation, usePageLeave, usePermission, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useResizeObserver, useScriptTag, useScroll, useScrollLock, useSessionStorage, useTextDirection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useVirtualList, useWindowsFocus };
|