Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discussion: Should we allow a “Node” as content. #207

Open
theengineear opened this issue Nov 15, 2024 · 2 comments
Open

Discussion: Should we allow a “Node” as content. #207

theengineear opened this issue Nov 15, 2024 · 2 comments

Comments

@theengineear
Copy link
Collaborator

Current

const container = document.createElement('div');
const input = document.createElement('input');
render(container, html`${input}`);
// <div>[object HTMLInputElement]</div>

We could handle Nodes in a special manner where we just bind the live value directly as content. We would also want to consider whether a Node would be allowed as a value of an array.

Pros

  • Just let me do the thing!

Cons

  • Added complexity / surface area to maintain.
  • Could invite bad practices.
  • Typically easy to work around (create a template or use observe).
@theengineear
Copy link
Collaborator Author

💡 — Another option here would be to enable some sort of syntax that could act as a “callback when instantiated”. Either way, this ticket is really about gaining references to nodes to leverage imperative apis. Here’s an example of what that might look like:

const callback = node => {
  node.addEventListener('change', () => console.log('changed!'));
}
html`<input(${callback})>`; // The “callback” method is called when the `input` is created.

@klebba
Copy link
Collaborator

klebba commented Nov 20, 2024

Note that if a DOM node binding is reused within a template only the last occurrence will actually be displayed (because copies of the Node will not occur)

Perhaps if we used a DocumentFragment we could detect this case and throw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants