leptos 0.8.19

Leptos is a full-stack, isomorphic Rust web framework leveraging fine-grained reactivity to build declarative user interfaces.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::into_view::{IntoView, View};
use std::{
    fmt::{self, Debug},
    sync::Arc,
};
use tachys::view::{
    any_view::{AnyView, IntoAny},
    fragment::{Fragment, IntoFragment},
    RenderHtml,
};

/// The most common type for the `children` property on components,
/// which can only be called once.
///
/// This does not support iterating over individual nodes within the children.
/// To iterate over children, use [`ChildrenFragment`].
pub type Children = Box<dyn FnOnce() -> AnyView + Send>;

/// A type for the `children` property on components that can be called only once,
/// and provides a collection of all the children passed to this component.
pub type ChildrenFragment = Box<dyn FnOnce() -> Fragment + Send>;

/// A type for the `children` property on components that can be called
/// more than once.
pub type ChildrenFn = Arc<dyn Fn() -> AnyView + Send + Sync>;

/// A type for the `children` property on components that can be called more than once,
/// and provides a collection of all the children passed to this component.
pub type ChildrenFragmentFn = Arc<dyn Fn() -> Fragment + Send>;

/// A type for the `children` property on components that can be called
/// more than once, but may mutate the children.
pub type ChildrenFnMut = Box<dyn FnMut() -> AnyView + Send>;

/// A type for the `children` property on components that can be called more than once,
/// but may mutate the children, and provides a collection of all the children
/// passed to this component.
pub type ChildrenFragmentMut = Box<dyn FnMut() -> Fragment + Send>;

// This is to still support components that accept `Box<dyn Fn() -> AnyView>` as a children.
type BoxedChildrenFn = Box<dyn Fn() -> AnyView + Send>;

/// This trait can be used when constructing a component that takes children without needing
/// to know exactly what children type the component expects. This is used internally by the
/// `view!` macro implementation, and can also be used explicitly when using the builder syntax.
///
///
/// Different component types take different types for their `children` prop, some of which cannot
/// be directly constructed. Using `ToChildren` allows the component user to pass children without
/// explicitly constructing the correct type.
///
/// ## Examples
///
/// ```
/// # use leptos::prelude::*;
/// # use leptos::html::p;
/// # use leptos::IntoView;
/// # use leptos_macro::component;
/// # use leptos::children::ToChildren;
/// use leptos::context::{Provider, ProviderProps};
/// use leptos::control_flow::{Show, ShowProps};
///
/// #[component]
/// fn App() -> impl IntoView {
///     (
///       Provider(
///         ProviderProps::builder()
///             .children(ToChildren::to_children(|| {
///                 p().child("Foo")
///             }))
///             // ...
///            .value("Foo")
///            .build(),
///        ),
///        Show(
///          ShowProps::builder()
///             .children(ToChildren::to_children(|| {
///                 p().child("Foo")
///             }))
///             // ...
///             .when(|| true)
///             .fallback(|| p().child("foo"))
///             .build(),
///        )
///     )
/// }
pub trait ToChildren<F> {
    /// Convert the provided type (generally a closure) to Self (generally a "children" type,
    /// e.g., [Children]). See the implementations to see exactly which input types are supported
    /// and which "children" type they are converted to.
    fn to_children(f: F) -> Self;
}

/// Compiler optimisation, can be used with certain type to avoid unique closures in the view!{} macro.
pub struct ChildrenOptContainer<T>(pub T);

impl<F, C> ToChildren<F> for Children
where
    F: FnOnce() -> C + Send + 'static,
    C: RenderHtml + Send + 'static,
{
    #[inline]
    fn to_children(f: F) -> Self {
        Box::new(move || f().into_any())
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for Children
where
    T: IntoAny + Send + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        Box::new(move || t.0.into_any())
    }
}

