-
Notifications
You must be signed in to change notification settings - Fork 319
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
Refine API and refactor #132
Changes from 28 commits
94d4e48
f8f8862
df3c39d
dc5d931
5861e0e
8f83306
e18819e
da54543
b39b480
5ef504e
10fdf29
f131d96
3b5611c
82be06b
3637c1e
bbd3267
3f0315c
85f4cf2
a8d888d
e97392e
00d9d5c
f2f908d
8160de8
e4c7d91
e6a3c70
b39b979
fed3307
5f3cffd
05ba1e4
4361daf
baaf296
f2865de
d4edcd8
9f1a709
25f4ca6
9f8e145
59cd15e
bf6da1d
f684c63
1953a79
f3f28b8
234dff0
24ac381
13e48e6
d708572
9a9aced
4bab118
88a1ad9
10edcc6
a447927
ec02fd8
b77ee89
b0c7428
9341ae2
d3a781b
af1c542
e9d400e
bb6d73d
55bc308
b611732
d11ba45
a85f4bb
c312464
84180cd
49cc74c
ffbfead
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,47 @@ | |
|
||
LitElement uses [lit-html](https://github.com/Polymer/lit-html) to render into the | ||
element's [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) | ||
and [Polymer's](https://github.com/Polymer/polymer) | ||
[PropertiesMixin](https://github.com/Polymer/polymer/blob/master/lib/mixins/properties-mixin.js) | ||
to help manage element properties and attributes. LitElement reacts to changes in properties | ||
and adds API help manage element properties and attributes. LitElement reacts to changes in properties | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to help manage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
and renders declaratively using `lit-html`. | ||
|
||
* **Setup properties:** LitElement supports observable properties which may trigger an | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the "may" here might be confusing... people might wonder, "but I want it to update!" - "so when doesn't it trigger an update then?" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
update when set. These properties can be written in a few ways: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indentation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. written? => added ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
* As class fields with the `@property()` [decorator](https://github.com/tc39/proposal-decorators#decorators), | ||
if you're using a compiler that supports them, like TypeScript or Babel. | ||
* With a static `properties` getter. | ||
* By manually writing getters and setters. This can be useful if tasks should | ||
be performed when a property is set, for example validation. You can call `invalidate()` | ||
in the setter to trigger an update. | ||
|
||
Properties can be given an options argument which is an object that describes how to | ||
process the property. This can be done either in the `@property({...})` decorator or in the | ||
object returned from the `properties` getter, e.g. `static get properties { return { foo: {...} }`. | ||
|
||
Property options include: | ||
|
||
* `attribute`: Describes how and if the property becomes an observed attribute. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if => whether? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
If the value is false, the property is not added to `observedAttributes`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. false => There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ie add back slack ` There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
If true or absent, the lowercased property name is observed (e.g. `fooBar` becomes `foobar`). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with true There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
If a string, the string value is observed (e.g `attribute: 'foo-bar'`). | ||
* `type`: Describes how to serialize and deserialize the attribute to/from a property. | ||
If this value is a function, it is used to deserialize the attribute value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So how does it get serialized in that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
a the property value. If it's an object, it can have keys for `fromAttribute` and | ||
`toAttribute` where `fromAttribute` is the deserialize function and `toAttribute` | ||
is a serialize function used to set the property to an attribute. If no `toAttribute` | ||
function is provided and `reflect` is set to true, the property value is set | ||
directly to the attribute. | ||
* `reflect`: Describes if the property should reflect to an attribute. | ||
If true, when the property is set, the attribute is set using the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true => There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
attribute name determined according to the rules for the `attribute` | ||
propety option and the value of the property serialized using the rules from | ||
the `type` property option. | ||
* `shouldInvalidate`: Describes if setting a property should trigger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this name might be a bit confusing, because as a dev I would think "why shouldn't it invalidade?" But when I read here it is more understandable that for complex objects equality is much more complex. Maybe I would turn it around and call it
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going to leave it as is for now to get some more feedback but will consider changing it. |
||
invalidation and updating. This function takes the `newValue` and `oldValue` and | ||
returns true if invalidation should occur. If not present, a strict identity | ||
check is used. This is useful if a property should be considered dirty only | ||
if some condition is met, like if a key of an object value changes. | ||
|
||
* **React to changes:** LitElement reacts to changes in properties and attributes by | ||
asynchronously rendering, ensuring changes are batched. This reduces overhead | ||
and maintains consistent state. | ||
|
@@ -68,10 +104,10 @@ and renders declaratively using `lit-html`. | |
## Minimal Example | ||
|
||
1. Create a class that extends `LitElement`. | ||
1. Implement a static `properties` getter that returns the element's properties | ||
(which automatically become observed attributes). | ||
1. Then implement a `_render(props)` method and use the element's | ||
current properties (props) to return a `lit-html` template result to render | ||
1. Use a `@property` decorator to create a property (or implement a static `properties` | ||
getter that returns the element's properties). (which automatically become observed attributes). | ||
1. Then implement a `render()` method and use the element's | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a minimal TypeScript example should be right up top to sell things a little. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
current properties to return a `lit-html` template result to render | ||
into the element. This is the only method that must be implemented by subclasses. | ||
|
||
```html | ||
|
@@ -81,11 +117,12 @@ into the element. This is the only method that must be implemented by subclasses | |
|
||
class MyElement extends LitElement { | ||
|
||
static get properties() { return { mood: String }} | ||
@property({type: String}) | ||
mood = 'happy'; | ||
|
||
_render({mood}) { | ||
render() { | ||
return html`<style> .mood { color: green; } </style> | ||
Web Components are <span class="mood">${mood}</span>!`; | ||
Web Components are <span class="mood">${this.mood}</span>!`; | ||
} | ||
|
||
} | ||
|
@@ -99,41 +136,71 @@ into the element. This is the only method that must be implemented by subclasses | |
## API Documentation | ||
|
||
See the [source](https://github.com/PolymerLabs/lit-element/blob/master/src/lit-element.ts#L90) | ||
for detailed API info, here are some highlights. Note, the leading underscore | ||
is used to indicate that these methods are | ||
[protected](https://en.wikipedia.org/wiki/Class_(computer_programming)#Member_accessibility); | ||
they are not private and can and should be implemented by subclasses. | ||
These methods generally are called as part of the rendering lifecycle and should | ||
not be called in user code unless otherwise indicated. | ||
|
||
* `_createRoot()`: Implement to customize where the | ||
for detailed API info, here are some highlights. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you only list highlights, how is it detailed then? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
|
||
* `createRenderRoot()` (protected): Implement to customize where the | ||
element's template is rendered by returning an element into which to | ||
render. By default this creates a shadowRoot for the element. | ||
To render into the element's childNodes, return `this`. | ||
|
||
* `_firstRendered()`: Called after the element DOM is rendered for the first time. | ||
|
||
* `_shouldRender(props, changedProps, prevProps)`: Implement to control if rendering | ||
should occur when property values change or `invalidate` is called. | ||
* `shouldUpdate(changedProperties)` (protected): Implement to control if updating and rendering | ||
should occur when property values change or `invalidate` is called. The `changedProps` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changedProps => changedProperties (you missed that during rename) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
argument is an object with keys for the changed properties pointing to their previous values. | ||
By default, this method always returns true, but this can be customized as | ||
an optimization to avoid rendering work when changes occur which should not be rendered. | ||
|
||
* `_render(props)`: Implement to describe the element's DOM using `lit-html`. Ideally, | ||
the `_render` implementation is a pure function using only `props` to describe | ||
the element template. This is the only method that must be implemented by subclasses. | ||
|
||
* `_didRender(props, changedProps, prevProps)`: Called after element DOM has been rendered. | ||
Implement to directly control rendered DOM. Typically this is not needed as `lit-html` | ||
can be used in the `_render` method to set properties, attributes, and | ||
event listeners. However, it is sometimes useful for calling methods on | ||
rendered elements, for example focusing an input: | ||
`this.shadowRoot.querySelector('input').focus()`. | ||
|
||
* `renderComplete`: Returns a promise which resolves after the element next renders. | ||
|
||
* `_requestRender`: Call to request the element to asynchronously re-render regardless | ||
an optimization to avoid updating work when changes occur, which should not be rendered. | ||
|
||
* `update()` (protected): This method calls `render()` and then uses `lit-html` to | ||
render the template DOM. Override to customize how the element renders DOM. Note, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to -> in order to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
during `update()` setting properties does not trigger `invalidate()`, allowing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comma after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
property values to be computed and validated. | ||
|
||
* `render()` (protected): Implement to describe the element's DOM using `lit-html`. Ideally, | ||
the `render` implementation is a pure function using only the element's current properties | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. link to description of what a pure function is, or describe it shortly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
to describe the element template. This is the only method that must be implemented by subclasses. | ||
Note, since `render()` is called by `update()` setting properties does not trigger | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comma after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
`invalidate()`, allowing property values to be computed and validated. | ||
|
||
* `finishUpdate(changedProperties): Promise?` (protected): Called after element DOM has been updated and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. element DOM => the element's DOM ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
before the `updateComplete` promise is resolved. Implement to directly control rendered DOM. | ||
Typically this is not needed as `lit-html` can be used in the `render` method | ||
to set properties, attributes, and event listeners. However, it is sometimes useful | ||
for calling methods on rendered elements, for example focusing an input: | ||
`this.shadowRoot.querySelector('input').focus()`. The `changedProps` argument is an object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changedProps => changedProperties There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
with keys for the changed properties pointing to their previous values. If this function | ||
returns a `Promise`, it will be *awaited* before resolving the `updateComplete` promise. | ||
Setting properties in `finishUpdate()` does trigger `invalidate()` and blocks | ||
the `updateComplete` promise. | ||
|
||
* `finishFirstUpdate(changedProperties): Promise?` (protected) Called after element DOM has been | ||
updated the first time. This method can be useful for capturing references to rendered static | ||
nodes that must be directly acted upon, for example in `finishUpdate`. | ||
|
||
* `updateComplete`: Returns a promise which resolves after the element next renders. | ||
|
||
* `invalidate`: Call to request the element to asynchronously update regardless | ||
of whether or not any property changes are pending. | ||
|
||
## Update Lifecycle | ||
|
||
* When the element is first connected or a property is set (e.g. `element.foo = 5`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find the nesting here to make this visually more complicated than it is. Also the "thens", "Notes" and other connector words. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rewrote |
||
and the property's `shouldInvalidate(value, oldValue)` returns true. Then | ||
* `invalidate()` tries to update the element after waiting a [microtask](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/) (at the end | ||
of the event loop, before the next paint). Then | ||
* `shouldUpdate(changedProps)` is called and if this returns true which it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comma before which There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changedProps again... also below... do a search for that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
does by default: | ||
* `update(changedProps)` is called to update the element. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
Note, setting properties inside `update()` will set their values but | ||
will *not* trigger `invalidate()`. This calls | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will after calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
* `render()` which should return a `lit-html` TemplateResult | ||
(e.g. <code>html\`Hello ${world}\`</code>) | ||
* `finishUpdate(changedProps)` is then called to do post update/render tasks. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
Note, setting properties here will trigger `invalidate()` and block | ||
the `updateComplete` promise. | ||
* `updateComplete` promise is resolved only if the element is | ||
not in an invalid state. | ||
* Any code awaiting the element's `updateComplete` promise runs and observes | ||
the element in the updated state. | ||
|
||
## Bigger Example | ||
|
||
```JavaScript | ||
|
@@ -142,25 +209,23 @@ import {LitElement, html} from '@polymer/lit-element'; | |
class MyElement extends LitElement { | ||
|
||
// Public property API that triggers re-render (synced with attributes) | ||
static get properties() { | ||
return { | ||
foo: String, | ||
whales: Number | ||
} | ||
} | ||
@property() | ||
foo = 'foo'; | ||
|
||
@property({type: Number}) | ||
whales = 5; | ||
|
||
constructor() { | ||
super(); | ||
this.foo = 'foo'; | ||
this.addEventListener('click', async (e) => { | ||
this.whales++; | ||
await this.renderComplete; | ||
await this.updateComplete; | ||
this.dispatchEvent(new CustomEvent('whales', {detail: {whales: this.whales}})) | ||
}); | ||
} | ||
|
||
// Render method should return a `TemplateResult` using the provided lit-html `html` tag function | ||
_render({foo, whales}) { | ||
render() { | ||
return html` | ||
<style> | ||
:host { | ||
|
@@ -170,8 +235,8 @@ class MyElement extends LitElement { | |
display: none; | ||
} | ||
</style> | ||
<h4>Foo: ${foo}</h4> | ||
<div>whales: ${'🐳'.repeat(whales)}</div> | ||
<h4>Foo: ${this.foo}</h4> | ||
<div>whales: ${'🐳'.repeat(this.whales)}</div> | ||
<slot></slot> | ||
`; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can render into light DOM as well, so maybe clarify