Wake Lock

The Wake Lock component provides an interface to the Screen Wake Lock API, enabling your Progressive Web App to prevent devices from dimming or locking the screen when the app needs to keep running. This is particularly useful for applications that require continuous user attention without interaction.

This component is useful for applications such as:

  • Recipe or cooking apps that need to keep instructions visible

  • Presentation or slideshow applications

  • Video or audio playback interfaces

  • Reading apps for long-form content

  • Fitness or workout apps displaying exercise instructions

  • Navigation or map applications

Browser Support

The Screen Wake Lock API is supported in most modern browsers including Chrome, Edge, and Safari. Always check browser compatibility and handle the case where the API is not available.

circle-info

Wake locks are automatically released when the page is not visible or when the user minimizes the browser.

Usage

Basic Wake Lock Toggle

<div {{ stimulus_controller('@pwa/wake-lock') }}>
    <button {{ stimulus_action('@pwa/wake-lock', 'toggle', 'click') }}>
        Toggle Screen Wake Lock
    </button>

    <p id="status">Wake lock status: <span>Not active</span></p>
</div>

<script>
    document.addEventListener('pwa--wake-lock:updated', (event) => {
        const status = event.detail.wakeLock;
        const statusText = status && !status.released ? 'Active' : 'Not active';
        document.querySelector('#status span').textContent = statusText;
    });
</script>

Explicit Lock and Unlock

Recipe App Example

Parameters

None

Actions

lock

Requests a screen wake lock to prevent the device from dimming or locking the screen.

If the wake lock cannot be obtained (e.g., browser doesn't support the API or user denied permission), the action fails silently. Listen to the updated event to check the actual state.

unlock

Releases the current wake lock, allowing the screen to dim and lock normally.

toggle

Toggles the wake lock state. If currently locked, it unlocks. If unlocked, it locks.

This is convenient for checkbox or toggle button implementations.

Targets

None

Events

pwa--wake-lock:updated

Dispatched whenever the wake lock state changes (locked, unlocked, or released by the system).

Payload: {wakeLock}

  • wakeLock is null when no wake lock is active

  • wakeLock is an object when active, containing:

    • type (string): The type of wake lock (usually "screen")

    • released (boolean): Whether the wake lock has been released

Example:

Best Practices

  1. Always provide UI feedback: Show users when the wake lock is active so they understand why their screen isn't dimming

  2. Make it optional: Allow users to enable/disable the wake lock based on their preference

  3. Handle visibility changes: The browser automatically releases wake locks when the page is hidden, but you should handle this gracefully

  4. Don't abuse it: Only use wake locks when truly necessary for the user experience

  5. Provide a toggle: Use the toggle action with checkboxes or switches for better UX

  6. Consider battery impact: Inform users that keeping the screen awake will consume more battery

Common Pitfalls

  • Wake lock requires user interaction: The lock can only be requested after a user gesture (click, touch, etc.)

  • Automatic release: Wake locks are automatically released when the page becomes hidden (user switches tabs or minimizes browser)

  • No persistent wake locks: Wake locks don't persist across page reloads

  • HTTPS required: Like most modern web APIs, the Screen Wake Lock API requires a secure context (HTTPS)

Complete Example

Here's a complete example for a meditation timer app:

Last updated

Was this helpful?