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

Attribute parts does not work with hard-coded strings #843

Closed
5 tasks
kaaninel opened this issue Mar 1, 2019 · 3 comments
Closed
5 tasks

Attribute parts does not work with hard-coded strings #843

kaaninel opened this issue Mar 1, 2019 · 3 comments

Comments

@kaaninel
Copy link

kaaninel commented Mar 1, 2019

Description

A clear and concise description of what the bug is.

Steps to Reproduce

  1. Write this code
// Does not work
html`<ui-hello-world @click="${this.Test}" .Text="But Why"></ui-hello-world>`

// Works
html`<ui-hello-world @click="${this.Test}" .Text="${'But Why'}"></ui-hello-world>`

Expected Results

It should set Text property of instance created by lit.

Actual Results

Lit does not process .Text attribute and it stays in dom like .text="but why"

Browsers Affected

  • [ x] Chrome
  • Firefox
  • Edge
  • Safari 11
  • Safari 10
  • IE 11
@jsilvermist
Copy link

jsilvermist commented Mar 1, 2019

AFAIK this is the expected behavior and the second syntax is the recommended way to do it. Only bindings are parsed by lit due to performance and design reasons.

@Westbrook
Copy link
Contributor

lit-html will only bind onto attributes with dynamic expressions in their value. So when you use .Text="But Why" lit-html won't even read into the statement to decide whether you're intending to use a property or an attribute. In the case that you're actually binding simple data like this (a String), I suggest you get around this issue by relying on standard attribute binding: Text="But Why".

Doing so with LitElement
If you're created the Test property in the static get properties() of your ui-hello-world element, the element will automatically pass the value of the Test attribute to the property of the same name for you.

Doing so with Vanilla Element
If you are working with a vanilla custom element, you can add the Text attribute to your static get observedAttributes():

static get observedAttributes() {
    return ['Text'];
}

And then pass that attribute data to the property internally via an attributeChangedCallback:

attributeChangedCallback(name, oldValue, newValue) {
  this[name] = newValue;
}

Complex Data Structures
In the case that you're using complex data structure (an Array or Object) you'll want to us a dynamic binding to begin with .Text="${yourComplexData}" so at to maintain the identity of your data. In this case your data would not be shared between the attribute and property of the shared name unless you were to manually do special handling interior to your element.

@ruphin
Copy link
Contributor

ruphin commented Mar 2, 2019

As mentioned before, lit-html does not process your HTML in any way, and anything outside dynamic bindings is left untouched. This is by design, and it is not an issue.

If you want to assign to properties declaratively, you can absolutely use this:

html`<div .prop=${ "value" }></div>`

This will only set the property once during the initial render, and does nothing on consecutive renders.

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

4 participants