@reactuses/core 1.1.1 → 1.1.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.
- package/dist/index.cjs +52 -0
- package/dist/index.d.ts +11 -2
- package/dist/index.mjs +51 -1
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -2407,9 +2407,60 @@ function useCycleList(list, i = 0) {
|
|
|
2407
2407
|
return [list[index], next, prev];
|
|
2408
2408
|
}
|
|
2409
2409
|
|
|
2410
|
+
function useFocus(target, initialValue = false) {
|
|
2411
|
+
const [focus, innerSetFocus] = react.useState(initialValue);
|
|
2412
|
+
useEventListener("focus", () => innerSetFocus(true), target);
|
|
2413
|
+
useEventListener("blur", () => innerSetFocus(false), target);
|
|
2414
|
+
useMount(() => {
|
|
2415
|
+
setFocus(focus);
|
|
2416
|
+
});
|
|
2417
|
+
const setFocus = (value) => {
|
|
2418
|
+
const element = getTargetElement(target);
|
|
2419
|
+
if (!element) {
|
|
2420
|
+
return;
|
|
2421
|
+
}
|
|
2422
|
+
if (!value) {
|
|
2423
|
+
element.blur();
|
|
2424
|
+
} else if (value) {
|
|
2425
|
+
element.focus();
|
|
2426
|
+
}
|
|
2427
|
+
};
|
|
2428
|
+
return [focus, setFocus];
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
function useControlled({
|
|
2432
|
+
controlled,
|
|
2433
|
+
defaultValue: defaultProp,
|
|
2434
|
+
state = "value"
|
|
2435
|
+
}) {
|
|
2436
|
+
const { current: isControlled } = react.useRef(controlled !== void 0);
|
|
2437
|
+
const [valueState, setValue] = react.useState(defaultProp);
|
|
2438
|
+
const value = isControlled ? controlled : valueState;
|
|
2439
|
+
react.useEffect(() => {
|
|
2440
|
+
if (isControlled !== (controlled !== void 0)) {
|
|
2441
|
+
console.error(
|
|
2442
|
+
[
|
|
2443
|
+
`A component is changing the ${isControlled ? "" : "un"}controlled ${state} state of ${name} to be ${isControlled ? "un" : ""}controlled.`,
|
|
2444
|
+
"Elements should not switch from uncontrolled to controlled (or vice versa).",
|
|
2445
|
+
`Decide between using a controlled or uncontrolled ${name} element for the lifetime of the component.`,
|
|
2446
|
+
"The nature of the state is determined during the first render. It's considered controlled if the value is not `undefined`.",
|
|
2447
|
+
"More info: https://fb.me/react-controlled-components"
|
|
2448
|
+
].join("\n")
|
|
2449
|
+
);
|
|
2450
|
+
}
|
|
2451
|
+
}, [state, controlled]);
|
|
2452
|
+
const setValueIfUncontrolled = react.useCallback((newValue) => {
|
|
2453
|
+
if (!isControlled) {
|
|
2454
|
+
setValue(newValue);
|
|
2455
|
+
}
|
|
2456
|
+
}, []);
|
|
2457
|
+
return [value, setValueIfUncontrolled];
|
|
2458
|
+
}
|
|
2459
|
+
|
|
2410
2460
|
exports.useActiveElement = useActiveElement;
|
|
2411
2461
|
exports.useClickOutside = useClickOutSide;
|
|
2412
2462
|
exports.useClipboard = useClipBorad;
|
|
2463
|
+
exports.useControlled = useControlled;
|
|
2413
2464
|
exports.useCounter = useCounter;
|
|
2414
2465
|
exports.useCustomCompareEffect = useCustomCompareEffect;
|
|
2415
2466
|
exports.useCycleList = useCycleList;
|
|
@@ -2429,6 +2480,7 @@ exports.useEventListener = useEventListener;
|
|
|
2429
2480
|
exports.useFavicon = useFavicon;
|
|
2430
2481
|
exports.useFileDialog = useFileDialog;
|
|
2431
2482
|
exports.useFirstMountState = useFirstMountState;
|
|
2483
|
+
exports.useFocus = useFocus;
|
|
2432
2484
|
exports.useFps = useFps$1;
|
|
2433
2485
|
exports.useFullscreen = useFullscreen;
|
|
2434
2486
|
exports.useGeolocation = useGeolocation;
|
package/dist/index.d.ts
CHANGED
|
@@ -684,7 +684,7 @@ declare function useElementBounding(target: BasicTarget, options?: UseElementBou
|
|
|
684
684
|
readonly update: () => void;
|
|
685
685
|
};
|
|
686
686
|
|
|
687
|
-
declare function useElementVisibility(target: BasicTarget<HTMLElement | SVGElement>, options?: IntersectionObserverInit): readonly [
|
|
687
|
+
declare function useElementVisibility(target: BasicTarget<HTMLElement | SVGElement>, options?: IntersectionObserverInit): readonly [boolean, () => void];
|
|
688
688
|
|
|
689
689
|
declare function useWindowsFocus(): boolean;
|
|
690
690
|
|
|
@@ -710,4 +710,13 @@ declare function useClickOutSide(target: BasicTarget, handler: (evt: EventType)
|
|
|
710
710
|
|
|
711
711
|
declare function useCycleList<T>(list: T[], i?: number): readonly [T, (i?: number) => void, (i?: number) => void];
|
|
712
712
|
|
|
713
|
-
|
|
713
|
+
declare function useFocus(target: BasicTarget<HTMLElement | SVGElement>, initialValue?: boolean): readonly [boolean, (value: boolean) => void];
|
|
714
|
+
|
|
715
|
+
interface IProps<T> {
|
|
716
|
+
controlled?: T;
|
|
717
|
+
defaultValue?: T;
|
|
718
|
+
state?: T;
|
|
719
|
+
}
|
|
720
|
+
declare function useControlled<T = string>({ controlled, defaultValue, state, }: IProps<T>): readonly [T, (newValue: T) => void];
|
|
721
|
+
|
|
722
|
+
export { useActiveElement, useClickOutSide as useClickOutside, useClipBorad as useClipboard, useControlled, useCounter, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useFavicon, useFileDialog, useFirstMountState, useFocus, _default as useFps, useFullscreen, useGeolocation, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLongPress, _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, useWindowScroll, useWindowSize, useWindowsFocus };
|
package/dist/index.mjs
CHANGED
|
@@ -2403,4 +2403,54 @@ function useCycleList(list, i = 0) {
|
|
|
2403
2403
|
return [list[index], next, prev];
|
|
2404
2404
|
}
|
|
2405
2405
|
|
|
2406
|
-
|
|
2406
|
+
function useFocus(target, initialValue = false) {
|
|
2407
|
+
const [focus, innerSetFocus] = useState(initialValue);
|
|
2408
|
+
useEventListener("focus", () => innerSetFocus(true), target);
|
|
2409
|
+
useEventListener("blur", () => innerSetFocus(false), target);
|
|
2410
|
+
useMount(() => {
|
|
2411
|
+
setFocus(focus);
|
|
2412
|
+
});
|
|
2413
|
+
const setFocus = (value) => {
|
|
2414
|
+
const element = getTargetElement(target);
|
|
2415
|
+
if (!element) {
|
|
2416
|
+
return;
|
|
2417
|
+
}
|
|
2418
|
+
if (!value) {
|
|
2419
|
+
element.blur();
|
|
2420
|
+
} else if (value) {
|
|
2421
|
+
element.focus();
|
|
2422
|
+
}
|
|
2423
|
+
};
|
|
2424
|
+
return [focus, setFocus];
|
|
2425
|
+
}
|
|
2426
|
+
|
|
2427
|
+
function useControlled({
|
|
2428
|
+
controlled,
|
|
2429
|
+
defaultValue: defaultProp,
|
|
2430
|
+
state = "value"
|
|
2431
|
+
}) {
|
|
2432
|
+
const { current: isControlled } = useRef(controlled !== void 0);
|
|
2433
|
+
const [valueState, setValue] = useState(defaultProp);
|
|
2434
|
+
const value = isControlled ? controlled : valueState;
|
|
2435
|
+
useEffect(() => {
|
|
2436
|
+
if (isControlled !== (controlled !== void 0)) {
|
|
2437
|
+
console.error(
|
|
2438
|
+
[
|
|
2439
|
+
`A component is changing the ${isControlled ? "" : "un"}controlled ${state} state of ${name} to be ${isControlled ? "un" : ""}controlled.`,
|
|
2440
|
+
"Elements should not switch from uncontrolled to controlled (or vice versa).",
|
|
2441
|
+
`Decide between using a controlled or uncontrolled ${name} element for the lifetime of the component.`,
|
|
2442
|
+
"The nature of the state is determined during the first render. It's considered controlled if the value is not `undefined`.",
|
|
2443
|
+
"More info: https://fb.me/react-controlled-components"
|
|
2444
|
+
].join("\n")
|
|
2445
|
+
);
|
|
2446
|
+
}
|
|
2447
|
+
}, [state, controlled]);
|
|
2448
|
+
const setValueIfUncontrolled = useCallback((newValue) => {
|
|
2449
|
+
if (!isControlled) {
|
|
2450
|
+
setValue(newValue);
|
|
2451
|
+
}
|
|
2452
|
+
}, []);
|
|
2453
|
+
return [value, setValueIfUncontrolled];
|
|
2454
|
+
}
|
|
2455
|
+
|
|
2456
|
+
export { useActiveElement, useClickOutSide as useClickOutside, useClipBorad as useClipboard, useControlled, useCounter, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps$1 as useFps, useFullscreen, useGeolocation, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLongPress, 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, useWindowScroll, useWindowSize, useWindowsFocus };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reactuses/core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"main": "./dist/index.cjs",
|
|
5
5
|
"module": "./dist/index.mjs",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"lint": "eslint \"{hooks,tests}/**/*.{ts,tsx}\"",
|
|
26
26
|
"build": "esno scripts/build.ts",
|
|
27
27
|
"build:rollup": "cross-env NODE_OPTIONS=\"--max-old-space-size=6144\" rollup -c",
|
|
28
|
+
"dev": "cross-env NODE_OPTIONS=\"--max-old-space-size=6144\" rollup -c --watch",
|
|
28
29
|
"publish:ci": "esno scripts/publish.ts",
|
|
29
30
|
"release:prepare": "bump",
|
|
30
31
|
"release": "esno scripts/release.ts",
|