diff --git a/Cargo.lock b/Cargo.lock index b38e0da..edda2b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -757,6 +757,13 @@ dependencies = [ "leptos", ] +[[package]] +name = "leptos-typed-fallback-show" +version = "0.0.4" +dependencies = [ + "leptos", +] + [[package]] name = "leptos_config" version = "0.7.5" diff --git a/README.md b/README.md index f7c66fc..e170950 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Documentation for the crates is available on [Docs.rs](https://docs.rs/): - [`leptos-struct-component`](https://docs.rs/leptos-struct-component/latest/leptos_struct_component/) - [`leptos-struct-component-macro`](https://docs.rs/leptos-struct-component-macro/latest/leptos_struct_component_macro/) - [`leptos-style`](https://docs.rs/leptos-style/latest/leptos_style/) +- [`leptos-typed-fallback-show`](https://docs.rs/leptos-typed-fallback-show/latest/leptos_typed_fallback_show/) ## License diff --git a/packages/leptos-typed-fallback-show/Cargo.toml b/packages/leptos-typed-fallback-show/Cargo.toml new file mode 100644 index 0000000..63ce23e --- /dev/null +++ b/packages/leptos-typed-fallback-show/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "leptos-typed-fallback-show" +description = "A Show component for Leptos with typed fallback support." +authors.workspace = true +edition.workspace = true +license.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] +leptos.workspace = true diff --git a/packages/leptos-typed-fallback-show/README.md b/packages/leptos-typed-fallback-show/README.md new file mode 100644 index 0000000..e7e4ee4 --- /dev/null +++ b/packages/leptos-typed-fallback-show/README.md @@ -0,0 +1,15 @@ +# Leptos Typed Fallback Show + +A `Show` component for [Leptos](https://leptos.dev/) with typed fallback support. + +## Documentation + +Documentation for the crate is available on [Docs.rs](https://docs.rs/): + +- [`leptos-typed-fallback-show`](https://docs.rs/leptos-typed-fallback-show/latest/leptos_typed_fallback_show/) + +## Rust For Web + +The Leptos Typed Fallback Show project is part of [Rust For Web](https://github.com/RustForWeb). + +[Rust For Web](https://github.com/RustForWeb) creates and ports web UI libraries for Rust. All projects are free and open source. diff --git a/packages/leptos-typed-fallback-show/src/lib.rs b/packages/leptos-typed-fallback-show/src/lib.rs new file mode 100644 index 0000000..249100e --- /dev/null +++ b/packages/leptos-typed-fallback-show/src/lib.rs @@ -0,0 +1,5 @@ +//! A `Show` component for [Leptos](https://leptos.dev/) with typed fallback support. + +mod typed_fallback_show; + +pub use typed_fallback_show::*; diff --git a/packages/leptos-typed-fallback-show/src/typed_fallback_show.rs b/packages/leptos-typed-fallback-show/src/typed_fallback_show.rs new file mode 100644 index 0000000..c57b02a --- /dev/null +++ b/packages/leptos-typed-fallback-show/src/typed_fallback_show.rs @@ -0,0 +1,26 @@ +use leptos::{either::Either, prelude::*}; + +/// A `Show` component with typed fallback support. +#[component] +pub fn TypedFallbackShow( + /// The children will be shown whenever the condition in the `when` closure returns `true`. + children: TypedChildrenFn, + /// A closure that returns a bool that determines whether this thing runs + when: W, + /// A closure that returns what gets rendered if the when statement is false. By default this is the empty view. + fallback: F, +) -> impl IntoView +where + W: Fn() -> bool + Send + Sync + 'static, + F: Fn() -> IV + Send + Sync + 'static, + IV: IntoView + 'static, + C: IntoView + 'static, +{ + let memoized_when = ArcMemo::new(move |_| when()); + let children = children.into_inner(); + + move || match memoized_when.get() { + true => Either::Left(children()), + false => Either::Right(fallback().into_view()), + } +}