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

Haunted not clearing innerHTML before first render #444

Open
micahjon opened this issue Oct 13, 2022 · 2 comments
Open

Haunted not clearing innerHTML before first render #444

micahjon opened this issue Oct 13, 2022 · 2 comments

Comments

@micahjon
Copy link
Contributor

micahjon commented Oct 13, 2022

I'm working on upgrading BeFunky to the latest version of Lit and Haunted.

We currently don't use shadow DOM for components, and I noticed that when useShadowDOM: false is set, Haunted doesn't clear the initial content in the custom element before rendering. Initial content is really helpful for progressive enhancement, avoiding layout shift when the component JS loads and renders, e.g.

<custom-element>
  <span class="loading-placeholder">Loading...<span>
</custom-element>

This was fine in Lit 1.*, but in Lit 2.*, the render() function doesn't clear the container's contents.

From the Lit upgrade guide: https://lit.dev/docs/releases/upgrade/#lit-html

render() no longer clears the container it's rendered to on first render. It now appends to the container by default.

I'm guessing we'll need to set component.innerHTML = '' before the first render manually when useShadowDOM: false is set.


Bug replicated in Codepen: https://codepen.io/pranksinatra/pen/XWqrpBj

@micahjon
Copy link
Contributor Author

Submitted a PR that should fix this issue. Open to any and all feedback, thanks!

@tommywalkie
Copy link

tommywalkie commented Nov 16, 2022

Slightly related to the question, is there a pragmatic way to use slotted content and/or retrieve the initial content before the first render? Currently playing with this on [email protected]:

function SampleCard(element: HTMLElement) {
  const [slot] = useState(Array.from(element.childNodes));
  element.innerHTML = "";
  return html`<div class="whatever-container">${slot}</div>`; 
}

Probably won't work after #445 is merged.

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