Skip to Content
ohooks

useWakeLock

Prevents the device screen from dimming or locking using the Screen Wake Lock API.

Loading preview...

The useWakeLock hook requests and releases a 'screen' wake lock via navigator.wakeLock.request('screen'), and automatically re-acquires the lock when the tab returns to 'visible' after being backgrounded.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useWakeLock.json

Usage

Examples

Declarative Wake Lock Tied to Playback State

Pass a boolean as the first argument to automatically acquire the wake lock when isPlaying is true and release it when false.

Imperative request and release Controls

Omit the boolean argument and use request() and release() directly with error and release callbacks.

API Reference

UseWakeLockOptions

Configuration options for visibility re-acquisition and lifecycle callbacks.

Property
Specification
reacquireOnVisible?
uniondefault:true
Automatically re-acquire the screen wake lock when the document becomes visible again
onError?
function
Callback fired if requesting the wake lock fails.

UseWakeLockReturn

Wake lock status and imperative request / release controls returned by the hook.

Property
Specification
isSupported
union
Whether the Screen Wake Lock API (navigator.wakeLock) is supported.
isLocked
union
Whether a screen wake lock is currently active.
error
union
Most recent error thrown while requesting the wake lock, or null.