Skip to content

DOM Visible

Reactive document.visibilityState

๐ŸŽฌ Usage

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

    const visible = dom_visible()
</script>

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

โ†ฉ๏ธ Returns

A subscribable store that contains the current visibility state.

๐Ÿงช Playground

Source Code ๐Ÿ‘€

Source Code
import type { Readable } from "svelte/store"

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

import { on } from "../event_listener"

/**
 * Reactive `document.visibilityState`
 *
 * @returns A readable store with the current visibility state.
 */
export function dom_visible(): Readable<DocumentVisibilityState> {
    if (!browser) return to_writable("hidden")

    const { subscribe, set } = to_writable<DocumentVisibilityState>(
        document.visibilityState
    )

    function handler() {
        if (document) set(document.visibilityState)
    }

    on(document, "visibilitychange", handler, true)

    return { subscribe }
}

Last update: 2023-03-09