2023-07-14 22:43:19 +01:00
|
|
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
|
|
|
|
2023-07-07 23:34:50 +01:00
|
|
|
use crate::use_event_listener;
|
2023-07-14 22:43:19 +01:00
|
|
|
use cfg_if::cfg_if;
|
2023-07-07 23:34:50 +01:00
|
|
|
use leptos::ev::visibilitychange;
|
|
|
|
use leptos::*;
|
|
|
|
|
|
|
|
/// Reactively track `document.visibilityState`
|
|
|
|
///
|
|
|
|
/// ## Demo
|
|
|
|
///
|
|
|
|
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_document_visibility)
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use leptos::*;
|
|
|
|
/// # use leptos_use::use_document_visibility;
|
|
|
|
/// #
|
|
|
|
/// # #[component]
|
|
|
|
/// # fn Demo(cx: Scope) -> impl IntoView {
|
|
|
|
/// let visibility = use_document_visibility(cx);
|
|
|
|
/// #
|
|
|
|
/// # view! { cx, }
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2023-07-14 22:43:19 +01:00
|
|
|
///
|
|
|
|
/// ## Server-Side Rendering
|
|
|
|
///
|
|
|
|
/// On the server this returns a `Signal` that always contains the value `web_sys::VisibilityState::Hidden`.
|
2023-07-07 23:34:50 +01:00
|
|
|
pub fn use_document_visibility(cx: Scope) -> Signal<web_sys::VisibilityState> {
|
2023-07-14 22:43:19 +01:00
|
|
|
cfg_if! { if #[cfg(feature = "ssr")] {
|
|
|
|
let inital_visibility = web_sys::VisibilityState::Hidden;
|
|
|
|
} else {
|
|
|
|
let inital_visibility = document().visibility_state();
|
|
|
|
}}
|
|
|
|
|
|
|
|
let (visibility, set_visibility) = create_signal(cx, inital_visibility);
|
2023-07-07 23:34:50 +01:00
|
|
|
|
2023-07-14 22:43:19 +01:00
|
|
|
cfg_if! { if #[cfg(not(feature = "ssr"))] {
|
|
|
|
let _ = use_event_listener(cx, document(), visibilitychange, move |_| {
|
|
|
|
set_visibility.set(document().visibility_state());
|
|
|
|
});
|
|
|
|
}}
|
2023-07-07 23:34:50 +01:00
|
|
|
|
|
|
|
visibility.into()
|
|
|
|
}
|