wip: more examples
This commit is contained in:
1 parent
304259d818
commit
56e7eb83a9
21 files changed
+162
-236
No files matched your search
@@ -1,24 +1,15 @@
|
||||
fn main() {}
|
||||
|
||||
use dioxus::*;
|
||||
use dioxus_core as dioxus;
|
||||
use dioxus_core::prelude::*;
|
||||
mod dioxus_elements {
|
||||
use super::*;
|
||||
pub struct div;
|
||||
impl DioxusElement for div {
|
||||
const TAG_NAME: &'static str = "str";
|
||||
const NAME_SPACE: Option<&'static str> = None;
|
||||
}
|
||||
}
|
||||
|
||||
static Example: FC<()> = |cx| {
|
||||
let list = (0..10).map(|f| {
|
||||
//
|
||||
LazyNodes::new(move |f: NodeFactory| todo!())
|
||||
});
|
||||
let list = (0..10).map(|f| LazyNodes::new(move |f| todo!()));
|
||||
|
||||
cx.render(LazyNodes::new(move |cx| {
|
||||
let bump = cx.bump();
|
||||
dioxus_core::builder::ElementBuilder::new(&cx, "h1")
|
||||
cx.raw_element("div")
|
||||
.children([
|
||||
cx.text(format_args!("hello")),
|
||||
cx.text(format_args!("hello")),
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
fn main() {}
|
||||
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::*;
|
||||
use std::rc::Rc;
|
||||
|
||||
struct AppProps {
|
||||
|
||||
@@ -44,6 +44,7 @@ pub fn fc_to_builder<T: Properties>(_: FC<T>) -> T::Builder {
|
||||
T::builder()
|
||||
}
|
||||
|
||||
use crate::nodebuilder::LazyNodes;
|
||||
/// Create inline fragments
|
||||
///
|
||||
/// Fragments capture a series of children without rendering extra nodes.
|
||||
@@ -53,5 +54,7 @@ pub fn fc_to_builder<T: Properties>(_: FC<T>) -> T::Builder {
|
||||
use crate::prelude::*;
|
||||
#[allow(non_upper_case_globals, non_snake_case)]
|
||||
pub fn Fragment<'a>(cx: Context<'a, ()>) -> VNode<'a> {
|
||||
cx.render(LazyNodes::new(move |f| f.fragment_from_iter(cx.children())))
|
||||
cx.render(LazyNodes::new(move |f| {
|
||||
f.fragment_from_iter(cx.children())
|
||||
}))
|
||||
}
|
||||
@@ -125,8 +125,8 @@ pub mod on {
|
||||
use std::{fmt::Debug, ops::Deref, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
builder::ElementBuilder,
|
||||
builder::NodeFactory,
|
||||
innerlude::ElementBuilder,
|
||||
innerlude::NodeFactory,
|
||||
innerlude::{Attribute, Listener, RealDomNode, VNode},
|
||||
};
|
||||
use std::cell::Cell;
|
||||
|
||||
+15
-30
@@ -28,10 +28,6 @@ pub mod tasks;
|
||||
pub mod util;
|
||||
pub mod virtual_dom;
|
||||
|
||||
pub mod builder {
|
||||
pub use super::nodebuilder::*;
|
||||
}
|
||||
|
||||
// types used internally that are important
|
||||
pub(crate) mod innerlude {
|
||||
pub use crate::bumpframe::*;
|
||||
@@ -43,6 +39,7 @@ pub(crate) mod innerlude {
|
||||
pub use crate::nodebuilder::*;
|
||||
pub use crate::nodes::*;
|
||||
pub use crate::scope::*;
|
||||
pub use crate::serialize::*;
|
||||
pub use crate::tasks::*;
|
||||
pub use crate::util::*;
|
||||
pub use crate::virtual_dom::*;
|
||||
@@ -54,35 +51,23 @@ pub(crate) mod innerlude {
|
||||
pub use dioxus_core_macro::{html, rsx};
|
||||
}
|
||||
|
||||
/// Re-export common types for ease of development use.
|
||||
/// Essential when working with the html! macro
|
||||
pub use crate::{
|
||||
innerlude::{
|
||||
DioxusElement, DomEdit, LazyNodes, NodeFactory, RealDom, RealDomNode, ScopeIdx, FC,
|
||||
},
|
||||
virtual_dom::VirtualDom,
|
||||
};
|
||||
|
||||
pub mod prelude {
|
||||
pub use crate::component::{fc_to_builder, Fragment, Properties};
|
||||
pub use crate::context::Context;
|
||||
use crate::nodes;
|
||||
pub use crate::styles::{AsAttr, StyleBuilder};
|
||||
|
||||
pub use crate::util::RealDomNode;
|
||||
pub use crate::virtual_dom::VirtualDom;
|
||||
pub use nodes::*;
|
||||
|
||||
pub use crate::nodebuilder::LazyNodes;
|
||||
|
||||
pub use crate::nodebuilder::{DioxusElement, NodeFactory};
|
||||
// pub use nodes::iterables::IterableNodes;
|
||||
/// This type alias is an internal way of abstracting over the static functions that represent components.
|
||||
pub use crate::innerlude::FC;
|
||||
|
||||
// expose our bumpalo type
|
||||
pub use bumpalo;
|
||||
pub use bumpalo::Bump;
|
||||
|
||||
// Re-export the FC macro
|
||||
pub use crate::nodebuilder as builder;
|
||||
// pub use dioxus_core_macro::fc;
|
||||
|
||||
pub use crate::innerlude::{LazyNodes, NodeFactory, FC};
|
||||
pub use crate::nodebuilder::DioxusElement;
|
||||
pub use crate::nodes::VNode;
|
||||
pub use crate::VirtualDom;
|
||||
pub use dioxus_core_macro::{format_args_f, html, rsx, Props};
|
||||
}
|
||||
|
||||
pub use crate::diff::DiffMachine;
|
||||
pub use crate::virtual_dom::ScopeIdx;
|
||||
pub mod exports {
|
||||
// export important things here
|
||||
}
|
||||
@@ -14,9 +14,8 @@ use bumpalo::Bump;
|
||||
|
||||
use crate::{
|
||||
events::VirtualEvent,
|
||||
innerlude::{Properties, RealDomNode, Scope, VComponent, VText, FC},
|
||||
innerlude::{Properties, RealDomNode, Scope, VComponent, VFragment, VText, FC},
|
||||
nodes::{Attribute, Listener, NodeKey, VNode},
|
||||
prelude::{VElement, VFragment},
|
||||
};
|
||||
|
||||
/// A virtual DOM element builder.
|
||||
@@ -647,6 +646,20 @@ impl<'a> NodeFactory<'a> {
|
||||
VNode::text(self.bump(), args)
|
||||
}
|
||||
|
||||
/// Create an element builder
|
||||
pub fn raw_element<'b>(
|
||||
&'b self,
|
||||
tag: &'static str,
|
||||
) -> ElementBuilder<
|
||||
'a,
|
||||
'b,
|
||||
bumpalo::collections::Vec<'a, Listener<'a>>,
|
||||
bumpalo::collections::Vec<'a, Attribute<'a>>,
|
||||
bumpalo::collections::Vec<'a, VNode<'a>>,
|
||||
> {
|
||||
ElementBuilder::new(self, tag)
|
||||
}
|
||||
|
||||
/// Create an element builder
|
||||
pub fn element<'b>(
|
||||
&'b self,
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
//!
|
||||
//!
|
||||
|
||||
use crate::prelude::ScopeIdx;
|
||||
use crate::innerlude::ScopeIdx;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// A `DomEdit` represents a serialzied form of the VirtualDom's trait-based API. This allows streaming edits across the
|
||||
|
||||
@@ -19,7 +19,7 @@ use std::{
|
||||
use futures_util::{Future, Stream, StreamExt};
|
||||
use slotmap::{DefaultKey, SlotMap};
|
||||
|
||||
use crate::{events::EventTrigger, prelude::ScopeIdx};
|
||||
use crate::{events::EventTrigger, innerlude::ScopeIdx};
|
||||
|
||||
pub type TaskSubmitter = Arc<dyn Fn(DTask)>;
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
//! of the Rsx and Html macros. Each element comes with a substantial amount of documentation on how to best use it, hopefully
|
||||
//! making the development cycle quick.
|
||||
|
||||
use dioxus_core::prelude::{DioxusElement, NodeFactory};
|
||||
use dioxus_core::{DioxusElement, NodeFactory};
|
||||
|
||||
macro_rules! builder_constructors {
|
||||
( $( $(#[$attr:meta])* $name:ident; )* ) => {
|
||||
|
||||
+34
-11
@@ -16,7 +16,7 @@ pub fn render_root(vdom: &VirtualDom) -> String {
|
||||
|
||||
pub struct SsrConfig {
|
||||
// currently not supported - control if we indent the HTML output
|
||||
_indent: bool,
|
||||
indent: bool,
|
||||
|
||||
// Control if elements are written onto a new line
|
||||
newline: bool,
|
||||
@@ -30,7 +30,7 @@ pub struct SsrConfig {
|
||||
impl Default for SsrConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
_indent: false,
|
||||
indent: false,
|
||||
|
||||
newline: false,
|
||||
_skip_components: false,
|
||||
@@ -68,10 +68,23 @@ impl<'a> TextRenderer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn html_render(&self, node: &VNode, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
fn html_render(&self, node: &VNode, f: &mut std::fmt::Formatter, il: u16) -> std::fmt::Result {
|
||||
match node {
|
||||
VNode::Text(text) => write!(f, "{}", text.text)?,
|
||||
VNode::Text(text) => {
|
||||
if self.cfg.indent {
|
||||
for _ in 0..il {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
}
|
||||
write!(f, "{}", text.text)?
|
||||
}
|
||||
VNode::Element(el) => {
|
||||
if self.cfg.indent {
|
||||
for _ in 0..il {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "<{}", el.tag_name)?;
|
||||
for attr in el.attributes {
|
||||
write!(f, " {}=\"{}\"", attr.name, attr.value)?;
|
||||
@@ -82,16 +95,26 @@ impl<'a> TextRenderer<'a> {
|
||||
}
|
||||
|
||||
for child in el.children {
|
||||
self.html_render(child, f)?;
|
||||
self.html_render(child, f, il + 1)?;
|
||||
}
|
||||
match self.cfg.newline {
|
||||
true => write!(f, "\n</{}>", el.tag_name)?,
|
||||
false => write!(f, "</{}>", el.tag_name)?,
|
||||
|
||||
if self.cfg.newline {
|
||||
write!(f, "\n")?;
|
||||
}
|
||||
if self.cfg.indent {
|
||||
for _ in 0..il {
|
||||
write!(f, " ")?;
|
||||
}
|
||||
}
|
||||
|
||||
write!(f, "</{}>", el.tag_name)?;
|
||||
if self.cfg.newline {
|
||||
write!(f, "\n")?;
|
||||
}
|
||||
}
|
||||
VNode::Fragment(frag) => {
|
||||
for child in frag.children {
|
||||
self.html_render(child, f)?;
|
||||
self.html_render(child, f, il + 1)?;
|
||||
}
|
||||
}
|
||||
VNode::Component(vcomp) => {
|
||||
@@ -105,7 +128,7 @@ impl<'a> TextRenderer<'a> {
|
||||
.frames
|
||||
.current_head_node();
|
||||
|
||||
self.html_render(new_node, f)?;
|
||||
self.html_render(new_node, f, il + 1)?;
|
||||
}
|
||||
VNode::Suspended { .. } => todo!(),
|
||||
}
|
||||
@@ -117,7 +140,7 @@ impl Display for TextRenderer<'_> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
let root = self.vdom.base_scope();
|
||||
let root_node = root.root();
|
||||
self.html_render(root_node, f)
|
||||
self.html_render(root_node, f, 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -444,31 +444,3 @@ pub fn intern_cache() {
|
||||
wasm_bindgen::intern(s);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::env;
|
||||
|
||||
use super::*;
|
||||
use dioxus::prelude::bumpalo;
|
||||
use dioxus::prelude::format_args_f;
|
||||
use dioxus_core as dioxus;
|
||||
use dioxus_core::prelude::html;
|
||||
|
||||
fn simple_patch() {
|
||||
env::set_var("RUST_LOG", "trace");
|
||||
pretty_env_logger::init();
|
||||
log::info!("Hello!");
|
||||
|
||||
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(|cx| {
|
||||
todo!()
|
||||
// cx.render(html! {
|
||||
// <div>
|
||||
// "Hello world"
|
||||
// <button onclick={move |_| log::info!("button1 clicked!")}> "click me" </button>
|
||||
// <button onclick={move |_| log::info!("button2 clicked!")}> "click me" </button>
|
||||
// </div>
|
||||
// })
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ use std::{collections::HashMap, rc::Rc, sync::Arc};
|
||||
|
||||
use dioxus_core::{
|
||||
events::{EventTrigger, VirtualEvent},
|
||||
prelude::{RealDomNode, ScopeIdx},
|
||||
RealDomNode, ScopeIdx,
|
||||
};
|
||||
use fxhash::FxHashMap;
|
||||
use slotmap::{DefaultKey, Key, KeyData};
|
||||
@@ -192,7 +192,7 @@ impl<'a> dioxus_core::diff::RealDom<'a> for WebsysDom {
|
||||
fn new_event_listener(
|
||||
&mut self,
|
||||
event: &'static str,
|
||||
scope: dioxus_core::prelude::ScopeIdx,
|
||||
scope: ScopeIdx,
|
||||
_element_id: usize,
|
||||
real_id: RealDomNode,
|
||||
) {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
//! webview dom
|
||||
|
||||
use dioxus_core as dioxus;
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_core::{diff::RealDom, serialize::DomEdit, virtual_dom::VirtualDom};
|
||||
use dioxus_core::{DomEdit, RealDom, RealDomNode, ScopeIdx};
|
||||
use DomEdit::*;
|
||||
|
||||
pub struct WebviewRegistry {}
|
||||
@@ -80,7 +78,7 @@ impl<'bump> RealDom<'bump> for WebviewDom<'bump> {
|
||||
fn new_event_listener(
|
||||
&mut self,
|
||||
event: &'static str,
|
||||
scope: dioxus_core::prelude::ScopeIdx,
|
||||
scope: ScopeIdx,
|
||||
element_id: usize,
|
||||
realnode: RealDomNode,
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user