@reactuses/core 5.0.13 → 5.0.15

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 CHANGED
@@ -584,6 +584,7 @@ const getInitialState$2 = (key, defaultValue, storage, serializer, onError)=>{
584
584
  if (raw !== undefined && raw !== null) {
585
585
  return serializer == null ? void 0 : serializer.read(raw);
586
586
  }
587
+ return null;
587
588
  } catch (error) {
588
589
  onError == null ? void 0 : onError(error);
589
590
  }
@@ -607,7 +608,8 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
607
608
  const serializer = (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
608
609
  const [state, setState] = React.useState(getInitialState$2(key, defaultValue, storage, serializer, onError));
609
610
  useDeepCompareEffect(()=>{
610
- const data = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue;
611
+ var _ref;
612
+ const data = (_ref = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue) != null ? _ref : null;
611
613
  const getStoredValue = ()=>{
612
614
  try {
613
615
  const raw = storage == null ? void 0 : storage.getItem(key);
@@ -3321,11 +3323,175 @@ const useMobileLandscape = ()=>{
3321
3323
  return isMobileLandscape;
3322
3324
  };
3323
3325
 
3326
+ const useControlled = (value, defaultValue, onChange)=>{
3327
+ const [stateValue, setStateValue] = React.useState(value !== undefined ? value : defaultValue);
3328
+ const isControlled = value !== undefined;
3329
+ const onChangeRef = useLatest(onChange);
3330
+ const setValue = React.useCallback((newValue)=>{
3331
+ if (!isControlled) {
3332
+ setStateValue(newValue);
3333
+ }
3334
+ onChangeRef.current == null ? void 0 : onChangeRef.current.call(onChangeRef, newValue);
3335
+ }, [
3336
+ isControlled,
3337
+ onChangeRef
3338
+ ]);
3339
+ return [
3340
+ isControlled ? value : stateValue,
3341
+ setValue
3342
+ ];
3343
+ };
3344
+
3345
+ const useDisclosure = (props = {})=>{
3346
+ const { defaultOpen, isOpen: isOpenProp, onClose: onCloseProp, onOpen: onOpenProp, onChange = ()=>{} } = props;
3347
+ const onOpenPropRef = useLatest(onOpenProp);
3348
+ const onClosePropRef = useLatest(onCloseProp);
3349
+ const [isOpen, setIsOpen] = useControlled(isOpenProp, defaultOpen || false, onChange);
3350
+ const isControlled = isOpenProp !== undefined;
3351
+ const onClose = React.useCallback(()=>{
3352
+ if (!isControlled) {
3353
+ setIsOpen(false);
3354
+ }
3355
+ onClosePropRef.current == null ? void 0 : onClosePropRef.current.call(onClosePropRef);
3356
+ }, [
3357
+ isControlled,
3358
+ onClosePropRef,
3359
+ setIsOpen
3360
+ ]);
3361
+ const onOpen = React.useCallback(()=>{
3362
+ if (!isControlled) {
3363
+ setIsOpen(true);
3364
+ }
3365
+ onOpenPropRef.current == null ? void 0 : onOpenPropRef.current.call(onOpenPropRef);
3366
+ }, [
3367
+ isControlled,
3368
+ onOpenPropRef,
3369
+ setIsOpen
3370
+ ]);
3371
+ const onOpenChange = React.useCallback(()=>{
3372
+ const action = isOpen ? onClose : onOpen;
3373
+ action();
3374
+ }, [
3375
+ isOpen,
3376
+ onOpen,
3377
+ onClose
3378
+ ]);
3379
+ return {
3380
+ isOpen: !!isOpen,
3381
+ onOpen,
3382
+ onClose,
3383
+ onOpenChange,
3384
+ isControlled
3385
+ };
3386
+ };
3387
+
3388
+ const useEventSource = (url, events = [], options = defaultOptions$1)=>{
3389
+ const [data, setData] = React.useState(null);
3390
+ const [error, setError] = React.useState(null);
3391
+ const [status, setStatus] = React.useState("DISCONNECTED");
3392
+ const [event, setEvent] = React.useState(null);
3393
+ const [lastEventId, setLastEventId] = React.useState(null);
3394
+ const retries = React.useRef(0);
3395
+ const explicitlyClosed = React.useRef(false);
3396
+ const eventSourceRef = React.useRef(null);
3397
+ const eventListenerRef = React.useRef();
3398
+ if (!eventListenerRef.current) {
3399
+ eventListenerRef.current = new Map();
3400
+ }
3401
+ const clean = useEvent(()=>{
3402
+ const listeners = eventListenerRef.current;
3403
+ events.forEach((name)=>{
3404
+ const handler = listeners == null ? void 0 : listeners.get(name);
3405
+ if (handler) {
3406
+ var _eventSourceRef_current;
3407
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.removeEventListener(name, handler);
3408
+ }
3409
+ });
3410
+ });
3411
+ const close = React.useCallback(()=>{
3412
+ var _eventSourceRef_current;
3413
+ setStatus("DISCONNECTED");
3414
+ clean();
3415
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.close();
3416
+ eventSourceRef.current = null;
3417
+ explicitlyClosed.current = true;
3418
+ }, [
3419
+ clean
3420
+ ]);
3421
+ const open = useEvent(()=>{
3422
+ close();
3423
+ explicitlyClosed.current = false;
3424
+ retries.current = 0;
3425
+ if (!eventSourceRef.current) {
3426
+ eventSourceRef.current = new EventSource(url, {
3427
+ withCredentials: options.withCredentials
3428
+ });
3429
+ }
3430
+ const es = eventSourceRef.current;
3431
+ es.onopen = ()=>{
3432
+ setStatus("CONNECTED");
3433
+ setError(null);
3434
+ };
3435
+ es.onmessage = (ev)=>{
3436
+ setData(ev.data);
3437
+ setLastEventId(ev.lastEventId);
3438
+ setStatus("CONNECTED");
3439
+ };
3440
+ es.onerror = (err)=>{
3441
+ setError(err);
3442
+ setStatus("DISCONNECTED");
3443
+ if (options.autoReconnect && !explicitlyClosed.current) {
3444
+ const { retries: maxRetries = -1, delay = 1000, onFailed } = options.autoReconnect;
3445
+ retries.current += 1;
3446
+ if (typeof maxRetries === "number" && (maxRetries < 0 || retries.current < maxRetries) || typeof maxRetries === "function" && maxRetries()) {
3447
+ setTimeout(open, delay);
3448
+ } else {
3449
+ onFailed == null ? void 0 : onFailed();
3450
+ }
3451
+ }
3452
+ };
3453
+ const listeners = eventListenerRef.current;
3454
+ events.forEach((name)=>{
3455
+ const handler = (event)=>{
3456
+ setEvent(name);
3457
+ var _event_data;
3458
+ setData((_event_data = event.data) != null ? _event_data : null);
3459
+ };
3460
+ es.addEventListener(name, handler);
3461
+ listeners == null ? void 0 : listeners.set(name, handler);
3462
+ });
3463
+ });
3464
+ React.useEffect(()=>{
3465
+ if (options.immediate !== false) {
3466
+ open();
3467
+ }
3468
+ return close;
3469
+ }, [
3470
+ open,
3471
+ close,
3472
+ options.immediate
3473
+ ]);
3474
+ useUnmount(()=>{
3475
+ close();
3476
+ });
3477
+ return {
3478
+ eventSourceRef,
3479
+ data,
3480
+ error,
3481
+ status,
3482
+ lastEventId,
3483
+ event,
3484
+ close,
3485
+ open
3486
+ };
3487
+ };
3488
+
3324
3489
  exports.defaultOptions = defaultOptions;
3325
3490
  exports.useActiveElement = useActiveElement;
3326
3491
  exports.useAsyncEffect = useAsyncEffect;
3327
3492
  exports.useClickOutside = useClickOutside;
3328
3493
  exports.useClipboard = useClipboard;
3494
+ exports.useControlled = useControlled;
3329
3495
  exports.useCookie = useCookie;
3330
3496
  exports.useCountDown = useCountDown;
3331
3497
  exports.useCounter = useCounter;
@@ -3336,6 +3502,7 @@ exports.useDarkMode = useDarkMode;
3336
3502
  exports.useDebounce = useDebounce;
3337
3503
  exports.useDebounceFn = useDebounceFn;
3338
3504
  exports.useDeepCompareEffect = useDeepCompareEffect;
3505
+ exports.useDisclosure = useDisclosure;
3339
3506
  exports.useDocumentVisibility = useDocumentVisibility;
3340
3507
  exports.useDoubleClick = useDoubleClick;
3341
3508
  exports.useDraggable = useDraggable;
@@ -3346,6 +3513,7 @@ exports.useElementVisibility = useElementVisibility;
3346
3513
  exports.useEvent = useEvent;
3347
3514
  exports.useEventEmitter = useEventEmitter;
3348
3515
  exports.useEventListener = useEventListener;
3516
+ exports.useEventSource = useEventSource;
3349
3517
  exports.useEyeDropper = useEyeDropper;
3350
3518
  exports.useFavicon = useFavicon;
3351
3519
  exports.useFileDialog = useFileDialog;
package/dist/index.d.cts CHANGED
@@ -2639,6 +2639,183 @@ 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
+
2642
2819
  /**
2643
2820
  * @title useDocumentVisiblity
2644
2821
  * @returns_en document visibility
@@ -2991,4 +3168,4 @@ defauleValue?: boolean) => boolean;
2991
3168
  */
2992
3169
  type UseMobileLandscape = () => boolean;
2993
3170
 
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 };
3171
+ export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventSourceStatus, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, 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 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, 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/dist/index.d.mts CHANGED
@@ -2639,6 +2639,183 @@ 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
+
2642
2819
  /**
2643
2820
  * @title useDocumentVisiblity
2644
2821
  * @returns_en document visibility
@@ -2991,4 +3168,4 @@ defauleValue?: boolean) => boolean;
2991
3168
  */
2992
3169
  type UseMobileLandscape = () => boolean;
2993
3170
 
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 };
3171
+ export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventSourceStatus, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, 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 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, 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/dist/index.d.ts CHANGED
@@ -2639,6 +2639,183 @@ 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
+
2642
2819
  /**
2643
2820
  * @title useDocumentVisiblity
2644
2821
  * @returns_en document visibility
@@ -2991,4 +3168,4 @@ defauleValue?: boolean) => boolean;
2991
3168
  */
2992
3169
  type UseMobileLandscape = () => boolean;
2993
3170
 
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 };
3171
+ export { type ColorScheme, type Contrast, type DepsEqualFnType, type EventSourceStatus, type EventType, type INetworkInformation, type IUseNetworkState, type KeyModifier, type Pausable, type Platform, 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 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, 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/dist/index.mjs CHANGED
@@ -577,6 +577,7 @@ const getInitialState$2 = (key, defaultValue, storage, serializer, onError)=>{
577
577
  if (raw !== undefined && raw !== null) {
578
578
  return serializer == null ? void 0 : serializer.read(raw);
579
579
  }
580
+ return null;
580
581
  } catch (error) {
581
582
  onError == null ? void 0 : onError(error);
582
583
  }
@@ -600,7 +601,8 @@ function useStorage(key, defaultValue, getStorage = ()=>isBrowser ? sessionStora
600
601
  const serializer = (_options_serializer = options.serializer) != null ? _options_serializer : StorageSerializers[type];
601
602
  const [state, setState] = useState(getInitialState$2(key, defaultValue, storage, serializer, onError));
602
603
  useDeepCompareEffect(()=>{
603
- const data = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue;
604
+ var _ref;
605
+ const data = (_ref = effectStorageValue ? isFunction(effectStorageValue) ? effectStorageValue() : effectStorageValue : defaultValue) != null ? _ref : null;
604
606
  const getStoredValue = ()=>{
605
607
  try {
606
608
  const raw = storage == null ? void 0 : storage.getItem(key);
@@ -3314,4 +3316,167 @@ const useMobileLandscape = ()=>{
3314
3316
  return isMobileLandscape;
3315
3317
  };
3316
3318
 
3317
- 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 };
3319
+ const useControlled = (value, defaultValue, onChange)=>{
3320
+ const [stateValue, setStateValue] = useState(value !== undefined ? value : defaultValue);
3321
+ const isControlled = value !== undefined;
3322
+ const onChangeRef = useLatest(onChange);
3323
+ const setValue = useCallback((newValue)=>{
3324
+ if (!isControlled) {
3325
+ setStateValue(newValue);
3326
+ }
3327
+ onChangeRef.current == null ? void 0 : onChangeRef.current.call(onChangeRef, newValue);
3328
+ }, [
3329
+ isControlled,
3330
+ onChangeRef
3331
+ ]);
3332
+ return [
3333
+ isControlled ? value : stateValue,
3334
+ setValue
3335
+ ];
3336
+ };
3337
+
3338
+ const useDisclosure = (props = {})=>{
3339
+ const { defaultOpen, isOpen: isOpenProp, onClose: onCloseProp, onOpen: onOpenProp, onChange = ()=>{} } = props;
3340
+ const onOpenPropRef = useLatest(onOpenProp);
3341
+ const onClosePropRef = useLatest(onCloseProp);
3342
+ const [isOpen, setIsOpen] = useControlled(isOpenProp, defaultOpen || false, onChange);
3343
+ const isControlled = isOpenProp !== undefined;
3344
+ const onClose = useCallback(()=>{
3345
+ if (!isControlled) {
3346
+ setIsOpen(false);
3347
+ }
3348
+ onClosePropRef.current == null ? void 0 : onClosePropRef.current.call(onClosePropRef);
3349
+ }, [
3350
+ isControlled,
3351
+ onClosePropRef,
3352
+ setIsOpen
3353
+ ]);
3354
+ const onOpen = useCallback(()=>{
3355
+ if (!isControlled) {
3356
+ setIsOpen(true);
3357
+ }
3358
+ onOpenPropRef.current == null ? void 0 : onOpenPropRef.current.call(onOpenPropRef);
3359
+ }, [
3360
+ isControlled,
3361
+ onOpenPropRef,
3362
+ setIsOpen
3363
+ ]);
3364
+ const onOpenChange = useCallback(()=>{
3365
+ const action = isOpen ? onClose : onOpen;
3366
+ action();
3367
+ }, [
3368
+ isOpen,
3369
+ onOpen,
3370
+ onClose
3371
+ ]);
3372
+ return {
3373
+ isOpen: !!isOpen,
3374
+ onOpen,
3375
+ onClose,
3376
+ onOpenChange,
3377
+ isControlled
3378
+ };
3379
+ };
3380
+
3381
+ const useEventSource = (url, events = [], options = defaultOptions$1)=>{
3382
+ const [data, setData] = useState(null);
3383
+ const [error, setError] = useState(null);
3384
+ const [status, setStatus] = useState("DISCONNECTED");
3385
+ const [event, setEvent] = useState(null);
3386
+ const [lastEventId, setLastEventId] = useState(null);
3387
+ const retries = useRef(0);
3388
+ const explicitlyClosed = useRef(false);
3389
+ const eventSourceRef = useRef(null);
3390
+ const eventListenerRef = useRef();
3391
+ if (!eventListenerRef.current) {
3392
+ eventListenerRef.current = new Map();
3393
+ }
3394
+ const clean = useEvent(()=>{
3395
+ const listeners = eventListenerRef.current;
3396
+ events.forEach((name)=>{
3397
+ const handler = listeners == null ? void 0 : listeners.get(name);
3398
+ if (handler) {
3399
+ var _eventSourceRef_current;
3400
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.removeEventListener(name, handler);
3401
+ }
3402
+ });
3403
+ });
3404
+ const close = useCallback(()=>{
3405
+ var _eventSourceRef_current;
3406
+ setStatus("DISCONNECTED");
3407
+ clean();
3408
+ (_eventSourceRef_current = eventSourceRef.current) == null ? void 0 : _eventSourceRef_current.close();
3409
+ eventSourceRef.current = null;
3410
+ explicitlyClosed.current = true;
3411
+ }, [
3412
+ clean
3413
+ ]);
3414
+ const open = useEvent(()=>{
3415
+ close();
3416
+ explicitlyClosed.current = false;
3417
+ retries.current = 0;
3418
+ if (!eventSourceRef.current) {
3419
+ eventSourceRef.current = new EventSource(url, {
3420
+ withCredentials: options.withCredentials
3421
+ });
3422
+ }
3423
+ const es = eventSourceRef.current;
3424
+ es.onopen = ()=>{
3425
+ setStatus("CONNECTED");
3426
+ setError(null);
3427
+ };
3428
+ es.onmessage = (ev)=>{
3429
+ setData(ev.data);
3430
+ setLastEventId(ev.lastEventId);
3431
+ setStatus("CONNECTED");
3432
+ };
3433
+ es.onerror = (err)=>{
3434
+ setError(err);
3435
+ setStatus("DISCONNECTED");
3436
+ if (options.autoReconnect && !explicitlyClosed.current) {
3437
+ const { retries: maxRetries = -1, delay = 1000, onFailed } = options.autoReconnect;
3438
+ retries.current += 1;
3439
+ if (typeof maxRetries === "number" && (maxRetries < 0 || retries.current < maxRetries) || typeof maxRetries === "function" && maxRetries()) {
3440
+ setTimeout(open, delay);
3441
+ } else {
3442
+ onFailed == null ? void 0 : onFailed();
3443
+ }
3444
+ }
3445
+ };
3446
+ const listeners = eventListenerRef.current;
3447
+ events.forEach((name)=>{
3448
+ const handler = (event)=>{
3449
+ setEvent(name);
3450
+ var _event_data;
3451
+ setData((_event_data = event.data) != null ? _event_data : null);
3452
+ };
3453
+ es.addEventListener(name, handler);
3454
+ listeners == null ? void 0 : listeners.set(name, handler);
3455
+ });
3456
+ });
3457
+ useEffect(()=>{
3458
+ if (options.immediate !== false) {
3459
+ open();
3460
+ }
3461
+ return close;
3462
+ }, [
3463
+ open,
3464
+ close,
3465
+ options.immediate
3466
+ ]);
3467
+ useUnmount(()=>{
3468
+ close();
3469
+ });
3470
+ return {
3471
+ eventSourceRef,
3472
+ data,
3473
+ error,
3474
+ status,
3475
+ lastEventId,
3476
+ event,
3477
+ close,
3478
+ open
3479
+ };
3480
+ };
3481
+
3482
+ 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.13",
3
+ "version": "5.0.15",
4
4
  "license": "Unlicense",
5
5
  "homepage": "https://www.reactuse.com/",
6
6
  "repository": {