yew_notifications/
lib.rs

1//! yew-notifications notifications components library for [Yew](https://yew.rs/).
2//! It's like [react-toastify](https://www.npmjs.com/package/react-toastify) but for [yew](https://yew.rs/) and more simpler.
3//!
4//! The purpose of this library is to provide the ability to easily add notifications to the yew web app.
5//!
6//! # Examples
7//!
8//! * [basic](https://github.com/TheBestTvarynka/yew-notifications/tree/main/examples/basic): shows how to use the `yew-notifications` library and built-in notifications.
9//! * [custom](https://github.com/TheBestTvarynka/yew-notifications/tree/main/examples/custom): shows how to write custom notifications.
10
11mod hook;
12mod manager;
13#[cfg(feature = "standard-notification")]
14mod notification;
15mod provider;
16mod utils;
17
18use std::any::Any;
19
20pub use hook::use_notification;
21pub use manager::NotificationsManager;
22#[cfg(feature = "standard-notification")]
23pub use notification::{
24    Notification, NotificationComponent, NotificationComponentProps, NotificationFactory, NotificationType,
25};
26pub use provider::{NotificationsPosition, NotificationsProvider, NotificationsProviderProps};
27use time::Duration;
28use uuid::Uuid;
29use yew::{Callback, Html, MouseEvent};
30
31/// This trait provides an interface for the notification. Everything,
32/// that implements this trait, can be used in the [`NotificationsProvider`].
33///
34/// # Lifetime
35///
36/// Every notification has such thing as *lifetime*.
37/// This is simply the amount of time that this notification is still "alive" (which means is present on the screen or is visible).
38/// Methods like [`Notifiable::apply_tick`], [`Notifiable::is_alive`], etc, are used by library internal to control life of the notification.
39pub trait Notifiable: Any {
40    /// Returns the id of the notification. Every notification has the id of the Uuid type and it should be unique.
41    fn id(&self) -> Uuid;
42
43    /// Applies some amount of time to this notification.
44    ///
45    /// # Arguments
46    ///
47    /// * `time` - An amount of time that has been spent.
48    fn apply_tick(&mut self, time: Duration);
49
50    /// Check if the notification is still "alive".
51    /// If it returns false, then this notification will be deleted (disappeared) on the next time tick.
52    fn is_alive(&self) -> bool;
53
54    /// Check if the notification is still "paused".
55    /// It means, that when notification is paused (this function returns true)
56    /// then time does not affect the notification lifetime.
57    fn is_paused(&self) -> bool;
58
59    /// This function calls when the mouse enters this notification.
60    fn mouse_in(&mut self);
61
62    /// This function calls when the mouse leaves this notification.
63    fn mouse_out(&mut self);
64}
65
66/// This trait provides an interface for the notification component factory.
67///
68/// This trait implementors are used be `yew-notifications` for notification components rendering.
69pub trait NotifiableComponentFactory<T: Notifiable> {
70    /// Creates a new notification component that can be rendered in `yew`.
71    fn component(
72        &self,
73        notification: T,
74        onclick: Callback<MouseEvent>,
75        onenter: Callback<MouseEvent>,
76        onleave: Callback<MouseEvent>,
77    ) -> Html;
78}