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

Refine API and refactor #132

Merged
merged 66 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
94d4e48
Refactor API (wip)
Jul 20, 2018
f8f8862
remove microtask lib in favor of Promise.
Jul 25, 2018
df3c39d
API changes
Aug 3, 2018
dc5d931
Update tests and Readme
Aug 4, 2018
5861e0e
Tweaks based on review.
Aug 4, 2018
8f83306
More review tweaks.
Aug 4, 2018
e18819e
Lint fixes.
Aug 4, 2018
da54543
work around custom elements polyfill issue in tests
Aug 5, 2018
b39b480
Changes based on review
Aug 5, 2018
5ef504e
Documentation updates and type tweaks based on review.
Aug 7, 2018
10fdf29
Allow symbols as properties
Aug 7, 2018
f131d96
Fix property to attributes serialization
Aug 8, 2018
3b5611c
Doc improvements based on review.
Aug 8, 2018
82be06b
Export `PropertyValues` type and fixup usage in tests
Aug 8, 2018
3637c1e
Fixes for IE11
Aug 8, 2018
bbd3267
Adds `firstUpdated`
Aug 8, 2018
3f0315c
Doc tweak based on review.
Aug 8, 2018
85f4cf2
Use new syntax in lit-html rather than old syntax in lit-extended.
Aug 13, 2018
a8d888d
Updates based on review.
Aug 13, 2018
e97392e
Import from core.
Aug 13, 2018
00d9d5c
Updates based on review.
Aug 13, 2018
f2f908d
Ensure properties set in update reflect properly
Aug 14, 2018
8160de8
Add notes about limited Symbol support and skipped test with linked i…
Aug 14, 2018
e4c7d91
Removed getProperty/setProperty
Aug 14, 2018
e6a3c70
Remove unused `_propertyValues`
Aug 14, 2018
b39b979
Support properties defined as symbols
Aug 15, 2018
fed3307
Save flags on element by using bitmask
Aug 15, 2018
5f3cffd
Update package.json
Aug 15, 2018
05ba1e4
Fix IE11 Map support
Aug 22, 2018
4361daf
Update wct package.
Aug 22, 2018
baaf296
Refine properties handling and add optimizations
Aug 22, 2018
f2865de
Fix exports
Aug 22, 2018
d4edcd8
Update tests for `shouldInvalidate` fix
Aug 22, 2018
9f1a709
Remove `BooleanAttribute` and special case `type: Boolean`
Aug 22, 2018
25f4ca6
Adds `invalidateProperty(name, oldValue)`
Aug 22, 2018
9f8e145
Simplify update cycle based on feedback
Aug 22, 2018
59cd15e
Documentation fixes based on feedback.
Aug 22, 2018
bf6da1d
Adds tests for setting properties and attributes in sub-elements.
Aug 23, 2018
f684c63
Fix tests to test updateComplete return value.
Aug 23, 2018
1953a79
Fix typo.
Aug 23, 2018
f3f28b8
Avoid use of Map.keys for IE compat.
Aug 23, 2018
234dff0
Adds `firstRendered` to LitElement and exposes static `render` method.
Aug 23, 2018
24ac381
Removes `finishFirstUpdate` and `finishUpdate`
Aug 23, 2018
13e48e6
Add tests for `@property` directive.
Aug 23, 2018
d708572
Add utility for checking size.
Aug 23, 2018
9a9aced
Documentation fixes based on feedback.
Aug 23, 2018
4bab118
Fix lint errors.
Aug 23, 2018
88a1ad9
Update gitignore and add rollup.config.js
Aug 23, 2018
10edcc6
Don't bother memoizing `observedAttributes` since it's accessed once.
Aug 23, 2018
a447927
IE11 test fixes and slight change to `styleString`
Aug 23, 2018
ec02fd8
Updates based on review.
Aug 23, 2018
b77ee89
Minor doc fixes
Aug 23, 2018
b0c7428
`invalidate` waits for the previous update to complete before advanci…
Aug 23, 2018
9341ae2
Make render abstract
Aug 24, 2018
d3a781b
Rename flag based on review
Aug 24, 2018
af1c542
Renaming based on feedback
Aug 24, 2018
e9d400e
Remove unnecessary check
Aug 24, 2018
bb6d73d
Bump version
Aug 24, 2018
55bc308
Bump lit-html constraint to require 0.11.0-dev.3 (#151)
justinfagnani Aug 24, 2018
b611732
Bump version to 0.6.0-dev.5
justinfagnani Aug 24, 2018
d11ba45
Fix tests on Safari 10
Aug 24, 2018
a85f4bb
Merge branch 'refactor' of https://github.com/PolymerLabs/polymer-lit…
Aug 24, 2018
c312464
Fix tests on Safari 9
Aug 24, 2018
84180cd
Now fix tests on Edge 17
Aug 24, 2018
49cc74c
Update Safari 11 version to test
Aug 24, 2018
ffbfead
Avoid Firefox timeout issue on Travis by testing on Sauce
Aug 24, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 75 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,22 @@

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)
Copy link
Contributor

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

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to help manage

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

