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::*;
|
2024-02-09 03:18:53 +00:00
|
|
|
/// # 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(
|
2024-02-09 03:18:53 +00:00
|
|
|
/// || js!("getBattery" in &window().navigator())
|
2023-05-29 01:52:03 +01:00
|
|
|
/// );
|
|
|
|
///
|
2023-06-21 13:09:00 +02: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
|
|
|
/// # }
|
|
|
|
/// ```
|
2024-07-23 21:33:14 -06:00
|
|
|
pub fn use_supported(callback: impl Fn() -> bool + Send + Sync + 'static) -> Signal<bool> {
|
2024-02-09 03:18:53 +00:00
|
|
|
#[cfg(feature = "ssr")]
|
|
|
|
{
|
|
|
|
let _ = callback;
|
|
|
|
Signal::derive(|| false)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "ssr"))]
|
|
|
|
{
|
|
|
|
Signal::derive(callback)
|
|
|
|
}
|
2023-05-29 01:52:03 +01:00
|
|
|
}
|