Vize

use-wake-lock

Keep the screen awake with the Screen Wake Lock API.

Package @vizejs/composable/use-wake-lock
Own the source vize lib pull composable:use-wake-lock
Runtime exports useWakeLock
Gzip budget 2048 B

Minimal setup

Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).

vp install @vizejs/composable

Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.

import { useWakeLock } from "@vizejs/composable/use-wake-lock";

Try it

Keep the screen on

Prevent the screen from sleeping while something is being read or presented.

Open the interactive example

What to observe: Keep screen on asks the browser for a wake lock; Allow sleep releases it. Some browsers refuse, for example on low battery.

With reacquireOnVisible, the lock is requested again when the page becomes visible. Every example renders a neutral message until it mounts, so server and client markup match, and then explains what to do when the browser lacks the API.

Compare the initial and interacted states
Keep the screen on: initial state
Initial state
Keep the screen on: state after the verified interactions
After the verified interactions

Copy the example

Save this complete SFC in your Vue project. Its public imports and source are the same ones executed in the preview.

<script setup lang="ts">
import { useMounted } from "@vizejs/composable/use-mounted";
import { useWakeLock } from "@vizejs/composable/use-wake-lock";

const mounted = useMounted();
const { supported, active, error, request, release } = useWakeLock();
</script>

<template>
  <div class="composable-example">
    <p>Keep the screen on while a recipe or a slide deck is open.</p>
    <div class="example-actions">
      <button type="button" :disabled="!mounted || !supported || active" @click="() => request()">
        Keep screen on
      </button>
      <button type="button" :disabled="!active" @click="() => release()">Allow sleep</button>
    </div>
    <output aria-live="polite">{{
      !mounted
        ? "Checking browser support…"
        : !supported
          ? "Screen Wake Lock is not available in this browser."
          : active
            ? "The screen stays on."
            : error
              ? "The browser did not keep the screen on."
              : "The screen may turn off."
    }}</output>
  </div>
</template>

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
useWakeLock system experimental deterministic-fallback caller-managed caller, reactive-scope web, desktop window tryOnScopeDispose

API

useWakeLock

Keep the screen awake with the Screen Wake Lock API. Browsers release screen locks when the page is hidden; while a lock is requested, it is re-acquired on the next visibilitychange to visible. Request failures (missing user activation, battery saver) are exposed through error rather than thrown. The lock and the visibility listener are released when the owning reactive scope stops. Server rendering: nothing is requested, supported and active are false.

function useWakeLock(options: UseWakeLockOptions = {}): WakeLockControls
const wakeLock = useWakeLock();
const startPresentation = () => wakeLock.request();

Types

WakeLockSentinelLike

Minimal WakeLockSentinel consumed by useWakeLock.

Member Type Description
released boolean Whether the platform already released this lock.
release () => Promise<void> Release the lock.

WakeLockHost

Capabilities used by useWakeLock.

Member Type Description
wakeLock { /** Request a lock of type. */ request(type: WakeLockKind): Promise<WakeLockSentinelLike>; } Screen Wake Lock API (navigator.wakeLock).
document? { /** Current visibility. / readonly visibilityState: string; /* Subscribe to visibilitychange. / addEventListener(type: "visibilitychange", listener: () => void): void; /* Unsubscribe from visibilitychange. */ removeEventListener(type: "visibilitychange", listener: () => void): void; } | null Document whose visibilitychange re-acquires a lock the platform released while the page was hidden.

UseWakeLockOptions

Options for useWakeLock.

Member Type Description
host? MaybeRefOrGetter<WakeLockHost | null | undefined> Wake Lock capability for alternate runtimes and tests.
reacquireOnVisible? boolean Re-acquire a requested lock when the page becomes visible again (the platform releases screen locks whenever the page is hidden).

WakeLockControls

Reactive state and actions returned by useWakeLock.

Member Type Description
supported ComputedRef<boolean> Whether the Screen Wake Lock API is available.
active Readonly<Ref<boolean>> Whether a lock is currently held.
requested Readonly<Ref<boolean>> Whether the caller wants a lock: set by request, cleared by release. While requested, the lock is re-acquired after visibility returns.
error Readonly<ShallowRef<unknown>> Most recent request failure (for example NotAllowedError), cleared on success.
request (type?: WakeLockKind) => Promise<boolean> Request a wake lock.
release () => Promise<void> Release the lock and stop re-acquiring it. Repeated calls are safe.