useScreen
Custom hook that tracks the screen dimensions and properties with debouncing support. Perfect for responsive React applications requiring display information with Next.js integration and TypeScript support.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
npx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-screen.json
Features
- Screen properties tracking providing comprehensive display information including dimensions and color depth
- Orientation support with automatic updates for device rotation and screen changes
- Debounced updates allowing performance optimization for high-frequency resize events
- SSR compatibility with proper server-side rendering support and initialization options
- TypeScript overloads providing type-safe usage patterns for different initialization modes
- Memory efficient using proper cleanup and event listener management
- shadcn/ui integration ideal for responsive layouts and device-specific components
Examples
Advanced Configuration with Debouncing
Demonstrates debounced updates, calculated values, and comprehensive screen property monitoring.
Use Cases
This free open source React component works well for:
- Responsive design - Adapt layouts based on screen resolution and available space with Next.js
- Device detection - Identify display capabilities for optimal user experience using TypeScript
- Orientation handling - Respond to device rotation and screen changes with shadcn/ui components
- Performance monitoring - Track screen properties for analytics and optimization using Tailwind CSS
- Multi-monitor support - Handle different display configurations and properties
API Reference
useScreen (SSR-safe)
useScreen(options: { initializeWithValue: false }): Screen | undefined
Returns undefined
initially, suitable for server-side rendering.
useScreen (Client-side)
useScreen(options?: Partial<UseScreenOptions<true>>): Screen
Returns Screen
object immediately, suitable for client-side only usage.
UseScreenOptions
Property | Type | Default | Description |
---|---|---|---|
initializeWithValue | boolean | true | Whether to initialize with screen values immediately |
debounceDelay | number | undefined | Delay in milliseconds for debounced updates |
Screen Properties
Property | Type | Description |
---|---|---|
width | number | Total screen width in pixels |
height | number | Total screen height in pixels |
availWidth | number | Available screen width (excluding taskbars) |
availHeight | number | Available screen height (excluding taskbars) |
colorDepth | number | Color depth in bits per pixel |
pixelDepth | number | Pixel depth in bits per pixel |
orientation | ScreenOrientation | null | Screen orientation object (if supported) |
ScreenOrientation Properties
Property | Type | Description |
---|---|---|
type | string | Orientation type (portrait-primary, landscape-primary, etc.) |
angle | number | Orientation angle in degrees (0, 90, 180, 270) |
Usage Patterns
Basic Screen Information
import { useScreen } from '@repo/use-screen';
function DisplayInfo() {
const screen = useScreen();
if (!screen) return <div>Loading screen info...</div>;
return (
<div>
Screen: {screen.width} × {screen.height}
Available: {screen.availWidth} × {screen.availHeight}
</div>
);
}
SSR-Safe Usage
const screen = useScreen({ initializeWithValue: false });
return (
<div>
{screen ? (
<span>Screen: {screen.width}×{screen.height}</span>
) : (
<span>Screen info loading...</span>
)}
</div>
);
Debounced Updates
const screen = useScreen({
debounceDelay: 300,
initializeWithValue: true
});
// Screen updates are debounced by 300ms
Responsive Breakpoints
const screen = useScreen();
const getScreenSize = (width: number) => {
if (width < 768) return 'mobile';
if (width < 1024) return 'tablet';
return 'desktop';
};
const screenSize = screen ? getScreenSize(screen.width) : 'unknown';
Implementation Notes
- Hook automatically listens to window resize events for screen property updates
- Returns
undefined
during SSR wheninitializeWithValue
is false - Screen object is shallow cloned on updates to ensure proper re-renders
- Debouncing is disabled by default for retro-compatibility with immediate updates
- Orientation information is only available on browsers that support
screen.orientation
- Hook uses
useEventListener
anduseIsomorphicLayoutEffect
for optimal performance - Proper cleanup is handled automatically when component unmounts
- Compatible with all modern browsers that expose the
window.screen
API
useResizeObserver
Custom hook that observes the size of an element using the ResizeObserver API. Perfect for responsive React applications requiring dynamic layout adjustments with Next.js integration and TypeScript support.
useScript
Custom hook that dynamically loads scripts and tracks their loading status with caching and cleanup support. Perfect for React applications requiring third-party libraries with Next.js integration and TypeScript support.