import { HydrationPriority, HydrationBoundaryId, HydrationState } from '../types'; /** * Configuration for deferred hydration. */ export interface DeferredHydrationConfig { /** * Unique identifier for this hydration boundary. * Will be auto-generated if not provided. */ readonly id?: string; /** * Priority level for when hydration is triggered. * * @default 'normal' */ readonly priority?: HydrationPriority; /** * Function to execute during hydration. * If not provided, hydration is a no-op state transition. */ readonly onHydrate?: () => void | Promise; /** * Callback when hydration completes. */ readonly onComplete?: (duration: number) => void; /** * Callback when hydration fails. */ readonly onError?: (error: Error) => void; /** * Maximum time to wait for hydration in milliseconds. * * @default 10000 */ readonly timeout?: number; /** * Whether to auto-register with the scheduler. * * @default false */ readonly autoRegister?: boolean; } /** * Return type for the useDeferredHydration hook. */ export interface UseDeferredHydrationReturn { /** * Current hydration state. */ readonly state: HydrationState; /** * Whether the component is fully hydrated. */ readonly isHydrated: boolean; /** * Whether hydration is in progress. */ readonly isHydrating: boolean; /** * Whether hydration is pending (not started). */ readonly isPending: boolean; /** * Error that occurred during hydration, if any. */ readonly error: Error | null; /** * Duration of hydration in milliseconds, if completed. */ readonly duration: number | null; /** * The boundary ID for this deferred hydration. */ readonly boundaryId: HydrationBoundaryId; /** * Triggers immediate hydration. * * @returns Promise that resolves when hydration completes */ readonly hydrate: () => Promise; /** * Schedules hydration to occur after a delay. * * @param delay - Delay in milliseconds before hydration * @returns Function to cancel the scheduled hydration */ readonly scheduleHydration: (delay: number) => () => void; /** * Cancels any pending or scheduled hydration. */ readonly cancel: () => void; /** * Resets the hydration state to pending. * Allows re-hydration after error or completion. */ readonly reset: () => void; /** * Props to spread on a container element for the scheduler. * Includes ref callback and data attributes. */ readonly containerProps: DeferredHydrationContainerProps; } /** * Props to spread on the container element. */ export interface DeferredHydrationContainerProps { readonly ref: (element: HTMLElement | null) => void; readonly 'data-hydration-boundary-id': string; readonly 'data-hydration-state': HydrationState; readonly 'data-hydration-deferred': boolean; } /** * Hook for manual control over component hydration. * * Provides explicit control over hydration timing, useful for: * - Components that should only hydrate on user action * - Delayed hydration after initial render * - Conditional hydration based on external factors * - Testing hydration behavior * * @param config - Deferred hydration configuration * @returns Deferred hydration control interface * * @example * ```tsx * // Basic manual hydration * function ManualComponent() { * const { isHydrated, hydrate, containerProps } = useDeferredHydration({ * id: 'manual-section', * onHydrate: async () => { * // Initialize component * await loadData(); * }, * }); * * return ( *
* {isHydrated ? ( * * ) : ( * * )} *
* ); * } * * // Scheduled hydration * function DelayedComponent() { * const { isHydrated, scheduleHydration, cancel } = useDeferredHydration({ * id: 'delayed-section', * }); * * useEffect(() => { * // Hydrate after 3 seconds of visibility * const cancelSchedule = scheduleHydration(3000); * * return cancelSchedule; * }, [scheduleHydration]); * * return ( *
* {isHydrated ? : } * *
* ); * } * * // Conditional hydration * function ConditionalComponent({ shouldHydrate }: { shouldHydrate: boolean }) { * const { isHydrated, hydrate } = useDeferredHydration({ * id: 'conditional-section', * }); * * useEffect(() => { * if (shouldHydrate && !isHydrated) { * hydrate(); * } * }, [shouldHydrate, isHydrated, hydrate]); * * return
{isHydrated ? : }
; * } * ``` */ export declare function useDeferredHydration(config?: DeferredHydrationConfig): UseDeferredHydrationReturn; /** * Hook for creating a simple deferred hydration trigger. * * Simplified version of useDeferredHydration for common use cases. * * @param id - Optional boundary ID * @returns Tuple of [isHydrated, hydrate function] * * @example * ```tsx * function SimpleDeferred() { * const [isHydrated, hydrate] = useSimpleDeferredHydration('my-component'); * * return ( *
* {isHydrated ? ( * * ) : ( * * )} *
* ); * } * ``` */ export declare function useSimpleDeferredHydration(id?: string): [boolean, () => Promise]; /** * Hook for hydration that triggers on idle. * * Schedules hydration to occur during browser idle time. * * @param config - Deferred hydration configuration * @returns Deferred hydration control interface * * @example * ```tsx * function IdleHydratedComponent() { * const { isHydrated } = useIdleHydration({ * id: 'idle-component', * idleTimeout: 5000, * }); * * return
{isHydrated ? : }
; * } * ``` */ export declare function useIdleHydration(config?: DeferredHydrationConfig & { idleTimeout?: number; }): UseDeferredHydrationReturn;