Skip to content

Commit dc3a10d

Browse files
author
Steven Orvell
committed
Merge branch 'master' into test-apply
2 parents 641fc8d + b8c53be commit dc3a10d

15 files changed

+971
-937
lines changed

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
<!--
9+
PRs should document their user-visible changes (if any) in the
10+
Unreleased section, uncommenting the header as necessary.
11+
-->
12+
13+
## Unreleased
14+
15+
### Added
16+
* Added `@query()`, `@queryAll()`, and `@customElement` decorators ([#159](https://github.com/Polymer/lit-element/pull/159))
17+
18+
<!-- ### Changed -->
19+
<!-- ### Removed -->
20+
<!-- ### Fixed -->

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ and renders declaratively using `lit-html`.
7070

7171
* The easiest way to try out LitElement is to use one of these online tools:
7272

73-
* Runs in all [supported](#supported-browsers) browsers: [StackBlitz](https://stackblitz.com/edit/lit-element-example?file=index.js), [Glitch](https://glitch.com/edit/#!/hello-lit-element?path=index.html)
73+
* Runs in all [supported](#supported-browsers) browsers: [Glitch](https://glitch.com/edit/#!/hello-lit-element?path=index.html)
7474

75-
* Runs in browsers with [JavaScript Modules](https://caniuse.com/#search=modules): [JSFiddle](https://jsfiddle.net/j6mf6gpo/), [JSBin](http://jsbin.com/zezilad/edit?html,output),
76-
[CodePen](https://codepen.io/sorvell/pen/BxZgPN).
75+
* Runs in browsers with [JavaScript Modules](https://caniuse.com/#search=modules): [JSFiddle](https://jsfiddle.net/rzhofu81/), [JSBin](http://jsbin.com/vecuyan/edit?html,output),
76+
[CodePen](https://codepen.io/sorvell/pen/RYQyoe?editors=1000).
7777

7878
* You can also copy [this HTML file](https://gist.githubusercontent.com/sorvell/48f4b7be35c8748e8f6db5c66d36ee29/raw/2427328cf1ebae5077902a6bff5ddd8db45e83e4/index.html) into a local file and run it in any browser that supports [JavaScript Modules]((https://caniuse.com/#search=modules)).
7979

@@ -275,3 +275,8 @@ Chrome, Safari, Opera, Firefox, Edge. In addition, Internet Explorer 11 is also
275275

276276
## Known Issues
277277

278+
* On very old versions of Safari (<=9) or Chrome (<=41), properties created for native
279+
platform properties like (`id` or `name`) may not have default values set in the element constructor.
280+
On these browsers native properties appear on instances and therefore their default value
281+
will overwrite any element default (e.g. if the element sets this.id = 'id' in the constructor,
282+
the 'id' will become '' since this is the native platform default).

package-lock.json

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@
2323
"devDependencies": {
2424
"@types/chai": "^4.0.1",
2525
"@types/mocha": "^5.2.4",
26-
"@webcomponents/webcomponentsjs": "^2.1.0",
26+
"@webcomponents/webcomponentsjs": "^2.1.1",
2727
"chai": "^4.0.2",
2828
"mocha": "^5.0.5",
2929
"rollup": "^0.64.1",
3030
"rollup-plugin-filesize": "^4.0.1",
31-
"rollup-plugin-node-resolve": "^3.3.0",
31+
"rollup-plugin-node-resolve": "^3.4.0",
3232
"rollup-plugin-terser": "^1.0.1",
3333
"tslint": "^5.7.0",
3434
"typedoc": "^0.8.0",
35-
"typescript": "^3.0.1",
35+
"typescript": "^3.0.3",
3636
"uglify-es": "^3.3.9",
3737
"wct-browser-legacy": "^1.0.1",
3838
"web-component-tester": "^6.8.0"
3939
},
4040
"typings": "lit-element.d.ts",
4141
"dependencies": {
42-
"lit-html": "^0.11.0"
42+
"lit-html": "^0.11.2"
4343
},
4444
"publishConfig": {
4545
"access": "public"

src/demo/ts-element.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { LitElement, html, property } from '../lit-element.js';
1+
import {html, LitElement, property} from '../lit-element.js';
22

33
class TSElement extends LitElement {
44

5-
@property()
6-
message = 'Hi';
5+
@property() message = 'Hi';
76

8-
@property({attribute: 'more-info', type: (value: string) => `[${value}]`})
7+
@property({attribute : 'more-info', type: (value: string) => `[${value}]`})
98
extra = '';
109

1110
render() {
@@ -18,6 +17,5 @@ class TSElement extends LitElement {
1817
</style>TSElement says: ${message} ${extra}
1918
`;
2019
}
21-
2220
}
2321
customElements.define('ts-element', TSElement);

src/lib/decorators.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
2+
/**
3+
* @license
4+
* Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
5+
* This code may only be used under the BSD style license found at
6+
* http://polymer.github.io/LICENSE.txt
7+
* The complete set of authors may be found at
8+
* http://polymer.github.io/AUTHORS.txt
9+
* The complete set of contributors may be found at
10+
* http://polymer.github.io/CONTRIBUTORS.txt
11+
* Code distributed by Google as part of the polymer project is also
12+
* subject to an additional IP rights grant found at
13+
* http://polymer.github.io/PATENTS.txt
14+
*/
15+
16+
import {LitElement} from '../lit-element.js';
17+
18+
import {PropertyDeclaration, UpdatingElement} from './updating-element.js';
19+
20+
export type Constructor<T> = {
21+
new (...args: unknown[]): T
22+
};
23+
24+
/**
25+
* Class decorator factory that defines the decorated class as a custom element.
26+
*
27+
* @param tagName the name of the custom element to define
28+
*
29+
* In TypeScript, the `tagName` passed to `customElement` must be a key of the
30+
* `HTMLElementTagNameMap` interface. To add your element to the interface,
31+
* declare the interface in this module:
32+
*
33+
* @customElement('my-element')
34+
* export class MyElement extends LitElement {}
35+
*
36+
* declare global {
37+
* interface HTMLElementTagNameMap {
38+
* 'my-element': MyElement;
39+
* }
40+
* }
41+
*
42+
*/
43+
export const customElement = (tagName: keyof HTMLElementTagNameMap) =>
44+
(clazz: Constructor<HTMLElement>) => {
45+
window.customElements.define(tagName, clazz);
46+
// Cast as any because TS doesn't recognize the return type as being a
47+
// subtype of the decorated class when clazz is typed as
48+
// `Constructor<HTMLElement>` for some reason. `Constructor<HTMLElement>`
49+
// is helpful to make sure the decorator is applied to elements however.
50+
return clazz as any;
51+
};
52+
53+
/**
54+
* A property decorator which creates a LitElement property which reflects a
55+
* corresponding attribute value. A `PropertyDeclaration` may optionally be
56+
* supplied to configure property features.
57+
*/
58+
export const property = (options?: PropertyDeclaration) => (proto: Object,
59+
name: string) => {
60+
(proto.constructor as typeof UpdatingElement).createProperty(name, options);
61+
};
62+
63+
/**
64+
* A property decorator that converts a class property into a getter that
65+
* executes a querySelector on the element's renderRoot.
66+
*/
67+
export const query = _query((target: NodeSelector, selector: string) =>
68+
target.querySelector(selector));
69+
70+
/**
71+
* A property decorator that converts a class property into a getter
72+
* that executes a querySelectorAll on the element's renderRoot.
73+
*/
74+
export const queryAll = _query((target: NodeSelector, selector: string) =>
75+
target.querySelectorAll(selector));
76+
77+
/**
78+
* Base-implementation of `@query` and `@queryAll` decorators.
79+
*
80+
* @param queryFn exectute a `selector` (ie, querySelector or querySelectorAll)
81+
* against `target`.
82+
*/
83+
function _query<T>(queryFn: (target: NodeSelector, selector: string) => T) {
84+
return (selector: string) => (proto: any, propName: string) => {
85+
Object.defineProperty(proto, propName, {
86+
get(this: LitElement) { return queryFn(this.renderRoot!, selector); },
87+
enumerable : true,
88+
configurable : true,
89+
});
90+
};
91+
}

0 commit comments

Comments
 (0)