Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 1x 79x 2x 4x 1x 3x 61x 2x 59x | import type { PageVisibilityState } from "./types";
/**
* The inert page-visibility value returned during server-side rendering and in
* any environment without a `document`. The page is optimistically treated as
* **visible** — this is the value most apps want on the server and it avoids a
* hydration mismatch on the client's first paint of a foreground tab.
*/
export const SERVER_PAGE_VISIBILITY = true;
/**
* Whether a `document` object is available (i.e. running in a browser-like
* environment rather than SSR).
*
* @returns `true` when `document` is defined.
*
* @example
* ```ts
* if (isDocumentAvailable()) {
* // Safe to read document.visibilityState
* }
* ```
*/
export function isDocumentAvailable(): boolean {
return typeof document !== "undefined";
}
/**
* Whether the Page Visibility API is available in the current environment.
*
* @returns `true` when `document.visibilityState` is present.
*
* @example
* ```ts
* const supported = isPageVisibilitySupported();
* ```
*/
export function isPageVisibilitySupported(): boolean {
return isDocumentAvailable() && "visibilityState" in document;
}
/**
* Read the raw {@link PageVisibilityState} from `document.visibilityState`.
*
* Anything that is not explicitly `"hidden"` is reported as `"visible"`, so
* legacy `"prerender"`/`"unloaded"` values collapse to `"visible"`.
*
* @returns `"visible"` or `"hidden"`. Returns `"visible"` under SSR / without a
* `document`, matching the optimistic server default.
*
* @example
* ```ts
* const state = getVisibilityState(); // "visible" | "hidden"
* ```
*/
export function getVisibilityState(): PageVisibilityState {
if (!isDocumentAvailable()) {
return "visible";
}
return document.visibilityState === "hidden" ? "hidden" : "visible";
}
/**
* Read the current page visibility as a boolean, derived from
* `document.visibilityState`.
*
* @returns `true` when the page is visible (foreground), `false` when hidden.
* Returns {@link SERVER_PAGE_VISIBILITY} (`true`) under SSR / without a
* `document`.
*
* @example
* ```ts
* if (getPageVisibility()) {
* // the page is in the foreground
* }
* ```
*/
export function getPageVisibility(): boolean {
if (!isDocumentAvailable()) {
return SERVER_PAGE_VISIBILITY;
}
return document.visibilityState !== "hidden";
}
|