further simplify spread example
This commit is contained in:
1 parent
97035a4482
commit
0e0819d284
1 file changed
+54
-169
+54
-169
@@ -1,191 +1,76 @@
|
|||||||
//! Example: SSR
|
//! Example: README.md showcase
|
||||||
//!
|
//!
|
||||||
//! This example shows how we can render the Dioxus Virtualdom using SSR.
|
//! The example from the README.md.
|
||||||
|
|
||||||
use dioxus::core::{Attribute, HasAttributesBox};
|
use dioxus::{
|
||||||
use dioxus::html::GlobalAttributesExtension;
|
core::{exports::bumpalo::Bump, Attribute, HasAttributesBox},
|
||||||
use dioxus::html::{AudioExtension, ExtendedAudioMarker, ExtendedGlobalAttributesMarker};
|
html::{ExtendedGlobalAttributesMarker, GlobalAttributesExtension},
|
||||||
use dioxus::prelude::*;
|
prelude::*,
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// We can render VirtualDoms
|
dioxus_desktop::launch(app);
|
||||||
let mut vdom = VirtualDom::new(app);
|
|
||||||
let _ = vdom.rebuild();
|
|
||||||
println!("{}", dioxus_ssr::render(&vdom));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: Scope) -> Element {
|
fn app(cx: Scope) -> Element {
|
||||||
cx.render(rsx!(Foo {
|
cx.render(::dioxus::core::LazyNodes::new(
|
||||||
autoplay: true,
|
move |__cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode {
|
||||||
controls: true,
|
static TEMPLATE: ::dioxus::core::Template = ::dioxus::core::Template {
|
||||||
src: "https://www.w3schools.com/tags/horse.ogg",
|
name: "src/main.rs:15:15:289",
|
||||||
width: "100",
|
roots: &[::dioxus::core::TemplateNode::Dynamic { id: 0usize }],
|
||||||
}))
|
node_paths: &[&[0u8]],
|
||||||
|
attr_paths: &[],
|
||||||
|
};
|
||||||
|
::dioxus::core::VNode {
|
||||||
|
parent: None,
|
||||||
|
key: None,
|
||||||
|
template: std::cell::Cell::new(TEMPLATE),
|
||||||
|
root_ids: dioxus::core::exports::bumpalo::collections::Vec::with_capacity_in(
|
||||||
|
1usize,
|
||||||
|
__cx.bump(),
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
dynamic_nodes: __cx.bump().alloc([__cx.component(
|
||||||
|
Component,
|
||||||
|
Props {
|
||||||
|
bump: __cx.bump(),
|
||||||
|
attributes: Vec::new(),
|
||||||
|
}
|
||||||
|
.width("hello"),
|
||||||
|
"Component",
|
||||||
|
)]),
|
||||||
|
dynamic_attrs: __cx.bump().alloc([]),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FooProps<'a> {
|
#[derive(Props)]
|
||||||
pub open: Option<&'a str>,
|
struct Props<'a> {
|
||||||
|
bump: &'a Bump,
|
||||||
attributes: Vec<Attribute<'a>>,
|
attributes: Vec<Attribute<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----
|
impl<'a> HasAttributesBox<'a, Props<'a>> for Props<'a> {
|
||||||
impl<'a> FooProps<'a> {
|
|
||||||
#[doc = "\nCreate a builder for building `FooProps`.\nOn the builder, call `.open(...)`(optional) to set the values of the fields.\nFinally, call `.build()` to create the instance of `FooProps`.\n "]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn builder(cx: &'a ScopeState) -> FooPropsBuilder<'a, ((),)> {
|
|
||||||
FooPropsBuilder {
|
|
||||||
bump: cx.bump(),
|
|
||||||
fields: ((),),
|
|
||||||
attributes: Vec::new(),
|
|
||||||
_phantom: core::default::Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[must_use]
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[allow(dead_code, non_camel_case_types, non_snake_case)]
|
|
||||||
pub struct FooPropsBuilder<'a, TypedBuilderFields> {
|
|
||||||
bump: &'a ::dioxus::core::exports::bumpalo::Bump,
|
|
||||||
fields: TypedBuilderFields,
|
|
||||||
attributes: Vec<Attribute<'a>>,
|
|
||||||
_phantom: core::marker::PhantomData<&'a ()>,
|
|
||||||
}
|
|
||||||
//impl<'a, TypedBuilderFields, > Clone for FooPropsBuilder<'a, TypedBuilderFields, > where TypedBuilderFields: Clone { fn clone(&self) -> Self { Self { fields: self.fields.clone(), attributes: self.attributes, _phantom: Default::default() } } }
|
|
||||||
impl<'a> dioxus::prelude::Properties<'a> for FooProps<'a> {
|
|
||||||
type Builder = FooPropsBuilder<'a, ((),)>;
|
|
||||||
const IS_STATIC: bool = false;
|
|
||||||
fn builder(cx: &'a ScopeState) -> Self::Builder {
|
|
||||||
FooProps::builder(cx)
|
|
||||||
}
|
|
||||||
unsafe fn memoize(&self, _other: &Self) -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[allow(dead_code, non_camel_case_types, non_snake_case)]
|
|
||||||
pub trait FooPropsBuilder_Optional<T> {
|
|
||||||
fn into_value<F: FnOnce() -> T>(self, default: F) -> T;
|
|
||||||
}
|
|
||||||
impl<T> FooPropsBuilder_Optional<T> for () {
|
|
||||||
fn into_value<F: FnOnce() -> T>(self, default: F) -> T {
|
|
||||||
default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<T> FooPropsBuilder_Optional<T> for (T,) {
|
|
||||||
fn into_value<F: FnOnce() -> T>(self, _: F) -> T {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[allow(dead_code, non_camel_case_types, missing_docs)]
|
|
||||||
impl<'a> FooPropsBuilder<'a, ((),)> {
|
|
||||||
pub fn open(
|
|
||||||
self,
|
|
||||||
open: &'a str,
|
|
||||||
) -> FooPropsBuilder<
|
|
||||||
'a,
|
|
||||||
((
|
|
||||||
Option<&'a str>,
|
|
||||||
// pub attributes: Vec<Attribute<'a>>,
|
|
||||||
),),
|
|
||||||
> {
|
|
||||||
let open = (Some(open),);
|
|
||||||
let (_,) = self.fields;
|
|
||||||
FooPropsBuilder {
|
|
||||||
bump: self.bump,
|
|
||||||
fields: (open,),
|
|
||||||
attributes: self.attributes,
|
|
||||||
_phantom: self._phantom,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[allow(dead_code, non_camel_case_types, non_snake_case)]
|
|
||||||
pub enum FooPropsBuilder_Error_Repeated_field_open {}
|
|
||||||
#[doc(hidden)]
|
|
||||||
#[allow(dead_code, non_camel_case_types, missing_docs)]
|
|
||||||
impl<'a>
|
|
||||||
FooPropsBuilder<
|
|
||||||
'a,
|
|
||||||
((
|
|
||||||
Option<&'a str>,
|
|
||||||
// pub attributes: Vec<Attribute<'a>>,
|
|
||||||
),),
|
|
||||||
>
|
|
||||||
{
|
|
||||||
#[deprecated(note = "Repeated field open")]
|
|
||||||
pub fn open(
|
|
||||||
self,
|
|
||||||
_: FooPropsBuilder_Error_Repeated_field_open,
|
|
||||||
) -> FooPropsBuilder<
|
|
||||||
'a,
|
|
||||||
((
|
|
||||||
Option<&'a str>,
|
|
||||||
// pub attributes: Vec<Attribute<'a>>,
|
|
||||||
),),
|
|
||||||
> {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#[allow(dead_code, non_camel_case_types, missing_docs)]
|
|
||||||
impl<'a, __open: FooPropsBuilder_Optional<Option<&'a str>>> FooPropsBuilder<'a, (__open,)> {
|
|
||||||
pub fn build(self) -> FooProps<'a> {
|
|
||||||
let (open,) = self.fields;
|
|
||||||
let open = FooPropsBuilder_Optional::into_value(open, || Default::default());
|
|
||||||
FooProps {
|
|
||||||
open,
|
|
||||||
attributes: self.attributes,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// -----
|
|
||||||
|
|
||||||
impl<'a, A> HasAttributesBox<'a, FooPropsBuilder<'a, (A,)>> for FooPropsBuilder<'a, (A,)> {
|
|
||||||
fn push_attribute(
|
fn push_attribute(
|
||||||
self,
|
mut self,
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
ns: Option<&'static str>,
|
ns: Option<&'static str>,
|
||||||
attr: impl IntoAttributeValue<'a>,
|
attr: impl IntoAttributeValue<'a>,
|
||||||
volatile: bool,
|
volatile: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut attrs = self.attributes;
|
self.attributes.push(Attribute {
|
||||||
// We insert attributes so that the list is binary-searchable
|
name,
|
||||||
if let Err(index) = attrs.binary_search_by(|probe| probe.name.cmp(name)) {
|
namespace: ns,
|
||||||
attrs.insert(
|
value: attr.into_value(self.bump),
|
||||||
index,
|
volatile,
|
||||||
Attribute::new(name, attr.into_value(self.bump), ns, volatile),
|
});
|
||||||
);
|
self
|
||||||
}
|
|
||||||
FooPropsBuilder {
|
|
||||||
bump: self.bump,
|
|
||||||
fields: self.fields,
|
|
||||||
attributes: attrs,
|
|
||||||
_phantom: self._phantom,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<A> ExtendedGlobalAttributesMarker for FooPropsBuilder<'_, (A,)> {}
|
|
||||||
impl<A> ExtendedAudioMarker for FooPropsBuilder<'_, (A,)> {}
|
|
||||||
|
|
||||||
#[allow(non_snake_case)]
|
impl ExtendedGlobalAttributesMarker for Props<'_> {}
|
||||||
pub fn Foo<'a>(cx: Scope<'a, FooProps<'a>>) -> Element<'a> {
|
|
||||||
let attributes = &cx.props.attributes;
|
fn Component<'a>(cx: Scope<'a, Props<'a>>) -> Element<'a> {
|
||||||
render! {
|
todo!()
|
||||||
// rsx! {
|
|
||||||
// audio {
|
|
||||||
// ...attributes,
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
::dioxus::core::LazyNodes::new(move |__cx: &::dioxus::core::ScopeState| -> ::dioxus::core::VNode {
|
|
||||||
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]] };
|
|
||||||
let attrs = vec![(&**attributes).into()];
|
|
||||||
::dioxus::core::VNode {
|
|
||||||
parent: None,
|
|
||||||
key: None,
|
|
||||||
template: std::cell::Cell::new(TEMPLATE),
|
|
||||||
root_ids: dioxus::core::exports::bumpalo::collections::Vec::new_in(__cx.bump()).into(),
|
|
||||||
dynamic_nodes: __cx.bump().alloc([]),
|
|
||||||
dynamic_attrs: __cx.bump().alloc(attrs),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user