leptos_use/use_toggle.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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
use crate::core::MaybeRwSignal;
use leptos::*;
/// A boolean switcher with utility functions.
///
/// ## Demo
///
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_toggle)
///
/// ## Usage
///
/// ```
/// # use leptos::*;
/// # use leptos_use::{use_toggle, UseToggleReturn};
/// #
/// # #[component]
/// # fn Demo() -> impl IntoView {
/// let UseToggleReturn { toggle, value, set_value } = use_toggle(true);
/// #
/// # view! { }
/// # }
/// ```
///
/// ## See also
///
/// * [`fn@crate::use_cycle_list`]
// #[doc(cfg(feature = "use_toggle"))]
pub fn use_toggle(
initial_value: impl Into<MaybeRwSignal<bool>>,
) -> UseToggleReturn<impl Fn() + Clone + 'static> {
let initial_value = initial_value.into();
let (value, set_value) = initial_value.into_signal();
let toggle = move || {
set_value.update(|v| *v = !*v);
};
UseToggleReturn {
toggle,
value,
set_value,
}
}
/// Return type of [`fn@crate::use_toggle`].
// #[doc(cfg(feature = "use_toggle"))]
pub struct UseToggleReturn<F>
where
F: Fn() + Clone + 'static,
{
/// Toggles the value between `true` and `false`.
pub toggle: F,
/// The current value as signal.
pub value: Signal<bool>,
/// Sets the current value to the given value.
pub set_value: WriteSignal<bool>,
}