primatejs / primate

Web framework focused on flexibility and developer freedom
https://primatejs.com
MIT License
211 stars 9 forks source link

[html] Support nesting components #2

Closed terrablue closed 2 years ago

terrablue commented 2 years ago

It's currently impossible to nest components, i.e.

import {router, html} from "primate";

router.get("/", html`<parent-component />`);
<!-- in parent-component -->
<child-component />

Doesn't expand <child-component>.

Additionally, there is the issue of passing fixed strings vs. values to child components. In JS this is not a problem, as we use template literals. But in HTML there is no distinction between strings and values.

A possible solution is to adhere to data attributes and use them to pass data, and otherwise treat everything as literal, e.g.

import {router, html} from "primate";

router.get("/", html`<parent-component user=${{"name": "Donald"}} />`);
<!-- in parent-component -->
<child-component label="user" data-user="user" />
<!-- in child-component -->
<span data-value="label"></span>
<span data-value="user"></span>

This, however, might seem inconsistent with the JS side and would result in conflicts if the data attribute and the normal attribute are called the same.

An alternative would be to force using template literals for actual data, and treat everything else as text in components.

<!-- in parent-component -->
<child-component label="user" user="${user}" />
<!-- in child-component -->
<span data-value="label"></span>
<span data-value="user"></span>

This raises the question whether enforcing the template literal syntax should be limited to component tags or to all HTML code within components, to reach uniformity at the cost of being more verbose.

terrablue commented 2 years ago

16d1757 enforces using template literals and quotes in all HTML code.

terrablue commented 2 years ago

done as of dac5ac7