wasm_react/builtin_components.rs
1use crate::{
2 props::{HType, H},
3 react_bindings, VNode,
4};
5use std::borrow::Cow;
6use wasm_bindgen::JsValue;
7
8/// A component that specifies the loading indicator when loading lazy descendant
9/// components.
10///
11/// For more information, see [React documentation about code-splitting][docs].
12///
13/// [docs]: https://react.dev/reference/react/Suspense
14///
15/// # Example
16///
17/// ```
18/// # use wasm_react::*;
19/// #
20/// # struct SomeLazyComponent {}
21/// # impl Component for SomeLazyComponent {
22/// # fn render(&self) -> VNode { VNode::default() }
23/// # }
24/// #
25/// # fn f() -> VNode {
26/// Suspense::new()
27/// .fallback(
28/// h!(div[."loading"]).build("Loading…"),
29/// )
30/// .build(
31/// SomeLazyComponent { /* … */ }.build()
32/// )
33/// # }
34/// ```
35#[derive(Debug, Default, Clone, Copy)]
36pub struct Suspense;
37
38impl HType for Suspense {
39 fn as_js(&self) -> Cow<'_, JsValue> {
40 Cow::Borrowed(&react_bindings::SUSPENSE)
41 }
42}
43
44impl Suspense {
45 /// Creates a new `React.Suspense` component builder.
46 pub fn new() -> H<Suspense> {
47 H::new(Suspense)
48 }
49}
50
51impl H<Suspense> {
52 /// Sets a fallback when loading lazy descendant components.
53 pub fn fallback(self, children: impl Into<VNode>) -> Self {
54 self.attr("fallback", children.into().as_ref())
55 }
56}