leptos-use/src/use_supported.rs

35 lines
692 B
Rust
Raw Normal View History

2024-05-07 12:41:44 +01:00
use leptos::prelude::*;
2023-05-29 01:52:03 +01:00
/// SSR compatibe `is_supported`
///
/// ## Usage
///
/// ```
2024-05-07 12:41:44 +01:00
/// # use leptos::prelude::*;
/// # use leptos_use::{use_supported, js};
2023-05-29 01:52:03 +01:00
/// # use wasm_bindgen::JsValue;
/// #
2023-07-27 18:06:36 +01:00
/// # pub fn Demo() -> impl IntoView {
2023-05-29 01:52:03 +01:00
/// let is_supported = use_supported(
/// || js!("getBattery" in &window().navigator())
2023-05-29 01:52:03 +01:00
/// );
///
/// if is_supported.get() {
2023-05-29 01:52:03 +01:00
/// // do something
/// }
2023-07-27 18:06:36 +01:00
/// # view! { }
2023-05-29 01:52:03 +01:00
/// # }
/// ```
2023-07-27 18:06:36 +01:00
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)
}
2023-05-29 01:52:03 +01:00
}