@sv-use/core
Version:
A collection of Svelte 5 utilities.
22 lines (21 loc) • 814 B
JavaScript
import { handleEventListener } from '../handle-event-listener/index.svelte.js';
import { noop } from '../__internal__/utils.svelte.js';
import { defaultDocument } from '../__internal__/configurable.js';
/**
* Whether the document is visible or not.
* @see https://svelte-librarian.github.io/sv-use/docs/core/get-document-visibility
*/
export function getDocumentVisibility(options = {}) {
const { autoCleanup = true, document = defaultDocument } = options;
let cleanup = noop;
let _current = $state(document?.visibilityState ?? 'visible');
if (document) {
cleanup = handleEventListener(document, 'visibilitychange', () => (_current = document.visibilityState), { autoCleanup });
}
return {
get current() {
return _current;
},
cleanup
};
}