leptos_use/
use_supported.rs

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
use leptos::*;

/// SSR compatibe `is_supported`
///
/// ## Usage
///
/// ```
/// # use leptos::*;
/// # use leptos_use::{use_supported, js};
/// # use wasm_bindgen::JsValue;
/// #
/// # pub fn Demo() -> impl IntoView {
/// let is_supported = use_supported(
///     || js!("getBattery" in &window().navigator())
/// );
///
/// if is_supported.get() {
///     // do something
/// }
/// #    view! { }
/// # }
/// ```
pub fn use_supported(callback: impl Fn() -> bool + 'static) -> Signal<bool> {
    #[cfg(feature = "ssr")]
    {
        let _ = callback;
        Signal::derive(|| false)
    }

    #[cfg(not(feature = "ssr"))]
    {
        Signal::derive(callback)
    }
}