Join our Discord Community

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.

Loading component...

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

Loading component...

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

PropertyTypeDefaultDescription
initializeWithValuebooleantrueWhether to initialize with screen values immediately
debounceDelaynumberundefinedDelay in milliseconds for debounced updates

Screen Properties

PropertyTypeDescription
widthnumberTotal screen width in pixels
heightnumberTotal screen height in pixels
availWidthnumberAvailable screen width (excluding taskbars)
availHeightnumberAvailable screen height (excluding taskbars)
colorDepthnumberColor depth in bits per pixel
pixelDepthnumberPixel depth in bits per pixel
orientationScreenOrientation | nullScreen orientation object (if supported)

ScreenOrientation Properties

PropertyTypeDescription
typestringOrientation type (portrait-primary, landscape-primary, etc.)
anglenumberOrientation 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 when initializeWithValue 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 and useIsomorphicLayoutEffect for optimal performance
  • Proper cleanup is handled automatically when component unmounts
  • Compatible with all modern browsers that expose the window.screen API