Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions packages/leptos-typed-fallback-show/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions packages/leptos-typed-fallback-show/README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions packages/leptos-typed-fallback-show/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;
26 changes: 26 additions & 0 deletions packages/leptos-typed-fallback-show/src/typed_fallback_show.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use leptos::{either::Either, prelude::*};

/// A `Show` component with typed fallback support.
#[component]
pub fn TypedFallbackShow<F, IV, W, C>(
/// The children will be shown whenever the condition in the `when` closure returns `true`.
children: TypedChildrenFn<C>,
/// 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()),
}
}