impl<F, C> ToChildren<F> for ChildrenFn
where
    F: Fn() -> C + Send + Sync + 'static,
    C: RenderHtml + Send + 'static,
{
    #[inline]
    fn to_children(f: F) -> Self {
        Arc::new(move || f().into_any())
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for ChildrenFn
where
    T: IntoAny + Clone + Send + Sync + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        Arc::new(move || t.0.clone().into_any())
    }
}

impl<F, C> ToChildren<F> for ChildrenFnMut
where
    F: Fn() -> C + Send + 'static,
    C: RenderHtml + Send + 'static,
{
    #[inline]
    fn to_children(f: F) -> Self {
        Box::new(move || f().into_any())
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for ChildrenFnMut
where
    T: IntoAny + Clone + Send + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        Box::new(move || t.0.clone().into_any())
    }
}

impl<F, C> ToChildren<F> for BoxedChildrenFn
where
    F: Fn() -> C + Send + 'static,
    C: RenderHtml + Send + 'static,
{
    #[inline]
    fn to_children(f: F) -> Self {
        Box::new(move || f().into_any())
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for BoxedChildrenFn
where
    T: IntoAny + Clone + Send + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        Box::new(move || t.0.clone().into_any())
    }
}

impl<F, C> ToChildren<F> for ChildrenFragment
where
    F: FnOnce() -> C + Send + 'static,
    C: IntoFragment,
{
    #[inline]
    fn to_children(f: F) -> Self {
        Box::new(move || f().into_fragment())
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for ChildrenFragment
where
    T: IntoAny + Send + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        Box::new(move || Fragment::new(vec![t.0.into_any()]))
    }
}

impl<F, C> ToChildren<F> for ChildrenFragmentFn
where
    F: Fn() -> C + Send + 'static,
    C: IntoFragment,
{
    #[inline]
    fn to_children(f: F) -> Self {
        Arc::new(move || f().into_fragment())
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for ChildrenFragmentFn
where
    T: IntoAny + Clone + Send + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        Arc::new(move || Fragment::new(vec![t.0.clone().into_any()]))
    }
}

impl<F, C> ToChildren<F> for ChildrenFragmentMut
where
    F: FnMut() -> C + Send + 'static,
    C: IntoFragment,
{
    #[inline]
    fn to_children(mut f: F) -> Self {
        Box::new(move || f().into_fragment())
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for ChildrenFragmentMut
where
    T: IntoAny + Clone + Send + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        Box::new(move || Fragment::new(vec![t.0.clone().into_any()]))
    }
}

