zino_dioxus/feedback/notification.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 58 59 60 61 62 63 64 65 66 67 68
use crate::class::Class;
use dioxus::prelude::*;
use std::time::Duration;
use zino_core::SharedString;
/// A simple colored block meant to draw the attention to the user about something.
pub fn Notification(props: NotificationProps) -> Element {
let mut hidden = use_signal(|| props.hidden);
if props.duration > 0 {
spawn(async move {
tokio::time::sleep(Duration::from_millis(props.duration)).await;
hidden.set(true);
});
}
if hidden() {
return rsx! {};
}
rsx! {
div {
class: "{props.class}",
class: if !props.color.is_empty() { "is-{props.color}" },
class: if !props.theme.is_empty() { "is-{props.theme}" },
position: "fixed",
top: "4rem",
right: "0.75rem",
z_index: 99,
if props.on_close.is_some() {
button {
r#type: "button",
class: props.close_class,
onclick: move |event| {
if let Some(handler) = props.on_close.as_ref() {
handler.call(event);
}
}
}
}
{ props.children }
}
}
}
/// The [`Notification`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct NotificationProps {
/// The class attribute for the component.
#[props(into, default = "notification")]
pub class: Class,
/// A class to apply to the `close` button element.
#[props(into, default = "delete")]
pub close_class: Class,
/// The color of the notification: `primary` | `link` | `info` | `success` | `warning` | `danger`.
#[props(into, default)]
pub color: SharedString,
/// The theme of the notification: `light`.
#[props(into, default)]
pub theme: SharedString,
/// An event handler to be called when the `close` button is clicked.
pub on_close: Option<EventHandler<MouseEvent>>,
/// A flag to determine whether the notification is hidden or not.
#[props(default)]
pub hidden: bool,
/// A duration in milliseconds.
#[props(default = 3000)]
pub duration: u64,
/// The children to render within the component.
children: Element,
}