yoshuawuyts / html

Type-safe HTML support for Rust
Apache License 2.0
244 stars 7 forks source link

Create a `CustomElement` definition #39

Open yoshuawuyts opened 1 year ago

yoshuawuyts commented 1 year ago

It should be possible to define <custom-element></custom-element> elements, and append them to other elements. We probably need to:

  1. Define a new CustomElement type
  2. Give it a constructor which takes a name
  3. Enable it to define custom attributes
  4. Enable all existing elements which have children to also take a CustomElement as a child
yoshuawuyts commented 11 months ago

We should do this using Declarative Shadow DOM.

References

Example

Given this HTML:

<host-element>
    <template shadowrootmode="open">
        <style>shadow styles</style>
        <h2>Shadow Content</h2>
        <slot></slot>
    </template>
    <h2>Light content</h2>
</host-element>

We should be able to write this Rust code:

CustomElement::new("host-element")
    .template(|t| t
        .shadow_root_mode("open")
        .style("shadow styles)
        .h2(|h2| h2.text("Shadow Context"))
        .slot(|_|))
    .h2(|h2| h2.text("Light Content");

Tasks

yoshuawuyts commented 11 months ago

Added the "help wanted" label to this. This should be fairly self-contained, and probably a fun one to pick up if anyone wants to. Doing this should enable us to build modular component-based server-rendered pages, which seems like a pretty big deal.