From c87b989c3c520a441ac94b82d3c0fca3d879def4 Mon Sep 17 00:00:00 2001 From: luoxiaozero <48741584+luoxiaozero@users.noreply.github.com> Date: Sun, 22 Sep 2024 18:08:24 +0800 Subject: [PATCH] feat: change the any_view prop of dispatch_toast to children (#267) --- thaw/src/menu/docs/mod.md | 4 ++-- thaw/src/tag/docs/mod.md | 4 ++-- thaw/src/toast/docs/mod.md | 8 ++++---- thaw/src/toast/mod.rs | 24 +++++++++++++++--------- thaw/src/toast/toaster.rs | 20 ++++++++++---------- thaw/src/upload/docs/mod.md | 4 ++-- 6 files changed, 35 insertions(+), 29 deletions(-) diff --git a/thaw/src/menu/docs/mod.md b/thaw/src/menu/docs/mod.md index 4bd2e24..925bee7 100644 --- a/thaw/src/menu/docs/mod.md +++ b/thaw/src/menu/docs/mod.md @@ -5,13 +5,13 @@ let toaster = ToasterInjection::expect_context(); let on_select = move |key: String| { leptos::logging::warn!("{}", key); - toaster.dispatch_toast(view! { + toaster.dispatch_toast(move || view! { "key" - }.into_any(), Default::default()); + }, Default::default()); }; view! { diff --git a/thaw/src/tag/docs/mod.md b/thaw/src/tag/docs/mod.md index 502d9f9..d324517 100644 --- a/thaw/src/tag/docs/mod.md +++ b/thaw/src/tag/docs/mod.md @@ -38,14 +38,14 @@ view! { let toaster = ToasterInjection::expect_context(); let on_dismiss = move |_| { - toaster.dispatch_toast(view! { + toaster.dispatch_toast(move || view! { "Tag" "Tag dismiss" - }.into_any(), Default::default()); + }, Default::default()); }; view! { diff --git a/thaw/src/toast/docs/mod.md b/thaw/src/toast/docs/mod.md index 37bbb04..5020c2c 100644 --- a/thaw/src/toast/docs/mod.md +++ b/thaw/src/toast/docs/mod.md @@ -4,7 +4,7 @@ let toaster = ToasterInjection::expect_context(); let on_click = move |_| { - toaster.dispatch_toast(view! { + toaster.dispatch_toast(move || view! { "Email sent" @@ -19,7 +19,7 @@ let on_click = move |_| { // Action - }.into_any(), Default::default()); + }, Default::default()); }; view! { @@ -33,7 +33,7 @@ view! { let toaster = ToasterInjection::expect_context(); fn dispatch_toast(toaster: ToasterInjection, position: ToastPosition) { - toaster.dispatch_toast(view! { + toaster.dispatch_toast(move || view! { "Email sent" @@ -48,7 +48,7 @@ fn dispatch_toast(toaster: ToasterInjection, position: ToastPosition) { // Action - }.into_any(), ToastOptions::default().with_position(position)); + }, ToastOptions::default().with_position(position)); }; view! { diff --git a/thaw/src/toast/mod.rs b/thaw/src/toast/mod.rs index 19931e3..9ed0c8a 100644 --- a/thaw/src/toast/mod.rs +++ b/thaw/src/toast/mod.rs @@ -13,12 +13,11 @@ pub use toaster_provider::*; use leptos::prelude::*; use std::sync::mpsc::{channel, Receiver, Sender, TryIter}; -use tachys::view::any_view::AnyView; use wasm_bindgen::UnwrapThrowExt; #[derive(Clone, Copy)] pub struct ToasterInjection { - sender: StoredValue, ToastOptions)>>, + sender: StoredValue>, trigger: StoredValue, } @@ -28,7 +27,7 @@ impl ToasterInjection { } pub fn channel() -> (Self, ToasterReceiver) { - let (sender, receiver) = channel::<(AnyView, ToastOptions)>(); + let (sender, receiver) = channel::<(Children, ToastOptions)>(); let trigger = ArcTrigger::new(); ( @@ -40,24 +39,31 @@ impl ToasterInjection { ) } - pub fn dispatch_toast(&self, any_view: AnyView, options: ToastOptions) { - self.sender - .with_value(|sender| sender.send((any_view, options)).unwrap_throw()); + pub fn dispatch_toast(&self, children: C, options: ToastOptions) + where + C: FnOnce() -> IV + Send + 'static, + IV: IntoView + 'static, + { + self.sender.with_value(|sender| { + sender + .send((Box::new(move || children().into_any()), options)) + .unwrap_throw() + }); self.trigger.with_value(|trigger| trigger.notify()); } } pub struct ToasterReceiver { - receiver: Receiver<(AnyView, ToastOptions)>, + receiver: Receiver<(Children, ToastOptions)>, trigger: ArcTrigger, } impl ToasterReceiver { - pub fn new(receiver: Receiver<(AnyView, ToastOptions)>, trigger: ArcTrigger) -> Self { + pub fn new(receiver: Receiver<(Children, ToastOptions)>, trigger: ArcTrigger) -> Self { Self { receiver, trigger } } - pub fn try_recv(&self) -> TryIter<'_, (AnyView, ToastOptions)> { + pub fn try_recv(&self) -> TryIter<'_, (Children, ToastOptions)> { self.trigger.track(); self.receiver.try_iter() } diff --git a/thaw/src/toast/toaster.rs b/thaw/src/toast/toaster.rs index 23f47bd..9461254 100644 --- a/thaw/src/toast/toaster.rs +++ b/thaw/src/toast/toaster.rs @@ -1,6 +1,6 @@ use super::{ToastOptions, ToastPosition, ToasterReceiver}; use crate::ConfigInjection; -use leptos::{either::Either, html, prelude::*, tachys::view::any_view::AnyView}; +use leptos::{either::Either, html, prelude::*}; use send_wrapper::SendWrapper; use std::{collections::HashMap, time::Duration}; use thaw_components::{CSSTransition, Teleport}; @@ -21,7 +21,7 @@ pub fn Toaster( let bottom_id_list = RwSignal::>::new(Default::default()); let bottom_start_id_list = RwSignal::>::new(Default::default()); let bottom_end_id_list = RwSignal::>::new(Default::default()); - let toasts = StoredValue::>, ToastOptions)>>::new( + let toasts = StoredValue::, ToastOptions)>>::new( Default::default(), ); @@ -77,7 +77,7 @@ pub fn Toaster( .flatten() { Either::Left( - view! { }, + view! { }, ) } else { Either::Right(()) @@ -91,7 +91,7 @@ pub fn Toaster( .flatten() { Either::Left( - view! { }, + view! { }, ) } else { Either::Right(()) @@ -105,7 +105,7 @@ pub fn Toaster( .flatten() { Either::Left( - view! { }, + view! { }, ) } else { Either::Right(()) @@ -119,7 +119,7 @@ pub fn Toaster( .flatten() { Either::Left( - view! { }, + view! { }, ) } else { Either::Right(()) @@ -133,7 +133,7 @@ pub fn Toaster( .flatten() { Either::Left( - view! { }, + view! { }, ) } else { Either::Right(()) @@ -147,7 +147,7 @@ pub fn Toaster( .flatten() { Either::Left( - view! { }, + view! { }, ) } else { Either::Right(()) @@ -161,9 +161,9 @@ pub fn Toaster( #[component] fn ToasterContainer( - view: AnyView, options: ToastOptions, #[prop(into)] on_close: StoredValue>, + children: Children, ) -> impl IntoView { let container_ref = NodeRef::::new(); let is_show = RwSignal::new(true); @@ -210,7 +210,7 @@ fn ToasterContainer( let:_ >
- {view} + {children()}
} diff --git a/thaw/src/upload/docs/mod.md b/thaw/src/upload/docs/mod.md index 334bd2a..b833c15 100644 --- a/thaw/src/upload/docs/mod.md +++ b/thaw/src/upload/docs/mod.md @@ -28,13 +28,13 @@ let toaster = ToasterInjection::expect_context(); let custom_request = move |file_list: FileList| { let len = file_list.length(); - toaster.dispatch_toast(view! { + toaster.dispatch_toast(move || view! { {format!("Number of uploaded files: {len}")} - }.into_any(), Default::default()); + }, Default::default()); }; view! {