and renders declaratively using `lit-html`.

* **Setup properties:** LitElement supports a static `properties` getter in which element property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest leading with a more generic description of properties:

LitElement supports observable properties which may trigger an update when set. These properties can be written in a few ways:

  • As class fields with the @property decorator (with links to class fields and decorators proposals?), if you're using a compiler that supports them, like TypeScript or Babel.
  • With a static properties getter or property.
  • By manually writing getters and setters

And then go into each method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

accessors are described. For each property, an object configures settings where the options are:

* attribute: if false, do not add to observedAttributes, if true or absent, observe the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add backticks around the property names: attribute, observedAttributes, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the leading word after the colon should be capitalized, and each "If...` clause a sentence.

Copy link
Contributor

@kenchris kenchris Aug 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing the camel casing automatically, why not also allow attribute: 'my-custom-name' as well, so that I can choose the mapping name that I want

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be exactly what you are doing in the TS example:

@property({attribute: 'more-info', type: (value: string) => `[${value}]`})

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, you seems to be doing exactly what I wanted, but the documentation doesn't mention that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

lowercased property name, if a string observe that value.
* type: if a function, use to deserialize the attribute value to the property value;
also can be an object with `{fromAttribute, toAttribute}` where `fromAttribute` is
the deserialize function and `toAttribute` is a serialize function.
* reflect: if true, on setting the property reflects the property value to an
attribute value using `type.toAttribute` if it exists.
* shouldInvalidate: optional function which should return true if setting the property
should cause the element to invalidate causing it to asynchronously update.

* **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.
Expand Down Expand Up @@ -70,8 +81,8 @@ and renders declaratively using `lit-html`.
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. Then implement a `render()` method and use the element's
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand All @@ -81,11 +92,11 @@ into the element. This is the only method that must be implemented by subclasses

class MyElement extends LitElement {

static get properties() { return { mood: String }}
static get properties() { return { mood: {type: String} }}

_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>!`;
}

}
Expand All @@ -99,41 +110,66 @@ 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you only list highlights, how is it detailed then? :)

Copy link
Member Author

Choose a reason for hiding this comment

The 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(changedProps)` (protected): Implement to control if updating and rendering
should occur when property values change or `invalidate` is called. The `changedProps`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changedProps => changedProperties (you missed that during rename)

Copy link
Member Author

Choose a reason for hiding this comment

The 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:
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to -> in order to

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

during `update()` setting properties does not trigger `invalidate()`, allowing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma after update()

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma after update()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

`invalidate()`, allowing property values to be computed and validated.

* `finishUpdate(changedProps)` (protected): Called after element DOM has been updated and
before the `updateComplete` promise is resolved. The `changedProps` argument is an object
with keys for the changed properties pointing to their previous values. This is an
async function which is *awaited* before resolving the `updateComplete` promise.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Async or not is an implementation detail of a function. I'd just say it can return a Promise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Setting properties in `finishUpdate()` does trigger `invalidate()` and blocks
the `updateComplete` promise. 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.
* `updateComplete`: Returns a promise which resolves after the element next renders.

* `_requestRender`: Call to request the element to asynchronously re-render regardless
* `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`)
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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. Then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does regular users understand about microtasks? Like to explanation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* `shouldUpdate(changedProps)` is called and if this returns true which it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comma before which

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changedProps again... also below... do a search for that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

