react-window-size-listener
Version:
Minimalistic React hook for listening to window resize events with built-in debouncing.
41 lines (40 loc) • 1.28 kB
TypeScript
export interface WindowSize {
width: number;
height: number;
}
export interface UseWindowSizeOptions {
/**
* Amount of time in milliseconds to wait before updating the state after the last resize event.
* Defaults to 100ms.
*/
debounceTime?: number;
}
/**
* Hook that tracks the window size with optional debouncing.
*
* @param options Configuration options
* @returns Object containing `width` and `height` of the window.
*
* @example
* ```tsx
* const { width, height } = useWindowSize();
* const isMobile = width < 768;
* ```
*/
export declare function useWindowSize(options?: UseWindowSizeOptions): WindowSize;
/**
* Hook that tracks the visual viewport size with optional debouncing.
* Uses the Visual Viewport API which accounts for pinch-zoom, on-screen keyboards,
* and browser chrome on mobile devices.
* Falls back to window.innerWidth/Height if Visual Viewport API is not available.
*
* @param options Configuration options
* @returns Object containing `width` and `height` of the visual viewport.
*
* @example
* ```tsx
* const { width, height } = useViewportSize();
* // Accounts for mobile keyboard, pinch-zoom, etc.
* ```
*/
export declare function useViewportSize(options?: UseWindowSizeOptions): WindowSize;