dioxus/packages/html
Demonthos 047ed1e553
Subtree memorization / reactive templates (#488)
This commit adds subtree memoization to Dioxus.

Subtree memoization is basically a compile-time step that drastically 
reduces the amount of work the diffing engine needs to do at runtime by
extracting non-changing nodes out into a static "template." Templates 
are then understood by the various renderers in the ecosystem as a 
faster way of rendering the same items. 

For example, in the web, templates are simply a set of DOM Nodes created 
once and then cloned later. This is the same pattern frameworks like Lithtml
and SolidJS use to achieve near-perfect performance. 

Subtree memoization adds an additional level of complexity to Dioxus. The RSX
macro needs to be much smarter to identify changing/nonchanging nodes and
generate a mapping between the Template and its runtime counterparts.

This commit represents a working starter point for this work, adding support 
for templates for the web, desktop, liveview, ssr, and native-core renderers.
In the future we will try to shrink code generation, generally improve 
performance, and simplify our implementation.
2022-09-30 12:03:06 -07:00
..
src Subtree memorization / reactive templates (#488) 2022-09-30 12:03:06 -07:00
Cargo.toml Tui: construct keyboard data with new api 2022-05-12 14:10:25 +03:00
CHANGELOG.md feat: add changelogs 2022-01-29 10:17:14 -05:00
README.md Fix various typos and grammar nits 2022-01-21 21:43:43 -06:00

Html (and SVG) Namespace for Dioxus

The Dioxus rsx! and html! macros can accept any compile-time correct namespace on top of NodeFactory. This crate provides the HTML (and SVG) namespaces which get imported in the Dioxus prelude.

However, this abstraction enables you to add any namespace of elements, provided they're in scope when rsx! is called. For an example, a UI that is designed for Augmented Reality might use different primitives than HTML:

use ar_namespace::*;

rsx! {
    magic_div {
        magic_header {}
        magic_paragraph {
            on_magic_click: move |event| {
                //
            }
        }
    }
}

This is currently a not-very-explored part of Dioxus. However, the namespacing system does make it possible to provide syntax highlighting, documentation, "go to definition" and compile-time correctness, so it's worth having it abstracted.

How it works:

Elements for dioxus must implement the (simple) DioxusElement trait to be used in the rsx! macro.

struct div;
impl DioxusElement for div {
    const TAG_NAME: &'static str = "div";
    const NAME_SPACE: Option<&'static str> = None;
}

All elements should be defined as a zero-sized-struct (also known as unit struct). These structs are zero-cost and just provide the type-level trickery to Rust for compile-time correct templates.

Attributes would then be implemented as methods on these unit structs.

The HTML namespace is defined mostly with macros. However, the expanded form would look something like this:

struct base;
impl DioxusElement for base {
    const TAG_NAME: &'static str = "base";
    const NAME_SPACE: Option<&'static str> = None;
}
impl base {
    #[inline]
    fn href<'a>(&self, f: NodeFactory<'a>, v: Arguments) -> Attribute<'a> {
        f.attr("href", v, None, false)
    }
    #[inline]
    fn target<'a>(&self, f: NodeFactory<'a>, v: Arguments) -> Attribute<'a> {
        f.attr("target", v, None, false)
    }
}

Because attributes are defined as methods on the unit struct, they guard the attribute creation behind a compile-time correct interface.

How to extend it:

Whenever the rsx! macro is called, it relies on a module dioxus_elements to be in scope. When you enable the html feature in dioxus, this module gets imported in the prelude. However, you can extend this with your own set of custom elements by making your own dioxus_elements module and re-exporting the html namespace.

mod dioxus_elements {
    use dioxus::prelude::dioxus_elements::*;
    struct my_element;
    impl DioxusElement for my_element {
        const TAG_NAME: &'static str = "base";
        const NAME_SPACE: Option<&'static str> = None;
    }
}

Limitations:

How to work around it:

If an attribute in Dioxus is invalid (defined incorrectly) - first, make an issue - but then, you can work around it. The raw builder API is actually somewhat ergonomic to work with, and the NodeFactory type exposes a bunch of methods to make any type of tree - even invalid ones! So obviously, be careful, but there's basically anything you can do.

cx.render(rsx!{
    div {
        h1 {}
        // Oh no! I need a super custom element
        {LazyNodes::new(move |f| {
            f.raw_element(
                // tag name
                "custom_element",

                // attributes
                &[f.attr("billy", format_args!("goat"))],

                // listeners
                &[f.listener(onclick(move |_| {}))],

                // children
                &[cx.render(rsx!(div {} ))],

                // key
                None
            )
        })}
    }
})