injectDocumentVisibility
injectDocumentVisibility
is a utility function that provides a reactive signal reflecting the current visibility state of the document. This function is particularly useful for scenarios such as tracking user presence on a webpage (e.g., for analytics or pausing/resuming activities) and can be adapted for use with iframes or in testing environments.
Usage
injectDocumentVisibility
accepts an optional parameter options
which can include a custom document
and an Injector
instance. The document
parameter is particularly useful for testing scenarios or when needing to track the visibility of an iframe. The injector
allows for dependency injection, providing more flexibility and facilitating testable code by decoupling from the global state or context.
API
Parameters
options
(optional): An object that can have the following properties:document
: A customDocument
instance, defaulting to the globaldocument
object.injector
: AnInjector
instance for Angular’s dependency injection.
Returns
Signal<DocumentVisibilityState>
: A reactive signal that emits the currentDocumentVisibilityState
(e.g.,"visible"
,"hidden"
) and updates when the document visibility state changes.