Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions src/jsx-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,24 +348,14 @@ export function parseJsx(moduleURL) {
applyDomDepthSubstitutions(elementTree, undefined, hasShadowRoot);

const serializedHtml = serialize(elementTree);
// we have to Shadow DOM use cases here
// 1. No shadowRoot, so we attachShadow and append the template
// 2. If there is root from the attachShadow signal, so we just need to inject innerHTML, say in an htmx
// could / should we do something else instead of .innerHTML
// https://github.com/ProjectEvergreen/wcc/issues/138
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}\`;`;

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

${elementRoot}.textContent = '';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From your comment i understand the benefit of this, but wasn't sure why we would need this on new / empty shadow root? Wouldn't it already be empty? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're correct, but from what I can tell by the code flow, we never actually had a check for if this is the first render or not.

${elementRoot}.appendChild(template.content.cloneNode(true));
`;
const transformed = acorn.parse(renderHandler, {
ecmaVersion: 'latest',
sourceType: 'module',
Expand Down