spread.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //! Example: README.md showcase
  2. //!
  3. //! The example from the README.md.
  4. use dioxus::{
  5. core::{exports::bumpalo::Bump, Attribute, HasAttributesBox},
  6. html::{ExtendedGlobalAttributesMarker, GlobalAttributesExtension},
  7. prelude::*,
  8. };
  9. fn main() {
  10. let mut dom = VirtualDom::new(app);
  11. dom.rebuild();
  12. let html = dioxus_ssr::render(&dom);
  13. println!("{}", html);
  14. }
  15. fn app(cx: Scope) -> Element {
  16. cx.render(::dioxus::core::LazyNodes::new(
  17. move |__cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode {
  18. static TEMPLATE: ::dioxus::core::Template = ::dioxus::core::Template {
  19. name: "src/main.rs:15:15:289",
  20. roots: &[::dioxus::core::TemplateNode::Dynamic { id: 0usize }],
  21. node_paths: &[&[0u8]],
  22. attr_paths: &[],
  23. };
  24. ::dioxus::core::VNode {
  25. parent: None,
  26. key: None,
  27. template: std::cell::Cell::new(TEMPLATE),
  28. root_ids: dioxus::core::exports::bumpalo::collections::Vec::with_capacity_in(
  29. 1usize,
  30. __cx.bump(),
  31. )
  32. .into(),
  33. dynamic_nodes: __cx.bump().alloc([__cx.component(
  34. Component,
  35. Props {
  36. bump: __cx.bump(),
  37. attributes: Vec::new(),
  38. }
  39. .width(10)
  40. .height("100px"),
  41. "Component",
  42. )]),
  43. dynamic_attrs: __cx.bump().alloc([]),
  44. }
  45. },
  46. ))
  47. }
  48. #[derive(Props)]
  49. struct Props<'a> {
  50. bump: &'a Bump,
  51. attributes: Vec<Attribute<'a>>,
  52. }
  53. impl<'a> HasAttributesBox<'a, Props<'a>> for Props<'a> {
  54. fn push_attribute(
  55. mut self,
  56. name: &'a str,
  57. ns: Option<&'static str>,
  58. attr: impl IntoAttributeValue<'a>,
  59. volatile: bool,
  60. ) -> Self {
  61. self.attributes.push(Attribute {
  62. name,
  63. namespace: ns,
  64. value: attr.into_value(self.bump),
  65. volatile,
  66. });
  67. self
  68. }
  69. }
  70. impl ExtendedGlobalAttributesMarker for Props<'_> {}
  71. fn Component<'a>(cx: Scope<'a, Props<'a>>) -> Element<'a> {
  72. let attributes = &cx.props.attributes;
  73. render! {
  74. // rsx! {
  75. // audio {
  76. // ...attributes,
  77. // }
  78. // }
  79. ::dioxus::core::LazyNodes::new(move |__cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode {
  80. static TEMPLATE: ::dioxus::core::Template = ::dioxus::core::Template { name: concat!(file!(), ":", line!(), ":", column!(), ":", "123" ), roots: &[::dioxus::core::TemplateNode::Element { tag: dioxus_elements::audio::TAG_NAME, namespace: dioxus_elements::audio::NAME_SPACE, attrs: &[::dioxus::core::TemplateAttribute::Dynamic { id: 0usize }], children: &[] }], node_paths: &[], attr_paths: &[&[0u8]] };
  81. let attrs = vec![(&**attributes).into()];
  82. ::dioxus::core::VNode {
  83. parent: None,
  84. key: None,
  85. template: std::cell::Cell::new(TEMPLATE),
  86. root_ids: dioxus::core::exports::bumpalo::collections::Vec::new_in(__cx.bump()).into(),
  87. dynamic_nodes: __cx.bump().alloc([]),
  88. dynamic_attrs: __cx.bump().alloc(attrs),
  89. }
  90. })
  91. }
  92. }