fn

useWakeLock

v0.0.14testeddemo

Reactive wrapper over the Screen Wake Lock API to keep the screen awake. Re-acquires a deferred lock automatically when the document returns to visible.

Examples

ts
const { isSupported, isActive, request, release } = useWakeLock();
await request('screen');
// ...later
await release();
ts
// forceRequest re-acquires immediately, dropping any existing lock
const { forceRequest } = useWakeLock();
await forceRequest('screen');

Demo

Loading demo…

Signature

ts
export function useWakeLock(options: UseWakeLockOptions ={ ... }

Parameters

ParameterTypeDescription
options?UseWakeLockOptionsOptions (custom navigator, document)

Returns

UseWakeLockReturn{ isSupported, sentinel, isActive, request, forceRequest, release }
PropertyTypeDescription
isSupportedComputedRef<boolean>Whether the Screen Wake Lock API is supported.
sentinelShallowRef<WakeLockSentinel | null>The current WakeLockSentinel, or null when no lock is held.
isActiveComputedRef<boolean>Whether a wake lock is currently held AND the document is visible.
request(type: WakeLockType) => Promise<void>Request a wake lock. If the document is hidden, the request is deferred and automatically (re)acquired once the document becomes visible again.
forceRequest(type: WakeLockType) => Promise<void>Request a wake lock immediately, releasing any existing one first. Will reject (via onError) if the document is hidden.
release() => Promise<void>Release the current wake lock and cancel any deferred request.