does by default:
* `update(changedProps)` is called to update the element.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will after calling super.update() right

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand All @@ -144,8 +180,8 @@ class MyElement extends LitElement {
// Public property API that triggers re-render (synced with attributes)
static get properties() {
return {
foo: String,
whales: Number
foo: {type: String},
whales: {type: Number}
}
}

Expand All @@ -154,13 +190,13 @@ class MyElement extends LitElement {
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 {
Expand All @@ -170,8 +206,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>
`;
}
Expand Down
61 changes: 49 additions & 12 deletions demo/lit-element.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,60 @@
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0, user-scalable=yes">
<title>lit-element demo</title>
<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
<script type="module" src = "./ts-element.js"></script>
</head>
<body>
<my-element bar="5" whales="5" foobar="17">Hi</my-element>
<hr>
<ts-element message="Yo" more-info="person"></ts-element>

<script type="module">
import { LitElement, html } from '../lit-element.js';

class Inner extends LitElement {
render() {
return html`Hello world`;
}
}

customElements.define('x-inner', Inner);

class MyElement extends LitElement {

static get properties() {
return {
foo: String,
bar: Number,
whales: Number
nug: {},
zot: {},
foo: {},
bar: {},
whales: {type: Number},
fooBar: {type: {fromAttribute: parseInt, toAttribute: (value) => value + '-attr'}, reflect: true}
}
}

get zot() { return this.getProperty('zot'); }

set zot(value) {
this.setProperty('zot', Number(value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if the example explained exactly what you are trying to accomplish here. Like why is this useful?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably something to cover in a getting started guide, but tweaked it a bit.

}


constructor() {
super();
this.foo = 'foo';
this.nug = [1, 2, 3];
this.whales = 0;
this.zot = '5';
this.addEventListener('click', async (e) => {
this.whales++;
await this.renderComplete;
await this.updateComplete;
this.dispatchEvent(new CustomEvent('whales', {detail: {whales: this.whales}}))
console.log(this.shadowRoot.querySelector('.count').textContent);
});
}

_firstRendered() {
console.log('first rendered!');
}

_render({foo, bar, whales}) {
render() {
const {foo, bar, whales, fooBar, nug} = this;
return html`
<style>
:host {
Expand All @@ -69,16 +91,31 @@ <h4 on-click="${(e) => console.log(this, e.target)}">Foo: ${foo}, Bar: ${bar}</h
<div class="count">
whales: ${'🐳'.repeat(whales)}
</div>
<ul>
${nug.map(n => html`<li>${n}</li>`)}
</ul>
<x-inner></x-inner>
`;
}

_didRender() {
console.log('didRender!');
update(changedProps) {
super.update();
console.log('updated!', changedProps);
}

async finishUpdate(changedProps) {
if (!this._inner) {
this._inner = this.shadowRoot.querySelector('x-inner');
}
await this._inner.updateComplete;
}
}

customElements.define('my-element', MyElement);
(async () => {
const x = document.querySelector('my-element');
await x.updateComplete;
console.log(x.shadowRoot.querySelector('x-inner').shadowRoot.textContent);
})();
</script>
<my-element bar="5" whales="5">Hi</my-element>
</body></html>
Loading