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

Styles don't get added if render() returns undefined at point of attach to DOM #618

Closed
KeithHenry opened this issue Mar 15, 2019 · 2 comments · Fixed by #712
Closed

Styles don't get added if render() returns undefined at point of attach to DOM #618

KeithHenry opened this issue Mar 15, 2019 · 2 comments · Fixed by #712

Comments

@KeithHenry
Copy link

Description

If render() returns undefined at the point where it is attached the static get styles() attribute is still called but never gets added to the shadow root.

Live Demo

https://stackblitz.com/edit/lit-element-example-debgxe?embed=1&file=index.js

Steps to Reproduce

The problem happens when you have a control that you don't want to render anything when properties are not set, for instance:

static get styles() { ... }

render() {
    if(!this.thingToRender)
        return;

    return html` ...${this.thingToRender.prop1} ...${this.thingToRender.prop2}  `;
}

If this control is attached to the DOM and then thingToRender is set render() calls as expected and populates the HTML, but the styles are never added.

Returning empty content appears to workaround this:

render() {
    if(!this.thingToRender)
        return html``;

So another option would be to update the documentation to state that render() should never return undefined.

Expected Results

Either the styles should be added on attach, or if skipped because no content it should be added after the first render() that contains content.

Actual Results

Styles are not added to the component.

Browsers Affected

I don't think this is browser specific.

Versions

  • lit-element: v2.0.1
  • webcomponents: v2.2.7
@jadjoubran
Copy link
Contributor

I can also reproduce this.
Some additional info:

  • Doesn't happen on Chrome 73 (because there's support for Constructible stylesheets)
  • I can reproduce on Firefox & Safari

In my case, I had the following:

  render() {
    if (!this.questions) {
      return "Loading...";
    }
    ...
}

which I needed to change to:

return html`Loading...`;

I can also see that the styles() method is being called but the styles not being attached

@darionco
Copy link

darionco commented Dec 5, 2019

Over 20% of browsers are affected by this bug:
https://gs.statcounter.com/browser-market-share

:(

darionco added a commit to unchartedsoftware/facets that referenced this issue Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants