@reactuses/core 5.0.14 → 5.0.16

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 CHANGED
@@ -56,6 +56,19 @@ Refer to [documentations](https://reactuse.com/) for more details.
56
56
 
57
57
  <hr/>
58
58
 
59
+ ## Sponsor Me
60
+
61
+ If my work has helped you, consider buying me a cup of coffee. Thank you very much🥰!.
62
+
63
+ [Buy me a coffee](https://www.buymeacoffee.com/lianwenwu)
64
+
65
+ ### For Chinese User
66
+
67
+ <p float="left">
68
+ <img src="https://d21002cb.images-f3o.pages.dev/images/wechat.jpg" alt="Wechat Pay" width="200" />
69
+ <img src="https://d21002cb.images-f3o.pages.dev/images/ali.jpg" alt="Ali Pay" width="200" />
70
+ </p>
71
+
59
72
  ## Feedback
60
73
 
61
74
  You can submit an [issue](https://github.com/childrentime/reactuse/issues) or provide feedback on [Discord](https://discord.gg/HMsq6cFkKp).
@@ -0,0 +1,29 @@
1
+ 'use client';
2
+ var React = require('react');
3
+
4
+ function assignRef(ref, value) {
5
+ if (ref == null) return;
6
+ if (typeof ref === "function") {
7
+ ref(value);
8
+ return;
9
+ }
10
+ try {
11
+ ref.current = value;
12
+ } catch (error) {
13
+ throw new Error(`Cannot assign value '${value}' to ref '${ref}'`);
14
+ }
15
+ }
16
+ function mergeRefs(...refs) {
17
+ return (node)=>{
18
+ refs.forEach((ref)=>{
19
+ assignRef(ref, node);
20
+ });
21
+ };
22
+ }
23
+ const useMergedRefs = (...refs)=>{
24
+ return React.useMemo(()=>mergeRefs(...refs), refs);
25
+ };
26
+
27
+ exports.assignRef = assignRef;
28
+ exports.mergeRefs = mergeRefs;
29
+ exports.useMergedRefs = useMergedRefs;
@@ -0,0 +1,27 @@
1
+ 'use client';
2
+ import { useMemo } from 'react';
3
+
4
+ function assignRef(ref, value) {
5
+ if (ref == null) return;
6
+ if (typeof ref === "function") {
7
+ ref(value);
8
+ return;
9
+ }
10
+ try {
11
+ ref.current = value;
12
+ } catch (error) {
13
+ throw new Error(`Cannot assign value '${value}' to ref '${ref}'`);
14
+ }
15
+ }
16
+ function mergeRefs(...refs) {
17
+ return (node)=>{
18
+ refs.forEach((ref)=>{
19
+ assignRef(ref, node);
20
+ });
21
+ };
22
+ }
23
+ const useMergedRefs = (...refs)=>{
24
+ return useMemo(()=>mergeRefs(...refs), refs);
25
+ };
26
+
27
+ export { assignRef as a, mergeRefs as m, useMergedRefs as u };
package/dist/index.cjs CHANGED
@@ -5,6 +5,7 @@ var lodashEs = require('lodash-es');
5
5
  var Cookies = require('js-cookie');
6
6
  var screenfull = require('screenfull');
7
7
  var index_js = require('use-sync-external-store/shim/index.js');
8
+ var indexClient = require('./index-client-DSNqfa2q.cjs');
8
9
 
9
10
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
11
 
@@ -3323,11 +3324,178 @@ const useMobileLandscape = ()=>{
3323
3324
  return isMobileLandscape;
3324
3325
  };
3325
3326
 
3327
+ const useControlled = (value, defaultValue, onChange)=>{
3328
+ const [stateValue, setStateValue] = React.useState(value !== undefined ? value : defaultValue);
3329
+ const isControlled = value !== undefined;
3330
+ const onChangeRef = useLatest(onChange);
3331
+ const setValue = React.useCallback((newValue)=>{
3332
+ if (!isControlled) {
3333
+ setStateValue(newValue);
3334
+ }
3335
+ onChangeRef.current == null ? void 0 : onChangeRef.current.call(onChangeRef, newValue);
3336
+ }, [
3337
+ isControlled,
3338
+ onChangeRef
3339
+ ]);
3340
+ return [
3341
+ isControlled ? value : stateValue,
3342
+ setValue
3343
+ ];
3344
+ };
3345
+
3346
+ const useDisclosure = (props = {})=>{
3347
+ const { defaultOpen, isOpen: isOpenProp, onClose: onCloseProp, onOpen: onOpenProp, onChange = ()=>{} } = props;
3348
+ const onOpenPropRef = useLatest(onOpenProp);
3349
+ const onClosePropRef = useLatest(onCloseProp);
3350
+ const [isOpen, setIsOpen] = useControlled(isOpenProp, defaultOpen || false, onChange);
3351
+ const isControlled = isOpenProp !== undefined;
3352
+ const onClose = React.useCallback(()=>{
3353
+ if (!isControlled) {
3354
+ setIsOpen(false);
3355
+ }
3356
+ onClosePropRef.current == null ? void 0 : onClosePropRef.current.call(onClosePropRef);
3357
+ }, [
3358
+ isControlled,
3359
+ onClosePropRef,
3360
+ setIsOpen
3361
+ ]);
3362
+ const onOpen = React.useCallback(()=>{
3363
+ if (!isControlled) {
3364
+ setIsOpen(true);
3365
+ }
3366
+ onOpenPropRef.current == null ? void 0 : onOpenPropRef.current.call(onOpenPropRef);
3367
+ }, [
3368
+ isControlled,
3369
+ onOpenPropRef,
3370
+ setIsOpen
3371
+ ]);
3372
+ const onOpenChange = React.useCallback(()=>{
3373
+ const action = isOpen ? onClose : onOpen;
3374
+ action();
3375
+ }, [
3376
+ isOpen,
3377
+ onOpen,
3378
+ onClose
3379
+ ]);
3380
+ return {
3381
+ isOpen: !!isOpen,
3382
+ onOpen,
3383
+ onClose,
3384
+ onOpenChange,
3385
+ isControlled
3386
+ };
3387
+ };
3388
+
3389
+ const useEventSource = (url, events = [], options = defaultOptions$1)=>{
3390
+ const [data, setData] = React.useState(null);
3391
+ const [error, setError] = React.useState(null);
3392
+ const [status, setStatus] = React.useState("DISCONNECTED");
3393
+ const [event, setEvent] = React.useState(null);
3394
+ const [lastEventId, setLastEventId] = React.useState(null);
3395
+ const retries = React.useRef(0);
3396
+ const explicitlyClosed = React.useRef(false);
3397
+ const eventSourceRef = React.useRef(null);
3398
+ const eventListenerRef = React.useRef();
3399
+ if (!eventListenerRef.current) {
3400
+ eventListenerRef.current = new Map();
3401
+ }
3402
+ const clean = useEvent(()=>{
3403
+ const listeners = eventListenerRef.current;
3404
+ events.forEach((name)=>{
3405
+ const handler = listeners == null ? void 0 : listeners.get(name);
3406
+ if (handler) {
3407
+ var _eventSourceRef_current;
3408
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.removeEventListener(name, handler);
3409
+ }
3410
+ });
3411
+ });
3412
+ const close = React.useCallback(()=>{
3413
+ var _eventSourceRef_current;
3414
+ setStatus("DISCONNECTED");
3415
+ clean();
3416
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.close();
3417
+ eventSourceRef.current = null;
3418
+ explicitlyClosed.current = true;
3419
+ }, [
3420
+ clean
3421
+ ]);
3422
+ const open = useEvent(()=>{
3423
+ close();
3424
+ explicitlyClosed.current = false;
3425
+ retries.current = 0;
3426
+ if (!eventSourceRef.current) {
3427
+ eventSourceRef.current = new EventSource(url, {
3428
+ withCredentials: options.withCredentials
3429
+ });
3430
+ }
3431
+ const es = eventSourceRef.current;
3432
+ es.onopen = ()=>{
3433
+ setStatus("CONNECTED");
3434
+ setError(null);
3435
+ };
3436
+ es.onmessage = (ev)=>{
3437
+ setData(ev.data);
3438
+ setLastEventId(ev.lastEventId);
3439
+ setStatus("CONNECTED");
3440
+ };
3441
+ es.onerror = (err)=>{
3442
+ setError(err);
3443
+ setStatus("DISCONNECTED");
3444
+ if (options.autoReconnect && !explicitlyClosed.current) {
3445
+ const { retries: maxRetries = -1, delay = 1000, onFailed } = options.autoReconnect;
3446
+ retries.current += 1;
3447
+ if (typeof maxRetries === "number" && (maxRetries < 0 || retries.current < maxRetries) || typeof maxRetries === "function" && maxRetries()) {
3448
+ setTimeout(open, delay);
3449
+ } else {
3450
+ onFailed == null ? void 0 : onFailed();
3451
+ }
3452
+ }
3453
+ };
3454
+ const listeners = eventListenerRef.current;
3455
+ events.forEach((name)=>{
3456
+ const handler = (event)=>{
3457
+ setEvent(name);
3458
+ var _event_data;
3459
+ setData((_event_data = event.data) != null ? _event_data : null);
3460
+ };
3461
+ es.addEventListener(name, handler);
3462
+ listeners == null ? void 0 : listeners.set(name, handler);
3463
+ });
3464
+ });
3465
+ React.useEffect(()=>{
3466
+ if (options.immediate !== false) {
3467
+ open();
3468
+ }
3469
+ return close;
3470
+ }, [
3471
+ open,
3472
+ close,
3473
+ options.immediate
3474
+ ]);
3475
+ useUnmount(()=>{
3476
+ close();
3477
+ });
3478
+ return {
3479
+ eventSourceRef,
3480
+ data,
3481
+ error,
3482
+ status,
3483
+ lastEventId,
3484
+ event,
3485
+ close,
3486
+ open
3487
+ };
3488
+ };
3489
+
3490
+ exports.assignRef = indexClient.assignRef;
3491
+ exports.mergeRefs = indexClient.mergeRefs;
3492
+ exports.useMergedRefs = indexClient.useMergedRefs;
3326
3493
  exports.defaultOptions = defaultOptions;
3327
3494
  exports.useActiveElement = useActiveElement;
3328
3495
  exports.useAsyncEffect = useAsyncEffect;
3329
3496
  exports.useClickOutside = useClickOutside;
3330
3497
  exports.useClipboard = useClipboard;
3498
+ exports.useControlled = useControlled;
3331
3499
  exports.useCookie = useCookie;
3332
3500
  exports.useCountDown = useCountDown;
3333
3501
  exports.useCounter = useCounter;
@@ -3338,6 +3506,7 @@ exports.useDarkMode = useDarkMode;
3338
3506
  exports.useDebounce = useDebounce;
3339
3507
  exports.useDebounceFn = useDebounceFn;
3340
3508
  exports.useDeepCompareEffect = useDeepCompareEffect;
3509
+ exports.useDisclosure = useDisclosure;
3341
3510
  exports.useDocumentVisibility = useDocumentVisibility;
3342
3511
  exports.useDoubleClick = useDoubleClick;
3343
3512
  exports.useDraggable = useDraggable;
@@ -3348,6 +3517,7 @@ exports.useElementVisibility = useElementVisibility;
3348
3517
  exports.useEvent = useEvent;
3349
3518
  exports.useEventEmitter = useEventEmitter;
3350
3519
  exports.useEventListener = useEventListener;
3520
+ exports.useEventSource = useEventSource;
3351
3521
  exports.useEyeDropper = useEyeDropper;
3352
3522
  exports.useFavicon = useFavicon;
3353
3523
  exports.useFileDialog = useFileDialog;
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as react from 'react';
2
- import react__default, { DependencyList, MutableRefObject, EffectCallback, Dispatch, SetStateAction, RefObject, useEffect, useLayoutEffect } from 'react';
2
+ import react__default, { DependencyList, MutableRefObject, EffectCallback, Dispatch, SetStateAction, RefObject, useEffect, useLayoutEffect, Ref } from 'react';
3
3
  import Cookies from 'js-cookie';
4
4
  import { DebounceSettings, ThrottleSettings, DebouncedFunc as DebouncedFunc$1 } from 'lodash-es';
5
5
  import * as lodash from 'lodash';
@@ -2639,6 +2639,193 @@ declare const usePlatform: UsePlatform;
2639
2639
 
2640
2640
  declare const useMobileLandscape: () => boolean;
2641
2641
 
2642
+ /**
2643
+ * @title useControlledState
2644
+ * @returns_en A tuple with the following elements:
2645
+ * - The current value.
2646
+ * - A function to update the value.
2647
+ * @returns 包含以下元素的元组:
2648
+ * - 当前值。
2649
+ * - 更新当前值的函数。
2650
+ */
2651
+ type UseControlled = <T>(
2652
+ /**
2653
+ * @en controlled value
2654
+ * @zh 受控值
2655
+ */
2656
+ value: T | undefined,
2657
+ /**
2658
+ * @en default value
2659
+ * @zh 默认值
2660
+ */
2661
+ defaultValue: T,
2662
+ /**
2663
+ * @en callback when value change
2664
+ * @zh 值改变时的回调
2665
+ */
2666
+ onChange?: ((v: T, ...args: any[]) => void) | undefined) => [T, (value: T) => void];
2667
+
2668
+ declare const useControlled: UseControlled;
2669
+
2670
+ /**
2671
+ * @title UseDisclosureProps
2672
+ */
2673
+ interface UseDisclosureProps {
2674
+ /**
2675
+ * @en Whether the disclosure is open, if passed, it will be controlled
2676
+ * @zh 是否打开,传了则为受控
2677
+ */
2678
+ isOpen?: boolean;
2679
+ /**
2680
+ * @en default open state
2681
+ * @zh 默认打开状态
2682
+ */
2683
+ defaultOpen?: boolean;
2684
+ /**
2685
+ * @en Callback when disclosure is closed
2686
+ * @zh 关闭时的回调
2687
+ */
2688
+ onClose?: () => void;
2689
+ /**
2690
+ * @en Callback when disclosure is opened
2691
+ * @zh 打开时的回调
2692
+ */
2693
+ onOpen?: () => void;
2694
+ /**
2695
+ * @en Callback when disclosure is changed
2696
+ * @zh 状态改变时的回调
2697
+ */
2698
+ onChange?(isOpen: boolean | undefined): void;
2699
+ }
2700
+ /**
2701
+ * @title useDisclosure
2702
+ */
2703
+ type UseDisclosure = (props?: UseDisclosureProps) => {
2704
+ isOpen: boolean;
2705
+ onOpen: () => void;
2706
+ onClose: () => void;
2707
+ onOpenChange: () => void;
2708
+ isControlled: boolean;
2709
+ };
2710
+
2711
+ declare const useDisclosure: (props?: UseDisclosureProps) => {
2712
+ isOpen: boolean;
2713
+ onOpen: () => void;
2714
+ onClose: () => void;
2715
+ onOpenChange: () => void;
2716
+ isControlled: boolean;
2717
+ };
2718
+
2719
+ type EventSourceStatus = "CONNECTING" | "CONNECTED" | "DISCONNECTED";
2720
+ /**
2721
+ * @title UseEventSourceOptions
2722
+ */
2723
+ interface UseEventSourceOptions extends EventSourceInit {
2724
+ /**
2725
+ * @en immediately open the connection, enabled by default
2726
+ * @zh 立即打开连接, 默认打开
2727
+ */
2728
+ immediate?: boolean;
2729
+ /**
2730
+ * @en Automatically reconnect when the connection is disconnected
2731
+ * @zh 连接断开时自动重连
2732
+ */
2733
+ autoReconnect?: UseEventSourceAutoReconnectOptions;
2734
+ }
2735
+ /**
2736
+ * @title UseEventSourceAutoReconnectOptions
2737
+ */
2738
+ interface UseEventSourceAutoReconnectOptions {
2739
+ /**
2740
+ * @en The number of retries, if it is a function, it will be called to determine whether to retry
2741
+ * @zh 重试次数,如果是函数,会调用来判断是否重试
2742
+ */
2743
+ retries?: number | (() => boolean);
2744
+ /**
2745
+ * @en The delay time before reconnecting
2746
+ * @zh 重连前的延迟时间
2747
+ */
2748
+ delay?: number;
2749
+ /**
2750
+ * @en Callback when reconnection fails
2751
+ * @zh 重连失败时的回调
2752
+ */
2753
+ onFailed?: () => void;
2754
+ }
2755
+ type UseEventSource = <Events extends string[]>(
2756
+ /**
2757
+ * @en The URL of the server-sent event
2758
+ * @zh 服务器发送事件的 URL
2759
+ */
2760
+ url: string | URL,
2761
+ /**
2762
+ * @en The event name to listen to
2763
+ * @zh 要监听的事件名
2764
+ */
2765
+ events?: Events,
2766
+ /**
2767
+ * @en EventSource options
2768
+ * @zh EventSource 选项
2769
+ */
2770
+ options?: UseEventSourceOptions) => UseEventSourceReturn;
2771
+ /**
2772
+ * @title UseEventSourceReturn
2773
+ */
2774
+ interface UseEventSourceReturn {
2775
+ /**
2776
+ * @en EventSource instance
2777
+ * @zh EventSource 实例
2778
+ */
2779
+ eventSourceRef: React.MutableRefObject<EventSource | null>;
2780
+ /**
2781
+ * @en The data received
2782
+ * @zh 接收到的数据
2783
+ */
2784
+ data: string | null;
2785
+ /**
2786
+ * @en The error occurred
2787
+ * @zh 发生的错误
2788
+ */
2789
+ error: Event | null;
2790
+ /**
2791
+ * @en The status of the connection
2792
+ * @zh 连接的状态
2793
+ */
2794
+ status: EventSourceStatus;
2795
+ /**
2796
+ * @en The last event ID
2797
+ * @zh 最后的事件 ID
2798
+ */
2799
+ lastEventId: string | null;
2800
+ /**
2801
+ * @en The event name
2802
+ * @zh 事件名
2803
+ */
2804
+ event: string | null;
2805
+ /**
2806
+ * @zh 关闭连接
2807
+ * @en Close the connection
2808
+ */
2809
+ close: () => void;
2810
+ /**
2811
+ * @zh 打开连接
2812
+ * @en Open the connection
2813
+ */
2814
+ open: () => void;
2815
+ }
2816
+
2817
+ declare const useEventSource: UseEventSource;
2818
+
2819
+ /**
2820
+ * @title useMergedRef
2821
+ */
2822
+ type UseMergedRef = <T>(...refs: PossibleRef<T>[]) => (node: T | null) => void;
2823
+ type PossibleRef<T> = Ref<T> | undefined;
2824
+
2825
+ declare function assignRef<T>(ref: PossibleRef<T>, value: T): void;
2826
+ declare function mergeRefs<T>(...refs: PossibleRef<T>[]): (node: T | null) => void;
2827
+ declare const useMergedRefs: <T>(...refs: PossibleRef<T>[]) => (node: T | null) => void;
2828
+
2642
2829
  /**
2643
2830
  * @title useDocumentVisiblity
2644
2831
  * @returns_en document visibility
@@ -2991,4 +3178,4 @@ defauleValue?: boolean) => boolean;
2991
3178
  */
2992
3179
  type UseMobileLandscape = () => boolean;
2993
3180
 
2994
- export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, type UseActiveElement, type UseAsyncEffect, type UseClickOutside, type UseClipboard, type UseCookie, type UseCookieState, type UseCountDown, type UseCounter, type UseCssVar, type UseCssVarOptions, type UseCustomCompareEffect, type UseCycleList, type UseDarkMode, type UseDarkOptions, type UseDebounce, type UseDebounceFn, type UseDeepCompareEffect, type UseDocumentVisibility, type UseDoubleClick, type UseDoubleClickProps, type UseDraggable, type UseDraggableOptions, type UseDropZone, type UseElementBounding, type UseElementBoundingOptions, type UseElementBoundingReturn, type UseElementSize, type UseElementVisibility, type UseEvent, type UseEventEmitter, type UseEventEmitterDisposable, type UseEventEmitterEvent, type UseEventEmitterEventOnce, type UseEventEmitterListener, type UseEventEmitterReturn, type UseEventListener, type UseEyeDropper, type UseEyeDropperOpenOptions, type UseEyeDropperOpenReturnType, type UseFavicon, type UseFileDialog, type UseFileDialogOptions, type UseFirstMountState, type UseFocus, type UseFps, type UseFpsOptions, type UseFullScreenOptions, type UseFullscreen, type UseGeolocation, type UseHover, type UseIdle, type UseInfiniteScroll, type UseInfiniteScrollArrivedState, type UseInfiniteScrollDirection, type UseInfiniteScrollLoadMore, type UseInfiniteScrollOptions, type UseIntersectionObserver, type UseInterval, type UseIntervalOptions, type UseKeyModifier, type UseLatest, type UseLocalStorage, type UseLocalStorageOptions, type UseLocalStorageSerializer, type UseLocationSelector, type UseLongPress, type UseLongPressOptions, type UseMeasure, type UseMeasureRect, type UseMediaDeviceOptions, type UseMediaDevices, type UseMediaQuery, type UseMobileLandscape, type UseModifierOptions, type UseMount, type UseMountedState, type UseMouse, type UseMouseCursorState, type UseMousePressed, type UseMousePressedOptions, type UseMousePressedSourceType, type UseMutationObserver, type UseNetwork, type UseObjectUrl, type UseOnline, type UseOrientation, type UseOrientationLockType, type UseOrientationState, type UseOrientationType, type UsePageLeave, type UsePermission, type UsePermissionDescriptorNamePolyfill, type UsePermissionGeneralPermissionDescriptor, type UsePermissionState, type UsePlatform, type UsePlatformProps, type UsePlatformReturn, type UsePreferredColorScheme, type UsePreferredContrast, type UsePreferredDark, type UsePrevious, type UseRafFn, type UseRafState, type UseReducedMotion, type UseResizeObserver, type UseScreenSafeArea, type UseScriptTag, type UseScriptTagOptions, type UseScriptTagStatus, type UseScroll, type UseScrollArrivedState, type UseScrollDirection, type UseScrollIntoView, type UseScrollIntoViewAnimation, type UseScrollIntoViewParams, type UseScrollLock, type UseScrollOffset, type UseScrollOptions, type UseSessionStorage, type UseSessionStorageOptions, type UseSessionStorageSerializer, type UseSetState, type UseSticky, type UseStickyParams, type UseSupported, type UseTextDirection, type UseTextDirectionOptions, type UseTextDirectionValue, type UseTextSelection, type UseThrottle, type UseThrottleFn, type UseTimeout, type UseTimeoutFn, type UseTimeoutFnOptions, type UseTimeoutOptions, type UseTitle, type UseToggle, type UseUnmount, type UseUpdate, type UseWebNotification, type UseWebNotificationReturn, type UseWebNotificationShow, type UseWindowScroll, type UseWindowScrollState, type UseWindowSize, type UseWindowsFocus, defaultOptions, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
3181
+ export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventSourceStatus, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, type PossibleRef, type UseActiveElement, type UseAsyncEffect, type UseClickOutside, type UseClipboard, type UseControlled, type UseCookie, type UseCookieState, type UseCountDown, type UseCounter, type UseCssVar, type UseCssVarOptions, type UseCustomCompareEffect, type UseCycleList, type UseDarkMode, type UseDarkOptions, type UseDebounce, type UseDebounceFn, type UseDeepCompareEffect, type UseDisclosure, type UseDisclosureProps, type UseDocumentVisibility, type UseDoubleClick, type UseDoubleClickProps, type UseDraggable, type UseDraggableOptions, type UseDropZone, type UseElementBounding, type UseElementBoundingOptions, type UseElementBoundingReturn, type UseElementSize, type UseElementVisibility, type UseEvent, type UseEventEmitter, type UseEventEmitterDisposable, type UseEventEmitterEvent, type UseEventEmitterEventOnce, type UseEventEmitterListener, type UseEventEmitterReturn, type UseEventListener, type UseEventSource, type UseEventSourceAutoReconnectOptions, type UseEventSourceOptions, type UseEventSourceReturn, type UseEyeDropper, type UseEyeDropperOpenOptions, type UseEyeDropperOpenReturnType, type UseFavicon, type UseFileDialog, type UseFileDialogOptions, type UseFirstMountState, type UseFocus, type UseFps, type UseFpsOptions, type UseFullScreenOptions, type UseFullscreen, type UseGeolocation, type UseHover, type UseIdle, type UseInfiniteScroll, type UseInfiniteScrollArrivedState, type UseInfiniteScrollDirection, type UseInfiniteScrollLoadMore, type UseInfiniteScrollOptions, type UseIntersectionObserver, type UseInterval, type UseIntervalOptions, type UseKeyModifier, type UseLatest, type UseLocalStorage, type UseLocalStorageOptions, type UseLocalStorageSerializer, type UseLocationSelector, type UseLongPress, type UseLongPressOptions, type UseMeasure, type UseMeasureRect, type UseMediaDeviceOptions, type UseMediaDevices, type UseMediaQuery, type UseMergedRef, type UseMobileLandscape, type UseModifierOptions, type UseMount, type UseMountedState, type UseMouse, type UseMouseCursorState, type UseMousePressed, type UseMousePressedOptions, type UseMousePressedSourceType, type UseMutationObserver, type UseNetwork, type UseObjectUrl, type UseOnline, type UseOrientation, type UseOrientationLockType, type UseOrientationState, type UseOrientationType, type UsePageLeave, type UsePermission, type UsePermissionDescriptorNamePolyfill, type UsePermissionGeneralPermissionDescriptor, type UsePermissionState, type UsePlatform, type UsePlatformProps, type UsePlatformReturn, type UsePreferredColorScheme, type UsePreferredContrast, type UsePreferredDark, type UsePrevious, type UseRafFn, type UseRafState, type UseReducedMotion, type UseResizeObserver, type UseScreenSafeArea, type UseScriptTag, type UseScriptTagOptions, type UseScriptTagStatus, type UseScroll, type UseScrollArrivedState, type UseScrollDirection, type UseScrollIntoView, type UseScrollIntoViewAnimation, type UseScrollIntoViewParams, type UseScrollLock, type UseScrollOffset, type UseScrollOptions, type UseSessionStorage, type UseSessionStorageOptions, type UseSessionStorageSerializer, type UseSetState, type UseSticky, type UseStickyParams, type UseSupported, type UseTextDirection, type UseTextDirectionOptions, type UseTextDirectionValue, type UseTextSelection, type UseThrottle, type UseThrottleFn, type UseTimeout, type UseTimeoutFn, type UseTimeoutFnOptions, type UseTimeoutOptions, type UseTitle, type UseToggle, type UseUnmount, type UseUpdate, type UseWebNotification, type UseWebNotificationReturn, type UseWebNotificationShow, type UseWindowScroll, type UseWindowScrollState, type UseWindowSize, type UseWindowsFocus, assignRef, defaultOptions, mergeRefs, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useControlled, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDisclosure, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEventSource, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMergedRefs, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as react from 'react';
2
- import react__default, { DependencyList, MutableRefObject, EffectCallback, Dispatch, SetStateAction, RefObject, useEffect, useLayoutEffect } from 'react';
2
+ import react__default, { DependencyList, MutableRefObject, EffectCallback, Dispatch, SetStateAction, RefObject, useEffect, useLayoutEffect, Ref } from 'react';
3
3
  import Cookies from 'js-cookie';
4
4
  import { DebounceSettings, ThrottleSettings, DebouncedFunc as DebouncedFunc$1 } from 'lodash-es';
5
5
  import * as lodash from 'lodash';
@@ -2639,6 +2639,193 @@ declare const usePlatform: UsePlatform;
2639
2639
 
2640
2640
  declare const useMobileLandscape: () => boolean;
2641
2641
 
2642
+ /**
2643
+ * @title useControlledState
2644
+ * @returns_en A tuple with the following elements:
2645
+ * - The current value.
2646
+ * - A function to update the value.
2647
+ * @returns 包含以下元素的元组:
2648
+ * - 当前值。
2649
+ * - 更新当前值的函数。
2650
+ */
2651
+ type UseControlled = <T>(
2652
+ /**
2653
+ * @en controlled value
2654
+ * @zh 受控值
2655
+ */
2656
+ value: T | undefined,
2657
+ /**
2658
+ * @en default value
2659
+ * @zh 默认值
2660
+ */
2661
+ defaultValue: T,
2662
+ /**
2663
+ * @en callback when value change
2664
+ * @zh 值改变时的回调
2665
+ */
2666
+ onChange?: ((v: T, ...args: any[]) => void) | undefined) => [T, (value: T) => void];
2667
+
2668
+ declare const useControlled: UseControlled;
2669
+
2670
+ /**
2671
+ * @title UseDisclosureProps
2672
+ */
2673
+ interface UseDisclosureProps {
2674
+ /**
2675
+ * @en Whether the disclosure is open, if passed, it will be controlled
2676
+ * @zh 是否打开,传了则为受控
2677
+ */
2678
+ isOpen?: boolean;
2679
+ /**
2680
+ * @en default open state
2681
+ * @zh 默认打开状态
2682
+ */
2683
+ defaultOpen?: boolean;
2684
+ /**
2685
+ * @en Callback when disclosure is closed
2686
+ * @zh 关闭时的回调
2687
+ */
2688
+ onClose?: () => void;
2689
+ /**
2690
+ * @en Callback when disclosure is opened
2691
+ * @zh 打开时的回调
2692
+ */
2693
+ onOpen?: () => void;
2694
+ /**
2695
+ * @en Callback when disclosure is changed
2696
+ * @zh 状态改变时的回调
2697
+ */
2698
+ onChange?(isOpen: boolean | undefined): void;
2699
+ }
2700
+ /**
2701
+ * @title useDisclosure
2702
+ */
2703
+ type UseDisclosure = (props?: UseDisclosureProps) => {
2704
+ isOpen: boolean;
2705
+ onOpen: () => void;
2706
+ onClose: () => void;
2707
+ onOpenChange: () => void;
2708
+ isControlled: boolean;
2709
+ };
2710
+
2711
+ declare const useDisclosure: (props?: UseDisclosureProps) => {
2712
+ isOpen: boolean;
2713
+ onOpen: () => void;
2714
+ onClose: () => void;
2715
+ onOpenChange: () => void;
2716
+ isControlled: boolean;
2717
+ };
2718
+
2719
+ type EventSourceStatus = "CONNECTING" | "CONNECTED" | "DISCONNECTED";
2720
+ /**
2721
+ * @title UseEventSourceOptions
2722
+ */
2723
+ interface UseEventSourceOptions extends EventSourceInit {
2724
+ /**
2725
+ * @en immediately open the connection, enabled by default
2726
+ * @zh 立即打开连接, 默认打开
2727
+ */
2728
+ immediate?: boolean;
2729
+ /**
2730
+ * @en Automatically reconnect when the connection is disconnected
2731
+ * @zh 连接断开时自动重连
2732
+ */
2733
+ autoReconnect?: UseEventSourceAutoReconnectOptions;
2734
+ }
2735
+ /**
2736
+ * @title UseEventSourceAutoReconnectOptions
2737
+ */
2738
+ interface UseEventSourceAutoReconnectOptions {
2739
+ /**
2740
+ * @en The number of retries, if it is a function, it will be called to determine whether to retry
2741
+ * @zh 重试次数,如果是函数,会调用来判断是否重试
2742
+ */
2743
+ retries?: number | (() => boolean);
2744
+ /**
2745
+ * @en The delay time before reconnecting
2746
+ * @zh 重连前的延迟时间
2747
+ */
2748
+ delay?: number;
2749
+ /**
2750
+ * @en Callback when reconnection fails
2751
+ * @zh 重连失败时的回调
2752
+ */
2753
+ onFailed?: () => void;
2754
+ }
2755
+ type UseEventSource = <Events extends string[]>(
2756
+ /**
2757
+ * @en The URL of the server-sent event
2758
+ * @zh 服务器发送事件的 URL
2759
+ */
2760
+ url: string | URL,
2761
+ /**
2762
+ * @en The event name to listen to
2763
+ * @zh 要监听的事件名
2764
+ */
2765
+ events?: Events,
2766
+ /**
2767
+ * @en EventSource options
2768
+ * @zh EventSource 选项
2769
+ */
2770
+ options?: UseEventSourceOptions) => UseEventSourceReturn;
2771
+ /**
2772
+ * @title UseEventSourceReturn
2773
+ */
2774
+ interface UseEventSourceReturn {
2775
+ /**
2776
+ * @en EventSource instance
2777
+ * @zh EventSource 实例
2778
+ */
2779
+ eventSourceRef: React.MutableRefObject<EventSource | null>;
2780
+ /**
2781
+ * @en The data received
2782
+ * @zh 接收到的数据
2783
+ */
2784
+ data: string | null;
2785
+ /**
2786
+ * @en The error occurred
2787
+ * @zh 发生的错误
2788
+ */
2789
+ error: Event | null;
2790
+ /**
2791
+ * @en The status of the connection
2792
+ * @zh 连接的状态
2793
+ */
2794
+ status: EventSourceStatus;
2795
+ /**
2796
+ * @en The last event ID
2797
+ * @zh 最后的事件 ID
2798
+ */
2799
+ lastEventId: string | null;
2800
+ /**
2801
+ * @en The event name
2802
+ * @zh 事件名
2803
+ */
2804
+ event: string | null;
2805
+ /**
2806
+ * @zh 关闭连接
2807
+ * @en Close the connection
2808
+ */
2809
+ close: () => void;
2810
+ /**
2811
+ * @zh 打开连接
2812
+ * @en Open the connection
2813
+ */
2814
+ open: () => void;
2815
+ }
2816
+
2817
+ declare const useEventSource: UseEventSource;
2818
+
2819
+ /**
2820
+ * @title useMergedRef
2821
+ */
2822
+ type UseMergedRef = <T>(...refs: PossibleRef<T>[]) => (node: T | null) => void;
2823
+ type PossibleRef<T> = Ref<T> | undefined;
2824
+
2825
+ declare function assignRef<T>(ref: PossibleRef<T>, value: T): void;
2826
+ declare function mergeRefs<T>(...refs: PossibleRef<T>[]): (node: T | null) => void;
2827
+ declare const useMergedRefs: <T>(...refs: PossibleRef<T>[]) => (node: T | null) => void;
2828
+
2642
2829
  /**
2643
2830
  * @title useDocumentVisiblity
2644
2831
  * @returns_en document visibility
@@ -2991,4 +3178,4 @@ defauleValue?: boolean) => boolean;
2991
3178
  */
2992
3179
  type UseMobileLandscape = () => boolean;
2993
3180
 
2994
- export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, type UseActiveElement, type UseAsyncEffect, type UseClickOutside, type UseClipboard, type UseCookie, type UseCookieState, type UseCountDown, type UseCounter, type UseCssVar, type UseCssVarOptions, type UseCustomCompareEffect, type UseCycleList, type UseDarkMode, type UseDarkOptions, type UseDebounce, type UseDebounceFn, type UseDeepCompareEffect, type UseDocumentVisibility, type UseDoubleClick, type UseDoubleClickProps, type UseDraggable, type UseDraggableOptions, type UseDropZone, type UseElementBounding, type UseElementBoundingOptions, type UseElementBoundingReturn, type UseElementSize, type UseElementVisibility, type UseEvent, type UseEventEmitter, type UseEventEmitterDisposable, type UseEventEmitterEvent, type UseEventEmitterEventOnce, type UseEventEmitterListener, type UseEventEmitterReturn, type UseEventListener, type UseEyeDropper, type UseEyeDropperOpenOptions, type UseEyeDropperOpenReturnType, type UseFavicon, type UseFileDialog, type UseFileDialogOptions, type UseFirstMountState, type UseFocus, type UseFps, type UseFpsOptions, type UseFullScreenOptions, type UseFullscreen, type UseGeolocation, type UseHover, type UseIdle, type UseInfiniteScroll, type UseInfiniteScrollArrivedState, type UseInfiniteScrollDirection, type UseInfiniteScrollLoadMore, type UseInfiniteScrollOptions, type UseIntersectionObserver, type UseInterval, type UseIntervalOptions, type UseKeyModifier, type UseLatest, type UseLocalStorage, type UseLocalStorageOptions, type UseLocalStorageSerializer, type UseLocationSelector, type UseLongPress, type UseLongPressOptions, type UseMeasure, type UseMeasureRect, type UseMediaDeviceOptions, type UseMediaDevices, type UseMediaQuery, type UseMobileLandscape, type UseModifierOptions, type UseMount, type UseMountedState, type UseMouse, type UseMouseCursorState, type UseMousePressed, type UseMousePressedOptions, type UseMousePressedSourceType, type UseMutationObserver, type UseNetwork, type UseObjectUrl, type UseOnline, type UseOrientation, type UseOrientationLockType, type UseOrientationState, type UseOrientationType, type UsePageLeave, type UsePermission, type UsePermissionDescriptorNamePolyfill, type UsePermissionGeneralPermissionDescriptor, type UsePermissionState, type UsePlatform, type UsePlatformProps, type UsePlatformReturn, type UsePreferredColorScheme, type UsePreferredContrast, type UsePreferredDark, type UsePrevious, type UseRafFn, type UseRafState, type UseReducedMotion, type UseResizeObserver, type UseScreenSafeArea, type UseScriptTag, type UseScriptTagOptions, type UseScriptTagStatus, type UseScroll, type UseScrollArrivedState, type UseScrollDirection, type UseScrollIntoView, type UseScrollIntoViewAnimation, type UseScrollIntoViewParams, type UseScrollLock, type UseScrollOffset, type UseScrollOptions, type UseSessionStorage, type UseSessionStorageOptions, type UseSessionStorageSerializer, type UseSetState, type UseSticky, type UseStickyParams, type UseSupported, type UseTextDirection, type UseTextDirectionOptions, type UseTextDirectionValue, type UseTextSelection, type UseThrottle, type UseThrottleFn, type UseTimeout, type UseTimeoutFn, type UseTimeoutFnOptions, type UseTimeoutOptions, type UseTitle, type UseToggle, type UseUnmount, type UseUpdate, type UseWebNotification, type UseWebNotificationReturn, type UseWebNotificationShow, type UseWindowScroll, type UseWindowScrollState, type UseWindowSize, type UseWindowsFocus, defaultOptions, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
3181
+ export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventSourceStatus, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, type PossibleRef, type UseActiveElement, type UseAsyncEffect, type UseClickOutside, type UseClipboard, type UseControlled, type UseCookie, type UseCookieState, type UseCountDown, type UseCounter, type UseCssVar, type UseCssVarOptions, type UseCustomCompareEffect, type UseCycleList, type UseDarkMode, type UseDarkOptions, type UseDebounce, type UseDebounceFn, type UseDeepCompareEffect, type UseDisclosure, type UseDisclosureProps, type UseDocumentVisibility, type UseDoubleClick, type UseDoubleClickProps, type UseDraggable, type UseDraggableOptions, type UseDropZone, type UseElementBounding, type UseElementBoundingOptions, type UseElementBoundingReturn, type UseElementSize, type UseElementVisibility, type UseEvent, type UseEventEmitter, type UseEventEmitterDisposable, type UseEventEmitterEvent, type UseEventEmitterEventOnce, type UseEventEmitterListener, type UseEventEmitterReturn, type UseEventListener, type UseEventSource, type UseEventSourceAutoReconnectOptions, type UseEventSourceOptions, type UseEventSourceReturn, type UseEyeDropper, type UseEyeDropperOpenOptions, type UseEyeDropperOpenReturnType, type UseFavicon, type UseFileDialog, type UseFileDialogOptions, type UseFirstMountState, type UseFocus, type UseFps, type UseFpsOptions, type UseFullScreenOptions, type UseFullscreen, type UseGeolocation, type UseHover, type UseIdle, type UseInfiniteScroll, type UseInfiniteScrollArrivedState, type UseInfiniteScrollDirection, type UseInfiniteScrollLoadMore, type UseInfiniteScrollOptions, type UseIntersectionObserver, type UseInterval, type UseIntervalOptions, type UseKeyModifier, type UseLatest, type UseLocalStorage, type UseLocalStorageOptions, type UseLocalStorageSerializer, type UseLocationSelector, type UseLongPress, type UseLongPressOptions, type UseMeasure, type UseMeasureRect, type UseMediaDeviceOptions, type UseMediaDevices, type UseMediaQuery, type UseMergedRef, type UseMobileLandscape, type UseModifierOptions, type UseMount, type UseMountedState, type UseMouse, type UseMouseCursorState, type UseMousePressed, type UseMousePressedOptions, type UseMousePressedSourceType, type UseMutationObserver, type UseNetwork, type UseObjectUrl, type UseOnline, type UseOrientation, type UseOrientationLockType, type UseOrientationState, type UseOrientationType, type UsePageLeave, type UsePermission, type UsePermissionDescriptorNamePolyfill, type UsePermissionGeneralPermissionDescriptor, type UsePermissionState, type UsePlatform, type UsePlatformProps, type UsePlatformReturn, type UsePreferredColorScheme, type UsePreferredContrast, type UsePreferredDark, type UsePrevious, type UseRafFn, type UseRafState, type UseReducedMotion, type UseResizeObserver, type UseScreenSafeArea, type UseScriptTag, type UseScriptTagOptions, type UseScriptTagStatus, type UseScroll, type UseScrollArrivedState, type UseScrollDirection, type UseScrollIntoView, type UseScrollIntoViewAnimation, type UseScrollIntoViewParams, type UseScrollLock, type UseScrollOffset, type UseScrollOptions, type UseSessionStorage, type UseSessionStorageOptions, type UseSessionStorageSerializer, type UseSetState, type UseSticky, type UseStickyParams, type UseSupported, type UseTextDirection, type UseTextDirectionOptions, type UseTextDirectionValue, type UseTextSelection, type UseThrottle, type UseThrottleFn, type UseTimeout, type UseTimeoutFn, type UseTimeoutFnOptions, type UseTimeoutOptions, type UseTitle, type UseToggle, type UseUnmount, type UseUpdate, type UseWebNotification, type UseWebNotificationReturn, type UseWebNotificationShow, type UseWindowScroll, type UseWindowScrollState, type UseWindowSize, type UseWindowsFocus, assignRef, defaultOptions, mergeRefs, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useControlled, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDisclosure, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEventSource, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMergedRefs, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as react from 'react';
2
- import react__default, { DependencyList, MutableRefObject, EffectCallback, Dispatch, SetStateAction, RefObject, useEffect, useLayoutEffect } from 'react';
2
+ import react__default, { DependencyList, MutableRefObject, EffectCallback, Dispatch, SetStateAction, RefObject, useEffect, useLayoutEffect, Ref } from 'react';
3
3
  import Cookies from 'js-cookie';
4
4
  import { DebounceSettings, ThrottleSettings, DebouncedFunc as DebouncedFunc$1 } from 'lodash-es';
5
5
  import * as lodash from 'lodash';
@@ -2639,6 +2639,193 @@ declare const usePlatform: UsePlatform;
2639
2639
 
2640
2640
  declare const useMobileLandscape: () => boolean;
2641
2641
 
2642
+ /**
2643
+ * @title useControlledState
2644
+ * @returns_en A tuple with the following elements:
2645
+ * - The current value.
2646
+ * - A function to update the value.
2647
+ * @returns 包含以下元素的元组:
2648
+ * - 当前值。
2649
+ * - 更新当前值的函数。
2650
+ */
2651
+ type UseControlled = <T>(
2652
+ /**
2653
+ * @en controlled value
2654
+ * @zh 受控值
2655
+ */
2656
+ value: T | undefined,
2657
+ /**
2658
+ * @en default value
2659
+ * @zh 默认值
2660
+ */
2661
+ defaultValue: T,
2662
+ /**
2663
+ * @en callback when value change
2664
+ * @zh 值改变时的回调
2665
+ */
2666
+ onChange?: ((v: T, ...args: any[]) => void) | undefined) => [T, (value: T) => void];
2667
+
2668
+ declare const useControlled: UseControlled;
2669
+
2670
+ /**
2671
+ * @title UseDisclosureProps
2672
+ */
2673
+ interface UseDisclosureProps {
2674
+ /**
2675
+ * @en Whether the disclosure is open, if passed, it will be controlled
2676
+ * @zh 是否打开,传了则为受控
2677
+ */
2678
+ isOpen?: boolean;
2679
+ /**
2680
+ * @en default open state
2681
+ * @zh 默认打开状态
2682
+ */
2683
+ defaultOpen?: boolean;
2684
+ /**
2685
+ * @en Callback when disclosure is closed
2686
+ * @zh 关闭时的回调
2687
+ */
2688
+ onClose?: () => void;
2689
+ /**
2690
+ * @en Callback when disclosure is opened
2691
+ * @zh 打开时的回调
2692
+ */
2693
+ onOpen?: () => void;
2694
+ /**
2695
+ * @en Callback when disclosure is changed
2696
+ * @zh 状态改变时的回调
2697
+ */
2698
+ onChange?(isOpen: boolean | undefined): void;
2699
+ }
2700
+ /**
2701
+ * @title useDisclosure
2702
+ */
2703
+ type UseDisclosure = (props?: UseDisclosureProps) => {
2704
+ isOpen: boolean;
2705
+ onOpen: () => void;
2706
+ onClose: () => void;
2707
+ onOpenChange: () => void;
2708
+ isControlled: boolean;
2709
+ };
2710
+
2711
+ declare const useDisclosure: (props?: UseDisclosureProps) => {
2712
+ isOpen: boolean;
2713
+ onOpen: () => void;
2714
+ onClose: () => void;
2715
+ onOpenChange: () => void;
2716
+ isControlled: boolean;
2717
+ };
2718
+
2719
+ type EventSourceStatus = "CONNECTING" | "CONNECTED" | "DISCONNECTED";
2720
+ /**
2721
+ * @title UseEventSourceOptions
2722
+ */
2723
+ interface UseEventSourceOptions extends EventSourceInit {
2724
+ /**
2725
+ * @en immediately open the connection, enabled by default
2726
+ * @zh 立即打开连接, 默认打开
2727
+ */
2728
+ immediate?: boolean;
2729
+ /**
2730
+ * @en Automatically reconnect when the connection is disconnected
2731
+ * @zh 连接断开时自动重连
2732
+ */
2733
+ autoReconnect?: UseEventSourceAutoReconnectOptions;
2734
+ }
2735
+ /**
2736
+ * @title UseEventSourceAutoReconnectOptions
2737
+ */
2738
+ interface UseEventSourceAutoReconnectOptions {
2739
+ /**
2740
+ * @en The number of retries, if it is a function, it will be called to determine whether to retry
2741
+ * @zh 重试次数,如果是函数,会调用来判断是否重试
2742
+ */
2743
+ retries?: number | (() => boolean);
2744
+ /**
2745
+ * @en The delay time before reconnecting
2746
+ * @zh 重连前的延迟时间
2747
+ */
2748
+ delay?: number;
2749
+ /**
2750
+ * @en Callback when reconnection fails
2751
+ * @zh 重连失败时的回调
2752
+ */
2753
+ onFailed?: () => void;
2754
+ }
2755
+ type UseEventSource = <Events extends string[]>(
2756
+ /**
2757
+ * @en The URL of the server-sent event
2758
+ * @zh 服务器发送事件的 URL
2759
+ */
2760
+ url: string | URL,
2761
+ /**
2762
+ * @en The event name to listen to
2763
+ * @zh 要监听的事件名
2764
+ */
2765
+ events?: Events,
2766
+ /**
2767
+ * @en EventSource options
2768
+ * @zh EventSource 选项
2769
+ */
2770
+ options?: UseEventSourceOptions) => UseEventSourceReturn;
2771
+ /**
2772
+ * @title UseEventSourceReturn
2773
+ */
2774
+ interface UseEventSourceReturn {
2775
+ /**
2776
+ * @en EventSource instance
2777
+ * @zh EventSource 实例
2778
+ */
2779
+ eventSourceRef: React.MutableRefObject<EventSource | null>;
2780
+ /**
2781
+ * @en The data received
2782
+ * @zh 接收到的数据
2783
+ */
2784
+ data: string | null;
2785
+ /**
2786
+ * @en The error occurred
2787
+ * @zh 发生的错误
2788
+ */
2789
+ error: Event | null;
2790
+ /**
2791
+ * @en The status of the connection
2792
+ * @zh 连接的状态
2793
+ */
2794
+ status: EventSourceStatus;
2795
+ /**
2796
+ * @en The last event ID
2797
+ * @zh 最后的事件 ID
2798
+ */
2799
+ lastEventId: string | null;
2800
+ /**
2801
+ * @en The event name
2802
+ * @zh 事件名
2803
+ */
2804
+ event: string | null;
2805
+ /**
2806
+ * @zh 关闭连接
2807
+ * @en Close the connection
2808
+ */
2809
+ close: () => void;
2810
+ /**
2811
+ * @zh 打开连接
2812
+ * @en Open the connection
2813
+ */
2814
+ open: () => void;
2815
+ }
2816
+
2817
+ declare const useEventSource: UseEventSource;
2818
+
2819
+ /**
2820
+ * @title useMergedRef
2821
+ */
2822
+ type UseMergedRef = <T>(...refs: PossibleRef<T>[]) => (node: T | null) => void;
2823
+ type PossibleRef<T> = Ref<T> | undefined;
2824
+
2825
+ declare function assignRef<T>(ref: PossibleRef<T>, value: T): void;
2826
+ declare function mergeRefs<T>(...refs: PossibleRef<T>[]): (node: T | null) => void;
2827
+ declare const useMergedRefs: <T>(...refs: PossibleRef<T>[]) => (node: T | null) => void;
2828
+
2642
2829
  /**
2643
2830
  * @title useDocumentVisiblity
2644
2831
  * @returns_en document visibility
@@ -2991,4 +3178,4 @@ defauleValue?: boolean) => boolean;
2991
3178
  */
2992
3179
  type UseMobileLandscape = () => boolean;
2993
3180
 
2994
- export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, type UseActiveElement, type UseAsyncEffect, type UseClickOutside, type UseClipboard, type UseCookie, type UseCookieState, type UseCountDown, type UseCounter, type UseCssVar, type UseCssVarOptions, type UseCustomCompareEffect, type UseCycleList, type UseDarkMode, type UseDarkOptions, type UseDebounce, type UseDebounceFn, type UseDeepCompareEffect, type UseDocumentVisibility, type UseDoubleClick, type UseDoubleClickProps, type UseDraggable, type UseDraggableOptions, type UseDropZone, type UseElementBounding, type UseElementBoundingOptions, type UseElementBoundingReturn, type UseElementSize, type UseElementVisibility, type UseEvent, type UseEventEmitter, type UseEventEmitterDisposable, type UseEventEmitterEvent, type UseEventEmitterEventOnce, type UseEventEmitterListener, type UseEventEmitterReturn, type UseEventListener, type UseEyeDropper, type UseEyeDropperOpenOptions, type UseEyeDropperOpenReturnType, type UseFavicon, type UseFileDialog, type UseFileDialogOptions, type UseFirstMountState, type UseFocus, type UseFps, type UseFpsOptions, type UseFullScreenOptions, type UseFullscreen, type UseGeolocation, type UseHover, type UseIdle, type UseInfiniteScroll, type UseInfiniteScrollArrivedState, type UseInfiniteScrollDirection, type UseInfiniteScrollLoadMore, type UseInfiniteScrollOptions, type UseIntersectionObserver, type UseInterval, type UseIntervalOptions, type UseKeyModifier, type UseLatest, type UseLocalStorage, type UseLocalStorageOptions, type UseLocalStorageSerializer, type UseLocationSelector, type UseLongPress, type UseLongPressOptions, type UseMeasure, type UseMeasureRect, type UseMediaDeviceOptions, type UseMediaDevices, type UseMediaQuery, type UseMobileLandscape, type UseModifierOptions, type UseMount, type UseMountedState, type UseMouse, type UseMouseCursorState, type UseMousePressed, type UseMousePressedOptions, type UseMousePressedSourceType, type UseMutationObserver, type UseNetwork, type UseObjectUrl, type UseOnline, type UseOrientation, type UseOrientationLockType, type UseOrientationState, type UseOrientationType, type UsePageLeave, type UsePermission, type UsePermissionDescriptorNamePolyfill, type UsePermissionGeneralPermissionDescriptor, type UsePermissionState, type UsePlatform, type UsePlatformProps, type UsePlatformReturn, type UsePreferredColorScheme, type UsePreferredContrast, type UsePreferredDark, type UsePrevious, type UseRafFn, type UseRafState, type UseReducedMotion, type UseResizeObserver, type UseScreenSafeArea, type UseScriptTag, type UseScriptTagOptions, type UseScriptTagStatus, type UseScroll, type UseScrollArrivedState, type UseScrollDirection, type UseScrollIntoView, type UseScrollIntoViewAnimation, type UseScrollIntoViewParams, type UseScrollLock, type UseScrollOffset, type UseScrollOptions, type UseSessionStorage, type UseSessionStorageOptions, type UseSessionStorageSerializer, type UseSetState, type UseSticky, type UseStickyParams, type UseSupported, type UseTextDirection, type UseTextDirectionOptions, type UseTextDirectionValue, type UseTextSelection, type UseThrottle, type UseThrottleFn, type UseTimeout, type UseTimeoutFn, type UseTimeoutFnOptions, type UseTimeoutOptions, type UseTitle, type UseToggle, type UseUnmount, type UseUpdate, type UseWebNotification, type UseWebNotificationReturn, type UseWebNotificationShow, type UseWindowScroll, type UseWindowScrollState, type UseWindowSize, type UseWindowsFocus, defaultOptions, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
3181
+ export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventSourceStatus, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, type PossibleRef, type UseActiveElement, type UseAsyncEffect, type UseClickOutside, type UseClipboard, type UseControlled, type UseCookie, type UseCookieState, type UseCountDown, type UseCounter, type UseCssVar, type UseCssVarOptions, type UseCustomCompareEffect, type UseCycleList, type UseDarkMode, type UseDarkOptions, type UseDebounce, type UseDebounceFn, type UseDeepCompareEffect, type UseDisclosure, type UseDisclosureProps, type UseDocumentVisibility, type UseDoubleClick, type UseDoubleClickProps, type UseDraggable, type UseDraggableOptions, type UseDropZone, type UseElementBounding, type UseElementBoundingOptions, type UseElementBoundingReturn, type UseElementSize, type UseElementVisibility, type UseEvent, type UseEventEmitter, type UseEventEmitterDisposable, type UseEventEmitterEvent, type UseEventEmitterEventOnce, type UseEventEmitterListener, type UseEventEmitterReturn, type UseEventListener, type UseEventSource, type UseEventSourceAutoReconnectOptions, type UseEventSourceOptions, type UseEventSourceReturn, type UseEyeDropper, type UseEyeDropperOpenOptions, type UseEyeDropperOpenReturnType, type UseFavicon, type UseFileDialog, type UseFileDialogOptions, type UseFirstMountState, type UseFocus, type UseFps, type UseFpsOptions, type UseFullScreenOptions, type UseFullscreen, type UseGeolocation, type UseHover, type UseIdle, type UseInfiniteScroll, type UseInfiniteScrollArrivedState, type UseInfiniteScrollDirection, type UseInfiniteScrollLoadMore, type UseInfiniteScrollOptions, type UseIntersectionObserver, type UseInterval, type UseIntervalOptions, type UseKeyModifier, type UseLatest, type UseLocalStorage, type UseLocalStorageOptions, type UseLocalStorageSerializer, type UseLocationSelector, type UseLongPress, type UseLongPressOptions, type UseMeasure, type UseMeasureRect, type UseMediaDeviceOptions, type UseMediaDevices, type UseMediaQuery, type UseMergedRef, type UseMobileLandscape, type UseModifierOptions, type UseMount, type UseMountedState, type UseMouse, type UseMouseCursorState, type UseMousePressed, type UseMousePressedOptions, type UseMousePressedSourceType, type UseMutationObserver, type UseNetwork, type UseObjectUrl, type UseOnline, type UseOrientation, type UseOrientationLockType, type UseOrientationState, type UseOrientationType, type UsePageLeave, type UsePermission, type UsePermissionDescriptorNamePolyfill, type UsePermissionGeneralPermissionDescriptor, type UsePermissionState, type UsePlatform, type UsePlatformProps, type UsePlatformReturn, type UsePreferredColorScheme, type UsePreferredContrast, type UsePreferredDark, type UsePrevious, type UseRafFn, type UseRafState, type UseReducedMotion, type UseResizeObserver, type UseScreenSafeArea, type UseScriptTag, type UseScriptTagOptions, type UseScriptTagStatus, type UseScroll, type UseScrollArrivedState, type UseScrollDirection, type UseScrollIntoView, type UseScrollIntoViewAnimation, type UseScrollIntoViewParams, type UseScrollLock, type UseScrollOffset, type UseScrollOptions, type UseSessionStorage, type UseSessionStorageOptions, type UseSessionStorageSerializer, type UseSetState, type UseSticky, type UseStickyParams, type UseSupported, type UseTextDirection, type UseTextDirectionOptions, type UseTextDirectionValue, type UseTextSelection, type UseThrottle, type UseThrottleFn, type UseTimeout, type UseTimeoutFn, type UseTimeoutFnOptions, type UseTimeoutOptions, type UseTitle, type UseToggle, type UseUnmount, type UseUpdate, type UseWebNotification, type UseWebNotificationReturn, type UseWebNotificationShow, type UseWindowScroll, type UseWindowScrollState, type UseWindowSize, type UseWindowsFocus, assignRef, defaultOptions, mergeRefs, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useControlled, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDisclosure, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEventSource, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMergedRefs, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
package/dist/index.mjs CHANGED
@@ -3,6 +3,7 @@ import { isEqual, debounce, throttle } from 'lodash-es';
3
3
  import Cookies from 'js-cookie';
4
4
  import screenfull from 'screenfull';
5
5
  import { useSyncExternalStore } from 'use-sync-external-store/shim/index.js';
6
+ export { a as assignRef, m as mergeRefs, u as useMergedRefs } from './index-client-KksAIxbB.js';
6
7
 
7
8
  const useLatest = (value)=>{
8
9
  const ref = useRef(value);
@@ -3316,4 +3317,167 @@ const useMobileLandscape = ()=>{
3316
3317
  return isMobileLandscape;
3317
3318
  };
3318
3319
 
3319
- export { defaultOptions, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
3320
+ const useControlled = (value, defaultValue, onChange)=>{
3321
+ const [stateValue, setStateValue] = useState(value !== undefined ? value : defaultValue);
3322
+ const isControlled = value !== undefined;
3323
+ const onChangeRef = useLatest(onChange);
3324
+ const setValue = useCallback((newValue)=>{
3325
+ if (!isControlled) {
3326
+ setStateValue(newValue);
3327
+ }
3328
+ onChangeRef.current == null ? void 0 : onChangeRef.current.call(onChangeRef, newValue);
3329
+ }, [
3330
+ isControlled,
3331
+ onChangeRef
3332
+ ]);
3333
+ return [
3334
+ isControlled ? value : stateValue,
3335
+ setValue
3336
+ ];
3337
+ };
3338
+
3339
+ const useDisclosure = (props = {})=>{
3340
+ const { defaultOpen, isOpen: isOpenProp, onClose: onCloseProp, onOpen: onOpenProp, onChange = ()=>{} } = props;
3341
+ const onOpenPropRef = useLatest(onOpenProp);
3342
+ const onClosePropRef = useLatest(onCloseProp);
3343
+ const [isOpen, setIsOpen] = useControlled(isOpenProp, defaultOpen || false, onChange);
3344
+ const isControlled = isOpenProp !== undefined;
3345
+ const onClose = useCallback(()=>{
3346
+ if (!isControlled) {
3347
+ setIsOpen(false);
3348
+ }
3349
+ onClosePropRef.current == null ? void 0 : onClosePropRef.current.call(onClosePropRef);
3350
+ }, [
3351
+ isControlled,
3352
+ onClosePropRef,
3353
+ setIsOpen
3354
+ ]);
3355
+ const onOpen = useCallback(()=>{
3356
+ if (!isControlled) {
3357
+ setIsOpen(true);
3358
+ }
3359
+ onOpenPropRef.current == null ? void 0 : onOpenPropRef.current.call(onOpenPropRef);
3360
+ }, [
3361
+ isControlled,
3362
+ onOpenPropRef,
3363
+ setIsOpen
3364
+ ]);
3365
+ const onOpenChange = useCallback(()=>{
3366
+ const action = isOpen ? onClose : onOpen;
3367
+ action();
3368
+ }, [
3369
+ isOpen,
3370
+ onOpen,
3371
+ onClose
3372
+ ]);
3373
+ return {
3374
+ isOpen: !!isOpen,
3375
+ onOpen,
3376
+ onClose,
3377
+ onOpenChange,
3378
+ isControlled
3379
+ };
3380
+ };
3381
+
3382
+ const useEventSource = (url, events = [], options = defaultOptions$1)=>{
3383
+ const [data, setData] = useState(null);
3384
+ const [error, setError] = useState(null);
3385
+ const [status, setStatus] = useState("DISCONNECTED");
3386
+ const [event, setEvent] = useState(null);
3387
+ const [lastEventId, setLastEventId] = useState(null);
3388
+ const retries = useRef(0);
3389
+ const explicitlyClosed = useRef(false);
3390
+ const eventSourceRef = useRef(null);
3391
+ const eventListenerRef = useRef();
3392
+ if (!eventListenerRef.current) {
3393
+ eventListenerRef.current = new Map();
3394
+ }
3395
+ const clean = useEvent(()=>{
3396
+ const listeners = eventListenerRef.current;
3397
+ events.forEach((name)=>{
3398
+ const handler = listeners == null ? void 0 : listeners.get(name);
3399
+ if (handler) {
3400
+ var _eventSourceRef_current;
3401
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.removeEventListener(name, handler);
3402
+ }
3403
+ });
3404
+ });
3405
+ const close = useCallback(()=>{
3406
+ var _eventSourceRef_current;
3407
+ setStatus("DISCONNECTED");
3408
+ clean();
3409
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.close();
3410
+ eventSourceRef.current = null;
3411
+ explicitlyClosed.current = true;
3412
+ }, [
3413
+ clean
3414
+ ]);
3415
+ const open = useEvent(()=>{
3416
+ close();
3417
+ explicitlyClosed.current = false;
3418
+ retries.current = 0;
3419
+ if (!eventSourceRef.current) {
3420
+ eventSourceRef.current = new EventSource(url, {
3421
+ withCredentials: options.withCredentials
3422
+ });
3423
+ }
3424
+ const es = eventSourceRef.current;
3425
+ es.onopen = ()=>{
3426
+ setStatus("CONNECTED");
3427
+ setError(null);
3428
+ };
3429
+ es.onmessage = (ev)=>{
3430
+ setData(ev.data);
3431
+ setLastEventId(ev.lastEventId);
3432
+ setStatus("CONNECTED");
3433
+ };
3434
+ es.onerror = (err)=>{
3435
+ setError(err);
3436
+ setStatus("DISCONNECTED");
3437
+ if (options.autoReconnect && !explicitlyClosed.current) {
3438
+ const { retries: maxRetries = -1, delay = 1000, onFailed } = options.autoReconnect;
3439
+ retries.current += 1;
3440
+ if (typeof maxRetries === "number" && (maxRetries < 0 || retries.current < maxRetries) || typeof maxRetries === "function" && maxRetries()) {
3441
+ setTimeout(open, delay);
3442
+ } else {
3443
+ onFailed == null ? void 0 : onFailed();
3444
+ }
3445
+ }
3446
+ };
3447
+ const listeners = eventListenerRef.current;
3448
+ events.forEach((name)=>{
3449
+ const handler = (event)=>{
3450
+ setEvent(name);
3451
+ var _event_data;
3452
+ setData((_event_data = event.data) != null ? _event_data : null);
3453
+ };
3454
+ es.addEventListener(name, handler);
3455
+ listeners == null ? void 0 : listeners.set(name, handler);
3456
+ });
3457
+ });
3458
+ useEffect(()=>{
3459
+ if (options.immediate !== false) {
3460
+ open();
3461
+ }
3462
+ return close;
3463
+ }, [
3464
+ open,
3465
+ close,
3466
+ options.immediate
3467
+ ]);
3468
+ useUnmount(()=>{
3469
+ close();
3470
+ });
3471
+ return {
3472
+ eventSourceRef,
3473
+ data,
3474
+ error,
3475
+ status,
3476
+ lastEventId,
3477
+ event,
3478
+ close,
3479
+ open
3480
+ };
3481
+ };
3482
+
3483
+ export { defaultOptions, useActiveElement, useAsyncEffect, useClickOutside, useClipboard, useControlled, useCookie, useCountDown, useCounter, useCssVar, useCustomCompareEffect, useCycleList, useDarkMode, useDebounce, useDebounceFn, useDeepCompareEffect, useDisclosure, useDocumentVisibility, useDoubleClick, useDraggable, useDropZone, useElementBounding, useElementSize, useElementVisibility, useEvent, useEventEmitter, useEventListener, useEventSource, useEyeDropper, useFavicon, useFileDialog, useFirstMountState, useFocus, useFps, useFullscreen, useGeolocation, useHover, useIdle, useInfiniteScroll, useIntersectionObserver, useInterval, useIsomorphicLayoutEffect, useKeyModifier, useLatest, useLocalStorage, useLocationSelector, useLongPress, useMeasure, useMediaDevices, useMediaQuery, useMobileLandscape, useMount, useMountedState, useMouse, useMousePressed, useMutationObserver, useNetwork, useObjectUrl, useOnceEffect, useOnceLayoutEffect, useOnline, useOrientation, usePageLeave, usePermission, usePlatform, usePreferredColorScheme, usePreferredContrast, usePreferredDark, usePrevious, useRafFn, useRafState, useReducedMotion, useResizeObserver, useScreenSafeArea, useScriptTag, useScroll, useScrollIntoView, useScrollLock, useSessionStorage, useSetState, useSticky, useSupported, useTextDirection, useTextSelection, useThrottle, useThrottleFn, useTimeout, useTimeoutFn, useTitle, useToggle, useUnmount, useUpdate, useUpdateEffect, useUpdateLayoutEffect, useWebNotification, useWindowScroll, useWindowSize, useWindowsFocus };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reactuses/core",
3
- "version": "5.0.14",
3
+ "version": "5.0.16",
4
4
  "license": "Unlicense",
5
5
  "homepage": "https://www.reactuse.com/",
6
6
  "repository": {