Skip to main content

leptos/
show.rs

1use crate::{
2    children::{TypedChildrenFn, ViewFn},
3    IntoView,
4};
5use leptos_macro::component;
6use reactive_graph::{computed::ArcMemo, traits::Get};
7use tachys::either::Either;
8
9#[component(transparent)]
10pub fn Show<W, C>(
11    /// The children will be shown whenever the condition in the `when` closure returns `true`.
12    children: TypedChildrenFn<C>,
13    /// A closure that returns a bool that determines whether this thing runs
14    when: W,
15    /// A closure that returns what gets rendered if the when statement is false. By default this is the empty view.
16    #[prop(optional, into)]
17    fallback: ViewFn,
18) -> impl IntoView
19where
20    W: Fn() -> bool + Send + Sync + 'static,
21    C: IntoView + 'static,
22{
23    let memoized_when = ArcMemo::new(move |_| when());
24    let children = children.into_inner();
25
26    move || match memoized_when.get() {
27        true => Either::Left(children()),
28        false => Either::Right(fallback.run()),
29    }
30}