30% offEnding soon
useWakeLockLoading saved progress…

useWakeLock

A screen wake lock asks the device to keep its display awake while a visible page needs the user's attention. Implement a React hook around the Screen Wake Lock API that reports support and active state, exposes stable request and release operations, and follows browser-initiated release. The hook must also handle overlapping requests and component cleanup without leaking a granted lock.

Signature

function useWakeLock(): {
  isSupported: boolean;
  isActive: boolean;
  error: Error | null;
  request: () => Promise<void>;
  release: () => Promise<void>;
}

Examples

const { result } = renderHook(() => useWakeLock());

await act(async () => result.current.request());
// navigator.wakeLock.request was called with 'screen'
// result.current.isActive === true
await act(async () => result.current.release());
// The held sentinel was released.
// result.current.isActive === false

Notes

  • Feature detectionisSupported becomes true only when navigator.wakeLock.request is a function; the initial server-safe state is false.
  • One lock at a time — repeated calls while active do nothing, and concurrent calls await one shared native request.
  • Release events — a browser or system release must clear the held sentinel and set isActive to false.
  • Failures — store request failures as Error objects and reject request; a failed manual release stays active and rejects release.
  • Cleanup — detach the exact listener and release a held sentinel on unmount. If a pending request resolves after unmount, release that late sentinel immediately.
  • Scope — request only the screen lock type. Do not add visibility-based reacquisition, permission checks, or battery logic.