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

optimize how initial HTML is set on initial render for JSX usage #138

Open
thescientist13 opened this issue Jan 6, 2024 · 0 comments
Open
Labels
enhancement Improvement to existing functionality good first issue Good for newcomers JSX
Milestone

Comments

@thescientist13
Copy link
Member

Summary

Currently as part of initializing a component (either Light or Shadow DOM) from render function when using JSX, we use innerHTML in a couple scenarios

  1. Light DOM uses innerHTML
  2. Shadow DOM, if there is a root but no HTML (like say injecting dynamic content at run time, a la htmx)
const renderHandler = hasShadowRoot
  ? `
      const template = document.createElement('template');
      template.innerHTML = \`${serializedHtml}\`;

      if(!${elementRoot}) {
        this.attachShadow({ mode: 'open' });
        this.shadowRoot.appendChild(template.content.cloneNode(true));
      } else {
        this.shadowRoot.innerHTML = template.innerHTML;
      }
    `
  : `${elementRoot}.innerHTML = \`${serializedHtml}\`;`;

Details

It would be nice in either of these cases if we could just "push" the template right into the node, instead of using innerHTML which has a couple downsides

So, instead would be nice to see if we could leverage actual DOM APIs , like say replaceWith, just like we are able to leverage appendNode.

const renderHandler = hasShadowRoot
  ? `
      const template = document.createElement('template');
      template.innerHTML = \`${serializedHtml}\`;

      if(!${elementRoot}) {
        this.attachShadow({ mode: 'open' });
        this.shadowRoot.appendChild(template.content.cloneNode(true));
      } else {
        this.shadowRoot.replaceNode(template);
      }
    `
  : `${elementRoot}.innerHTML = \`${serializedHtml}\`;`;
@thescientist13 thescientist13 added enhancement Improvement to existing functionality JSX labels Jan 6, 2024
@thescientist13 thescientist13 added this to the 1.0 milestone Jan 6, 2024
@thescientist13 thescientist13 added the good first issue Good for newcomers label Jan 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement to existing functionality good first issue Good for newcomers JSX
Projects
None yet
Development

No branches or pull requests

1 participant