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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_elements::events::MouseEvent;
use freya_hooks::{use_animation, use_get_theme, Animation};
/// [`Switch`] component properties.
#[derive(Props)]
pub struct SwitchProps<'a> {
/// Whether the `Switch` is enabled or not.
pub enabled: bool,
/// Handler for the `ontoggled` event.
pub ontoggled: EventHandler<'a, ()>,
}
/// Controlled `Switch` component.
///
/// # Props
/// See [`SwitchProps`].
///
/// # Styling
/// Inherits the [`SwitchTheme`](freya_hooks::SwitchTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// fn app(cx: Scope) -> Element {
/// let enabled = use_state(&cx, || false);
///
/// render!(
/// Switch {
/// enabled: *enabled.get(),
/// ontoggled: |_| {
/// enabled.set(!enabled.get());
/// }
/// }
/// )
/// }
/// ```
///
#[allow(non_snake_case)]
pub fn Switch<'a>(cx: Scope<'a, SwitchProps<'a>>) -> Element<'a> {
let animation = use_animation(cx, || 0.0);
let theme = use_get_theme(cx);
let hovering = use_state(cx, || false);
let clicking = use_state(cx, || false);
let onmouseleave = |_: MouseEvent| {
if !(*clicking.get()) {
hovering.set(false);
}
};
let onmouseover = |_: MouseEvent| {
hovering.set(true);
};
let onmousedown = |_: MouseEvent| {
clicking.set(true);
};
let onclick = |_: MouseEvent| {
clicking.set(false);
cx.props.ontoggled.call(());
};
let (offset_x, border, circle) = {
if cx.props.enabled {
(
animation.value(),
theme.switch.enabled_background,
theme.switch.enabled_thumb_background,
)
} else {
(
animation.value(),
theme.switch.background,
theme.switch.thumb_background,
)
}
};
use_memo(cx, &cx.props.enabled, move |enabled| {
if enabled {
animation.start(Animation::new_sine_in_out(0.0..=25.0, 200));
} else if animation.value() > 0.0 {
animation.start(Animation::new_sine_in_out(25.0..=0.0, 200));
}
});
render!(
rect {
margin: "1.5",
width: "50",
height: "25",
padding: "1",
corner_radius: "50",
background: "{border}",
onmousedown: onmousedown,
onmouseover: onmouseover,
onmouseleave: onmouseleave,
onclick: onclick,
rect {
width: "100%",
height: "100%",
offset_x: "{offset_x}",
padding: "2.5",
corner_radius: "50",
rect {
background: "{circle}",
direction: "both",
width: "18",
height: "18",
corner_radius: "50",
}
}
}
)
}