-
Notifications
You must be signed in to change notification settings - Fork 12
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
Can we help you possibly use LitElement? #53
Comments
Thanks for reaching out @justinfagnani! We're definitely interested trying to extend LitElement. In fact, we drafted off of Polymer and watched the development of LitElement to help guide some decision making. We'd be willing to try and extend LitElement, especially if you would be willing to lend a hand now and again. I think a good starting point would be to ask if you have examples of others who have built out support for observing properties and computing properties. We could then try and boot up an alternative base element and start trying it out. |
I could definitely lend a hand. LitElement should be pretty extensible already, and we're landing some changes to make it even more so. For observing properties, I've used a subclass like this: https://gist.github.com/justinfagnani/d7f73971bfa3c96d5a08f9dadda6ea08 For computing properties, can you clarify what you mean? I typically just use class getters. Do you need more? |
We've been doing the computation up-front (vs lazily through getters) to avoid memoization and allow reflection and observation to follow from computation. However, I think your suggestion is to invert that mindset and let observation and reflection motivate the computation of properties -- handling memoization of computation as an optimization. In general, I have a strong feeling that we'll be able to successfully extend LitElement. I think we should maybe just attempt to create a POC and report back with any roadblocks. I'll try to find some time to do that and I'll let you know how it goes! |
@justinfagnani, I took a little time to start sizing up the task of trying to extend lit-element and a couple snags have come up so far:
A reasonable base test would be for us to support the following properties block:
I don't think i'm completely stuck yet and I do intend to try and get something working so that we can both see how easy / hard it is to extend |
Thanks for the feedback. I think we're actually looking pretty good here. For (1), we just landed lit/lit-element#914 which makes it easier to override the property definition. You should be able to grab the type from the options and do the coercion in the setter. For (2) and (3) things might work already. If you define a property for a getter, you can set the class MyElement extends XElement {
@property({reflect: true})
a;
@property({reflect: true})
b;
@property({reflect: true})
get c() { return a + b; }
} |
We don't transpile our code, so I'm using // index.html
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>lit-element-test</title>
</head>
<body>
<lit-element-test></lit-element-test>
<script src="index.js" type="module"></script>
</body>
</html> // index.js
import { LitElement, css } from 'https://cdn.pika.dev/lit-element/^2.2.1';
import { html } from 'https://cdn.pika.dev/lit-html@^1.1.2';
const STYLE = css`
:host {
display: block;
}
div {
max-width: min-content;
white-space: nowrap;
}
div + div {
margin-top: 4px;
}
#equation,
#not-even {
background-color: coral;
}
:host([even]) #equation,
#even {
background-color: skyblue;
}
`;
export default class LitElementTest extends LitElement {
constructor() {
super();
this.a = 1;
this.b = 3;
}
get c() {
return this.a + this.b;
}
get even() {
return this.c % 2 === 0;
}
static get styles() {
return [STYLE];
}
render() {
return html`
<div id="not-even">not even</div>
<div id="even">even</div>
<div id="equation">${this.a} + ${this.b} = ${this.c}</div>
`;
}
}
LitElementTest.createProperty('a', { type: Number });
LitElementTest.createProperty('b', { type: Number });
LitElementTest.createProperty('c', { type: Number });
LitElementTest.createProperty('even', { type: Boolean, reflect: true });
customElements.define('lit-element-test', LitElementTest); |
Unrelated to the the issue with export default class LitElementTest extends LitElement {
static properties = {
a: {type: Number},
b: {type: Number},
c: {type: Number},
even: {type: Boolean, reflect: true},
};
} The reason why the computed properties aren't reflected is that they're never marked as changed in the update lifecycle. I was able to work around this manually by overriding This requires a bug fix that landed in lit-element 2.3.0 so that a export default class LitElementTest extends XElement {
static properties = {
a: {type: Number},
b: {type: Number},
c: {type: Number, dependsOn: ['a', 'b']},
even: {type: Boolean, , dependsOn: ['c'], reflect: true},
};
} And have an override of Because these dependency chains are order-dependent or may form a cycle, you might have to do the checks in declaration order and document that, do a topological sort on the dependencies, or set changed properties synchronously (in @sorvell and @kevinpschaaf for an interesting use case for property customization. |
It's exciting to see another web component base class based on lit-html, but from a quick glance this seems to do a lot of the same things that LitElement does, and some of the additional things (like property observers) could be layered on. Is there anything we can do on the LitElement side of things to make it so you can use that instead of maintaining your own base class? I'm happy to help explore ideas if this is interesting to you at all.
The text was updated successfully, but these errors were encountered: