Skip to content

Conversation

geoffreygarrett
Copy link
Contributor

Adds TypedFallbackShow component that enables proper attribute spreading in Leptos 0.7 when working with fallback content. Works around current AnyView limitations in Leptos 0.7.2 for type-safe conditional rendering.

@geoffreygarrett
Copy link
Contributor Author

Precisely this solves, in conjunction with #14, the following pattern:

#[component]
pub fn Primitive<E, C>(
    element: fn() -> HtmlElement<E, (), ()>,
    children: TypedChildrenFn<C>,
    #[prop(optional, into)] as_child: MaybeProp<bool>,
    #[prop(optional, into)] node_ref: AnyNodeRef,
) -> impl IntoView
where
    E: ElementType + 'static,
    C: IntoView + 'static,
    View<C>: RenderHtml,
    HtmlElement<E, (), ()>: ElementChild<View<C>>,
    <HtmlElement<E, (), ()> as ElementChild<View<C>>>::Output: IntoView,
    <E as ElementType>::Output: JsCast,
    AnyNodeRef: NodeRefContainer<E>,
{
    let children = StoredValue::new(children.into_inner());
    view! {
        <TypedFallbackShow
            when=move || as_child.get().unwrap_or_default()
            fallback=move || {
                element().child(children.with_value(|children| children())).add_any_attr(any_node_ref(node_ref))
            }
        >
            {children.with_value(|children| children()).add_any_attr(any_node_ref(node_ref))}
        </TypedFallbackShow>
    }
}

This enables safe type-preserving attribute spreading across both the default and fallback branches, while allowing node reference reuse.

@geoffreygarrett geoffreygarrett changed the title feat(typed-fallback-show): add type-safe fallback show component Add type-safe fallback show component (RustForWeb/radix#398) Jan 6, 2025
@geoffreygarrett geoffreygarrett changed the title Add type-safe fallback show component (RustForWeb/radix#398) Add type-safe fallback show component Jan 6, 2025
@geoffreygarrett geoffreygarrett changed the title Add type-safe fallback show component Add TypedFallbackShow component Jan 6, 2025
@DanielleHuisman DanielleHuisman changed the title Add TypedFallbackShow component Add TypedFallbackShow component Feb 10, 2025
@DanielleHuisman DanielleHuisman force-pushed the feature/typed-fallback-show branch from 9edcc97 to 4774430 Compare February 10, 2025 13:53
@DanielleHuisman DanielleHuisman merged commit 89967f1 into RustForWeb:main Feb 10, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants