typescriptintermediate
useFullscreen Hook
Toggle fullscreen mode on an element using the Fullscreen API with state tracking.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useState, useCallback, useEffect, RefObject } from 'react';
export function useFullscreen<T extends HTMLElement>(ref: RefObject<T | null>) {
const [isFullscreen, setIsFullscreen] = useState(false);
const enter = useCallback(async () => {
try {
await ref.current?.requestFullscreen();
} catch { /* ignore */ }
}, [ref]);
const exit = useCallback(async () => {
try {
await document.exitFullscreen();
} catch { /* ignore */ }
}, []);
const toggle = useCallback(() => {
isFullscreen ? exit() : enter();
}, [isFullscreen, enter, exit]);
useEffect(() => {
const handler = () => setIsFullscreen(!!document.fullscreenElement);
document.addEventListener('fullscreenchange', handler);
return () => document.removeEventListener('fullscreenchange', handler);
}, []);
return { isFullscreen, enter, exit, toggle };
}Use Cases
- Video players
- Presentation mode
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
usePermission Hook
Queries and tracks browser permission state for features like notifications, camera, and geolocation.
Best for: Permission-gated features
#hooks#permissions
typescriptintermediate
useGeolocation Hook
Accesses browser geolocation API with loading, error states, and watch mode support.
Best for: Location-based features
#hooks#geolocation
typescriptbeginner
usePageVisibility Hook
Detects when the page is hidden or visible using the Page Visibility API.
Best for: Pausing animations
#hooks#visibility
typescriptintermediate
useScreenWakeLock Hook
Prevents the screen from turning off using the Screen Wake Lock API.
Best for: Video playback
#hooks#wake-lock