/// New-type wrapper for a function that returns a view with `From` and `Default` traits implemented
/// to enable optional props in for example `<Show>` and `<Suspense>`.
#[derive(Clone)]
pub struct ViewFn(Arc<dyn Fn() -> AnyView + Send + Sync + 'static>);

impl Default for ViewFn {
    fn default() -> Self {
        Self(Arc::new(|| ().into_any()))
    }
}

impl<F, C> From<F> for ViewFn
where
    F: Fn() -> C + Send + Sync + 'static,
    C: RenderHtml + Send + 'static,
{
    fn from(value: F) -> Self {
        Self(Arc::new(move || value().into_any()))
    }
}

impl<C> From<View<C>> for ViewFn
where
    C: Clone + Send + Sync + 'static,
    View<C>: IntoAny,
{
    fn from(value: View<C>) -> Self {
        Self(Arc::new(move || value.clone().into_any()))
    }
}

impl ViewFn {
    /// Execute the wrapped function
    pub fn run(&self) -> AnyView {
        (self.0)()
    }
}

/// New-type wrapper for a function, which will only be called once and returns a view with `From` and
/// `Default` traits implemented to enable optional props in for example `<Show>` and `<Suspense>`.
pub struct ViewFnOnce(Box<dyn FnOnce() -> AnyView + Send + 'static>);

impl Default for ViewFnOnce {
    fn default() -> Self {
        Self(Box::new(|| ().into_any()))
    }
}

impl<F, C> From<F> for ViewFnOnce
where
    F: FnOnce() -> C + Send + 'static,
    C: RenderHtml + Send + 'static,
{
    fn from(value: F) -> Self {
        Self(Box::new(move || value().into_any()))
    }
}

impl<C> From<View<C>> for ViewFnOnce
where
    C: Send + Sync + 'static,
    View<C>: IntoAny,
{
    fn from(value: View<C>) -> Self {
        Self(Box::new(move || value.into_any()))
    }
}

impl ViewFnOnce {
    /// Execute the wrapped function
    pub fn run(self) -> AnyView {
        (self.0)()
    }
}

/// A typed equivalent to [`Children`], which takes a generic but preserves type information to
/// allow the compiler to optimize the view more effectively.
pub struct TypedChildren<T>(Box<dyn FnOnce() -> View<T> + Send>);

impl<T> TypedChildren<T> {
    /// Extracts the inner `children` function.
    pub fn into_inner(self) -> impl FnOnce() -> View<T> + Send {
        self.0
    }
}

impl<F, C> ToChildren<F> for TypedChildren<C>
where
    F: FnOnce() -> C + Send + 'static,
    C: IntoView,
    C::AsyncOutput: Send,
{
    #[inline]
    fn to_children(f: F) -> Self {
        TypedChildren(Box::new(move || f().into_view()))
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for TypedChildren<T>
where
    T: IntoView + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        TypedChildren(Box::new(move || t.0.into_view()))
    }
}

/// A typed equivalent to [`ChildrenFnMut`], which takes a generic but preserves type information to
/// allow the compiler to optimize the view more effectively.
pub struct TypedChildrenMut<T>(Box<dyn FnMut() -> View<T> + Send>);

impl<T> Debug for TypedChildrenMut<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("TypedChildrenMut").finish()
    }
}

impl<T> TypedChildrenMut<T> {
    /// Extracts the inner `children` function.
    pub fn into_inner(self) -> impl FnMut() -> View<T> + Send {
        self.0
    }
}

impl<F, C> ToChildren<F> for TypedChildrenMut<C>
where
    F: FnMut() -> C + Send + 'static,
    C: IntoView,
    C::AsyncOutput: Send,
{
    #[inline]
    fn to_children(mut f: F) -> Self {
        TypedChildrenMut(Box::new(move || f().into_view()))
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for TypedChildrenMut<T>
where
    T: IntoView + Clone + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        TypedChildrenMut(Box::new(move || t.0.clone().into_view()))
    }
}

/// A typed equivalent to [`ChildrenFn`], which takes a generic but preserves type information to
/// allow the compiler to optimize the view more effectively.
pub struct TypedChildrenFn<T>(Arc<dyn Fn() -> View<T> + Send + Sync>);

impl<T> Debug for TypedChildrenFn<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("TypedChildrenFn").finish()
    }
}

impl<T> Clone for TypedChildrenFn<T> {
    // Manual implementation to avoid the `T: Clone` bound.
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<T> TypedChildrenFn<T> {
    /// Extracts the inner `children` function.
    pub fn into_inner(self) -> Arc<dyn Fn() -> View<T> + Send + Sync> {
        self.0
    }
}

impl<F, C> ToChildren<F> for TypedChildrenFn<C>
where
    F: Fn() -> C + Send + Sync + 'static,
    C: IntoView,
    C::AsyncOutput: Send,
{
    #[inline]
    fn to_children(f: F) -> Self {
        TypedChildrenFn(Arc::new(move || f().into_view()))
    }
}

impl<T> ToChildren<ChildrenOptContainer<T>> for TypedChildrenFn<T>
where
    T: IntoView + Clone + Sync + 'static,
{
    #[inline]
    fn to_children(t: ChildrenOptContainer<T>) -> Self {
        TypedChildrenFn(Arc::new(move || t.0.clone().into_view()))
    }
}