Skip to content

Window Focus

Reactive window focus.

๐ŸŽฌ Usage

<script>
    import { window_focus } from "@sveu/browser"

    const focused = window_focus()
</script>

๐Ÿ‘ฉโ€๐Ÿ’ปAPI

โ†ฉ๏ธ Returns

A readable boolean store with the current window focus state.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
import { browser, to_readable, to_writable } from "@sveu/shared"

import { on } from "../event_listener"

/**
 * Reactively track window focus with `window.onfocus` and `window.onblur`.
 *
 * @returns A readable store with the current window focus state.
 */
export function window_focus() {
    if (!browser) return to_readable(false)

    const { set, subscribe } = to_writable(window.document.hasFocus())

    on(window, "blur", () => set(false))

    on(window, "focus", () => set(true))

    return { subscribe }
}

Last update: 2023-03-09