impulse_thaw/toast/
toast.rs

1use leptos::prelude::*;
2use std::time::Duration;
3use thaw_utils::{class_list, ArcOneCallback};
4
5#[component]
6pub fn Toast(
7    #[prop(optional, into)] class: MaybeProp<String>,
8    children: Children,
9) -> impl IntoView {
10    view! { <div class=class_list!["thaw-toast", class]>{children()}</div> }
11}
12
13#[derive(Default, Clone, Copy)]
14pub enum ToastPosition {
15    Top,
16    TopStart,
17    TopEnd,
18    Bottom,
19    BottomStart,
20    #[default]
21    BottomEnd,
22}
23
24impl ToastPosition {
25    pub fn as_str(&self) -> &'static str {
26        match self {
27            Self::Top => "top",
28            Self::TopStart => "top-start",
29            Self::TopEnd => "top-dnc",
30            Self::Bottom => "bottom",
31            Self::BottomStart => "bottom-start",
32            Self::BottomEnd => "bottom-end",
33        }
34    }
35}
36
37#[derive(Debug, Default, Clone, Copy)]
38pub enum ToastIntent {
39    Success,
40    #[default]
41    Info,
42    Warning,
43    Error,
44}
45
46#[derive(Debug, Clone, PartialEq)]
47pub enum ToastStatus {
48    Mounted,
49    Unmounted,
50}
51
52#[derive(Clone)]
53pub struct ToastOptions {
54    pub(crate) id: uuid::Uuid,
55    pub(crate) position: Option<ToastPosition>,
56    pub(crate) timeout: Option<Duration>,
57    pub(crate) intent: Option<ToastIntent>,
58    pub(crate) on_status_change: Option<ArcOneCallback<ToastStatus>>,
59}
60
61impl Default for ToastOptions {
62    fn default() -> Self {
63        Self {
64            id: uuid::Uuid::new_v4(),
65            position: None,
66            timeout: None,
67            intent: None,
68            on_status_change: None,
69        }
70    }
71}
72
73impl ToastOptions {
74    /// The id that will be assigned to this toast.
75    pub fn with_id(mut self, id: uuid::Uuid) -> Self {
76        self.id = id;
77        self
78    }
79
80    /// The position the toast should render.
81    pub fn with_position(mut self, position: ToastPosition) -> Self {
82        self.position = Some(position);
83        self
84    }
85
86    /// Auto dismiss timeout in milliseconds.
87    pub fn with_timeout(mut self, timeout: Duration) -> Self {
88        self.timeout = Some(timeout);
89        self
90    }
91
92    /// Intent.
93    pub fn with_intent(mut self, intent: ToastIntent) -> Self {
94        self.intent = Some(intent);
95        self
96    }
97
98    /// Status change callback.
99    pub fn with_on_status_change(
100        mut self,
101        on_status_change: impl Fn(ToastStatus) + Send + Sync + 'static,
102    ) -> Self {
103        self.on_status_change = Some(on_status_change.into());
104        self
105    }
106}