mirror of
https://github.com/adoyle0/thaw.git
synced 2025-02-08 19:03:09 -05:00
Compare commits
No commits in common. "50bd30de1d68cf7fc5b8ddea96a65602496089aa" and "e18bbff2165f451225e1fda7f647a6e22f5488e6" have entirely different histories.
50bd30de1d
...
e18bbff216
5 changed files with 13 additions and 103 deletions
|
@ -113,7 +113,7 @@ pub fn OverlayDrawer(
|
||||||
}
|
}
|
||||||
node_ref=drawer_ref
|
node_ref=drawer_ref
|
||||||
role="dialog"
|
role="dialog"
|
||||||
aria-modal={if modal_type == DrawerModalType::Modal {"true"} else {"false"}}
|
aria-modal="true"
|
||||||
>
|
>
|
||||||
{children()}
|
{children()}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -102,12 +102,6 @@ let toaster = ToasterInjection::expect_context();
|
||||||
let id = uuid::Uuid::new_v4();
|
let id = uuid::Uuid::new_v4();
|
||||||
|
|
||||||
|
|
||||||
let mounted = RwSignal::new(false);
|
|
||||||
|
|
||||||
let on_status_change = move |status| {
|
|
||||||
mounted.set(status == ToastStatus::Mounted);
|
|
||||||
};
|
|
||||||
|
|
||||||
let dispatch = move |_| {
|
let dispatch = move |_| {
|
||||||
toaster.dispatch_toast(move || view! {
|
toaster.dispatch_toast(move || view! {
|
||||||
<Toast>
|
<Toast>
|
||||||
|
@ -119,7 +113,7 @@ let dispatch = move |_| {
|
||||||
</ToastBodySubtitle>
|
</ToastBodySubtitle>
|
||||||
</ToastBody>
|
</ToastBody>
|
||||||
</Toast>
|
</Toast>
|
||||||
},ToastOptions::default().with_id(id).with_on_status_change(on_status_change))
|
},ToastOptions::default().with_id(id));
|
||||||
};
|
};
|
||||||
|
|
||||||
let dismiss = move |_| {
|
let dismiss = move |_| {
|
||||||
|
@ -127,45 +121,8 @@ let dismiss = move |_| {
|
||||||
};
|
};
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
{move || {if !mounted.get() {
|
<Button on_click=dispatch>"Show toast"</Button>
|
||||||
view!{<Button on_click=dispatch>"Show toast"</Button>}
|
<Button on_click=dismiss>"Hide toast"</Button>
|
||||||
} else {
|
|
||||||
view!{<Button on_click=dismiss>"Hide toast"</Button>}
|
|
||||||
}
|
|
||||||
}}}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Dismiss All
|
|
||||||
|
|
||||||
```rust demo
|
|
||||||
let toaster = ToasterInjection::expect_context();
|
|
||||||
|
|
||||||
fn dispatch_toast(toaster: ToasterInjection) {
|
|
||||||
toaster.dispatch_toast(move || view! {
|
|
||||||
<Toast>
|
|
||||||
<ToastTitle>"Email sent"</ToastTitle>
|
|
||||||
<ToastBody>
|
|
||||||
"This is a toast body"
|
|
||||||
<ToastBodySubtitle slot>
|
|
||||||
"Subtitle"
|
|
||||||
</ToastBodySubtitle>
|
|
||||||
</ToastBody>
|
|
||||||
<ToastFooter>
|
|
||||||
"Footer"
|
|
||||||
</ToastFooter>
|
|
||||||
</Toast>
|
|
||||||
}, ToastOptions::default());
|
|
||||||
};
|
|
||||||
|
|
||||||
let dismiss_all = move || {
|
|
||||||
toaster.dismiss_all();
|
|
||||||
};
|
|
||||||
|
|
||||||
view! {
|
|
||||||
<Space>
|
|
||||||
<Button on_click=move |_| dispatch_toast(toaster)>"Dispatch toast"</Button>
|
|
||||||
<Button on_click=move |_| dismiss_all()>"Dismiss all"</Button>
|
|
||||||
</Space>
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -203,12 +160,11 @@ view! {
|
||||||
|
|
||||||
### ToastOptions Props
|
### ToastOptions Props
|
||||||
|
|
||||||
| Name | Type | Description |
|
| Name | Type | Description |
|
||||||
| --------------------- | ----------------------------------------------------- | ------------------------------------- |
|
| ------------- | --------------------------------------- | ------------------------------------- |
|
||||||
| with_position | `Fn(mut self, position: ToastPosition)` | The position the toast should render. |
|
| with_position | `Fn(mut self, position: ToastPosition)` | The position the toast should render. |
|
||||||
| with_timeout | `Fn(mut self, timeout: Duration)` | Auto dismiss timeout in milliseconds. |
|
| with_timeout | `Fn(mut self, timeout: Duration)` | Auto dismiss timeout in milliseconds. |
|
||||||
| with_intent | `Fn(mut self, intent: ToastIntent)` | The intent of the toast. |
|
| with_intent | `Fn(mut self, intent: ToastIntent)` | The intent of the toast. |
|
||||||
| with_on_status_change | `Fn(mut self, on_status_change: Fn(ToastStatus))` | The intent of the toast. |
|
|
||||||
|
|
||||||
### Toast & ToastFooter Props
|
### Toast & ToastFooter Props
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,6 @@ pub struct ToasterInjection {
|
||||||
enum ToasterMessage {
|
enum ToasterMessage {
|
||||||
Dispatch(Children, ToastOptions),
|
Dispatch(Children, ToastOptions),
|
||||||
Dismiss(uuid::Uuid),
|
Dismiss(uuid::Uuid),
|
||||||
DismissAll,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ToasterInjection {
|
impl ToasterInjection {
|
||||||
|
@ -54,15 +53,6 @@ impl ToasterInjection {
|
||||||
self.trigger.with_value(|trigger| trigger.notify());
|
self.trigger.with_value(|trigger| trigger.notify());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dismiss_all(&self) {
|
|
||||||
self.sender.with_value(|sender| {
|
|
||||||
sender
|
|
||||||
.send(ToasterMessage::DismissAll)
|
|
||||||
.unwrap_throw()
|
|
||||||
});
|
|
||||||
self.trigger.with_value(|trigger| trigger.notify());
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn dispatch_toast<C, IV>(&self, children: C, options: ToastOptions)
|
pub fn dispatch_toast<C, IV>(&self, children: C, options: ToastOptions)
|
||||||
where
|
where
|
||||||
C: FnOnce() -> IV + Send + 'static,
|
C: FnOnce() -> IV + Send + 'static,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use thaw_utils::{class_list, ArcOneCallback};
|
use thaw_utils::class_list;
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn Toast(
|
pub fn Toast(
|
||||||
|
@ -43,19 +43,12 @@ pub enum ToastIntent {
|
||||||
Error,
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum ToastStatus {
|
|
||||||
Mounted,
|
|
||||||
Unmounted,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ToastOptions {
|
pub struct ToastOptions {
|
||||||
pub(crate) id: uuid::Uuid,
|
pub(crate) id: uuid::Uuid,
|
||||||
pub(crate) position: Option<ToastPosition>,
|
pub(crate) position: Option<ToastPosition>,
|
||||||
pub(crate) timeout: Option<Duration>,
|
pub(crate) timeout: Option<Duration>,
|
||||||
pub(crate) intent: Option<ToastIntent>,
|
pub(crate) intent: Option<ToastIntent>,
|
||||||
pub(crate) on_status_change: Option<ArcOneCallback<ToastStatus>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ToastOptions {
|
impl Default for ToastOptions {
|
||||||
|
@ -65,7 +58,6 @@ impl Default for ToastOptions {
|
||||||
position: None,
|
position: None,
|
||||||
timeout: None,
|
timeout: None,
|
||||||
intent: None,
|
intent: None,
|
||||||
on_status_change: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,13 +86,4 @@ impl ToastOptions {
|
||||||
self.intent = Some(intent);
|
self.intent = Some(intent);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Status change callback.
|
|
||||||
pub fn with_on_status_change(
|
|
||||||
mut self,
|
|
||||||
on_status_change: impl Fn(ToastStatus) + Send + Sync + 'static,
|
|
||||||
) -> Self {
|
|
||||||
self.on_status_change = Some(on_status_change.into());
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use super::{ToastIntent, ToastOptions, ToastPosition, ToasterReceiver};
|
use super::{ToastIntent, ToastOptions, ToastPosition, ToasterReceiver};
|
||||||
use crate::{toast::ToasterMessage, ConfigInjection, ToastStatus};
|
use crate::{toast::ToasterMessage, ConfigInjection};
|
||||||
use leptos::{context::Provider, either::Either, html, prelude::*};
|
use leptos::{context::Provider, either::Either, html, prelude::*};
|
||||||
use send_wrapper::SendWrapper;
|
use send_wrapper::SendWrapper;
|
||||||
use std::{collections::HashMap, time::Duration};
|
use std::{collections::HashMap, time::Duration};
|
||||||
|
@ -51,14 +51,10 @@ pub fn Toaster(
|
||||||
if options.intent.is_none() {
|
if options.intent.is_none() {
|
||||||
options.intent = Some(intent);
|
options.intent = Some(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
let list = id_list(&options.position.unwrap_throw());
|
let list = id_list(&options.position.unwrap_throw());
|
||||||
let id = options.id;
|
let id = options.id;
|
||||||
let is_show = owner.with(|| RwSignal::new(true));
|
let is_show = owner.with(|| RwSignal::new(true));
|
||||||
|
|
||||||
if let Some(on_status_change) = options.on_status_change.clone() {
|
|
||||||
on_status_change(ToastStatus::Mounted)
|
|
||||||
}
|
|
||||||
|
|
||||||
toasts.update_value(|map| {
|
toasts.update_value(|map| {
|
||||||
map.insert(id, (SendWrapper::new(view), options, is_show));
|
map.insert(id, (SendWrapper::new(view), options, is_show));
|
||||||
});
|
});
|
||||||
|
@ -70,18 +66,7 @@ pub fn Toaster(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ToasterMessage::Dismiss(toast_id) => {
|
ToasterMessage::Dismiss(toast_id) => {
|
||||||
toast_show_list.with_value(|map| {
|
toast_show_list.with_value(|map| map.get(&toast_id).unwrap_throw().set(false));
|
||||||
if let Some(is_show) = map.get(&toast_id) {
|
|
||||||
is_show.set(false)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
ToasterMessage::DismissAll => {
|
|
||||||
toast_show_list.with_value(|map| {
|
|
||||||
for is_show in map.values() {
|
|
||||||
is_show.set(false)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -253,7 +238,6 @@ fn ToasterContainer(
|
||||||
timeout,
|
timeout,
|
||||||
position,
|
position,
|
||||||
intent,
|
intent,
|
||||||
on_status_change,
|
|
||||||
..
|
..
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
|
@ -282,9 +266,6 @@ fn ToasterContainer(
|
||||||
f(id, position);
|
f(id, position);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if let Some(on_status_change) = on_status_change.clone() {
|
|
||||||
on_status_change(ToastStatus::Unmounted);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
|
|
Loading…
Add table
Reference in a new issue