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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
use dioxus::prelude::*;
use freya_elements::elements as dioxus_elements;
use freya_hooks::{
use_animation,
use_applied_theme,
AnimNum,
Ease,
Function,
SnackBarTheme,
SnackBarThemeWith,
};
/// `SnackBar` component. Use in combination with other components.
///
/// # Styling
/// Inherits the [`SnackBarTheme`](freya_hooks::SnackBarTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// fn app() -> Element {
/// let mut show = use_signal(|| false);
///
/// rsx!(
/// rect {
/// height: "100%",
/// width: "100%",
/// Button {
/// onpress: move |_| show.toggle(),
/// label { "Open" }
/// }
/// SnackBar {
/// show,
/// label {
/// "Hello, World!"
/// }
/// }
/// }
/// )
/// }
/// ```
#[allow(non_snake_case)]
#[component]
pub fn SnackBar(
/// Inner children of the SnackBar.
children: Element,
/// Signal to show the snackbar or not.
show: Signal<bool>,
/// Theme override.
theme: Option<SnackBarThemeWith>,
) -> Element {
let animation = use_animation(|ctx| {
ctx.with(
AnimNum::new(50., 0.)
.time(200)
.ease(Ease::Out)
.function(Function::Expo),
)
});
use_effect(move || {
if *show.read() {
animation.start();
} else if animation.peek_has_run_yet() {
animation.reverse();
}
});
let offset_y = animation.get().read().as_f32();
rsx!(
rect {
width: "100%",
height: "40",
position: "absolute",
position_bottom: "0",
offset_y: "{offset_y}",
overflow: "clip",
SnackBarBox {
theme,
{children}
}
}
)
}
#[doc(hidden)]
#[allow(non_snake_case)]
#[component]
pub fn SnackBarBox(children: Element, theme: Option<SnackBarThemeWith>) -> Element {
let SnackBarTheme { background, color } = use_applied_theme!(&theme, snackbar);
rsx!(
rect {
width: "fill",
height: "40",
background: "{background}",
overflow: "clip",
padding: "10",
color: "{color}",
direction: "horizontal",
{children}
}
)
}
#[cfg(test)]
mod test {
use std::time::Duration;
use dioxus::prelude::use_signal;
use freya::prelude::*;
use freya_testing::prelude::*;
use tokio::time::sleep;
#[tokio::test]
pub async fn snackbar() {
fn snackbar_app() -> Element {
let mut show = use_signal(|| false);
rsx!(
rect {
height: "100%",
width: "100%",
Button {
onpress: move |_| show.toggle(),
label { "Open" }
}
SnackBar {
show,
label {
"Hello, World!"
}
}
}
)
}
let mut utils = launch_test(snackbar_app);
let root = utils.root();
let snackbar_box = root.get(0).get(1).get(0);
utils.wait_for_update().await;
// Snackbar is closed.
assert!(!snackbar_box.is_visible());
// Open the snackbar by clicking at the button
utils.push_event(PlatformEvent::Mouse {
name: EventName::Click,
cursor: (15.0, 15.0).into(),
button: Some(MouseButton::Left),
});
// Wait a bit for the snackbar to show up
utils.wait_for_update().await;
utils.wait_for_update().await;
sleep(Duration::from_millis(15)).await;
utils.wait_for_update().await;
// Snackbar is visible.
assert!(snackbar_box.is_visible());
}
}