yew_notifications/notification/
component.rs

1use yew::{classes, function_component, html, Callback, Html, MouseEvent, Properties};
2
3use crate::utils::format_date_time;
4use crate::{Notifiable, Notification};
5
6/// Props for [`NotificationComponent`]
7#[derive(Properties, Clone, PartialEq)]
8pub struct NotificationComponentProps {
9    /// Notification object to render.
10    pub notification: Notification,
11
12    /// *onclick* event callback.
13    pub onclick: Callback<MouseEvent>,
14
15    /// *onenter* event callback.
16    pub onenter: Callback<MouseEvent>,
17
18    /// *onleave* event callback.
19    pub onleave: Callback<MouseEvent>,
20}
21
22/// Standard notification component.
23#[function_component(NotificationComponent)]
24pub fn notification_component(props: &NotificationComponentProps) -> Html {
25    let title = props.notification.title.as_ref();
26    let text = &props.notification.text;
27    let notification_type = &props.notification.notification_type;
28    let spawn_time = &props.notification.spawn_time;
29
30    let onclick = props.onclick.clone();
31    let onenter = props.onenter.clone();
32    let onleave = props.onleave.clone();
33
34    let mut classes = vec![classes!("notification"), notification_type.into()];
35    if props.notification.is_paused() {
36        classes.push(classes!("paused"));
37    }
38
39    html! {
40        <div {onclick} onmouseenter={onenter} onmouseleave={onleave} class={classes}>
41            {if let Some(title) = title {
42                html! { <span class={classes!("notification-title")}>{title}</span> }
43            } else {
44                html! {}
45            }}
46            <span>{text}</span>
47            <span class={classes!("time")}>{format_date_time(spawn_time)}</span>
48        </div>
49    }
50}