Skip to main content

useWakeLock

The useWakeLock hook keeps the screen awake with the Screen Wake Lock API — useful for recipes, presentations, video players, or navigation. Browsers drop the lock when the tab is hidden; the hook automatically re-acquires it when the tab becomes visible again.

Usage​

Installation​

import { useWakeLock } from "hookstorm";

Parameters​

This hook does not take any parameters.

Return Values​

  • isSupported — true if the browser supports the Wake Lock API.
  • isActive — true while a wake lock is held.
  • request() — Requests the lock. Returns a promise that resolves to true on success.
  • release() — Releases the lock and stops re-acquiring it.

Example Usage​

import { ReactElement } from "react";
import { useWakeLock } from "hookstorm";

export default function Recipe(): ReactElement {
const { isSupported, isActive, request, release } = useWakeLock();

if (!isSupported) return <p>Your browser cannot keep the screen on.</p>;

return (
<button onClick={isActive ? release : request}>
{isActive ? "Allow screen to sleep" : "Keep screen on"}
</button>
);
}

Notes​

  • The request can be refused by the browser (for example when the tab is not visible or the battery is low). In that case request resolves to false.
  • The lock is released automatically when the component unmounts.
  • Requires a secure context (https:// or localhost).