preact-missing-hooks
Version:
A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.
41 lines (40 loc) • 1.51 kB
TypeScript
/** Effective connection type from Network Information API */
export type EffectiveConnectionType = "slow-2g" | "2g" | "3g" | "4g";
/** Network connection type (e.g., wifi, cellular) */
export type ConnectionType = "bluetooth" | "cellular" | "ethernet" | "mixed" | "none" | "other" | "unknown" | "wifi";
export interface NetworkState {
/** Whether the browser is online */
online: boolean;
/** Effective connection type (when supported) */
effectiveType?: EffectiveConnectionType;
/** Estimated downlink speed in Mbps (when supported) */
downlink?: number;
/** Estimated round-trip time in ms (when supported) */
rtt?: number;
/** Whether the user has requested reduced data usage (when supported) */
saveData?: boolean;
/** Connection type (when supported) */
connectionType?: ConnectionType;
}
/**
* A Preact hook that returns the current network state, including online/offline
* status and (when supported) connection type, downlink, RTT, and save-data preference.
* Updates reactively when the network state changes.
*
* @returns The current network state object
*
* @example
* ```tsx
* function NetworkStatus() {
* const { online, effectiveType, saveData } = useNetworkState();
* return (
* <div>
* Status: {online ? 'Online' : 'Offline'}
* {effectiveType && ` (${effectiveType})`}
* {saveData && ' - Reduced data mode'}
* </div>
* );
* }
* ```
*/
export declare function useNetworkState(): NetworkState;