useWindowSize
Window dimension tracking with debouncing and SSR support. Perfect for React applications requiring responsive design with Next.js integration and TypeScript support.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/use-window-size.json
npx shadcn@latest add https://www.shadcn.io/registry/use-window-size.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-window-size.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-window-size.json
Features
- Real-time dimension tracking with automatic window resize event handling
- Debouncing support with configurable delay to optimize performance during rapid resize events
- SSR compatibility designed for Next.js server-side rendering with proper hydration handling
- TypeScript overloads with complete type safety for both SSR and CSR usage patterns
- Memory efficient with optimized event listeners and automatic cleanup on unmount
- Flexible initialization supporting both immediate values and undefined states for server environments
Examples
Advanced Responsive Dashboard
Comprehensive demo showing debouncing options, breakpoint detection, and responsive design context with real-time metrics.
Use Cases
This free open source React hook works well for:
- Responsive components - Dynamic layouts based on viewport dimensions with Next.js and TypeScript
- Media queries in JavaScript - Programmatic breakpoint handling using React state management
- Canvas and visualization - Dynamic sizing for charts with shadcn/ui component integration
- Mobile-first design - Viewport-aware components with Tailwind CSS responsive utilities
- Performance optimization - Debounced resize handling for JavaScript-intensive applications
- Dashboard layouts - Adaptive grid systems with real-time dimension awareness
API Reference
useWindowSize
SSR Version (returns undefined initially):
useWindowSize(options: { initializeWithValue: false }): WindowSize
CSR Version (returns numbers immediately):
useWindowSize(options?: Partial<UseWindowSizeOptions<true>>): WindowSize<number>
Options
Option | Type | Default | Description |
---|---|---|---|
initializeWithValue | boolean | true | Whether to initialize with actual window dimensions (false for SSR) |
debounceDelay | number | undefined | Debounce delay in milliseconds for resize events |
Return Value
Property | Type | Description |
---|---|---|
width | number | undefined | Current window width in pixels |
height | number | undefined | Current window height in pixels |
Implementation Notes
- Hook automatically detects server environment and disables initialization for SSR compatibility
- Resize events are attached to window object with proper cleanup on component unmount
- Debouncing uses the existing useDebounceCallback hook for consistent performance optimization
- Initial dimensions are set using useIsomorphicLayoutEffect to prevent hydration mismatches
- TypeScript overloads ensure type safety between SSR and CSR usage patterns
- Window dimensions are captured from
window.innerWidth
andwindow.innerHeight
properties