1 | /**
|
2 | * @license
|
3 | * Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
|
4 | * This code may only be used under the BSD style license found at
|
5 | * http://polymer.github.io/LICENSE.txt
|
6 | * The complete set of authors may be found at
|
7 | * http://polymer.github.io/AUTHORS.txt
|
8 | * The complete set of contributors may be found at
|
9 | * http://polymer.github.io/CONTRIBUTORS.txt
|
10 | * Code distributed by Google as part of the polymer project is also
|
11 | * subject to an additional IP rights grant found at
|
12 | * http://polymer.github.io/PATENTS.txt
|
13 | */
|
14 | import { TemplateResult } from 'lit-html';
|
15 | import { render } from 'lit-html/lib/shady-render';
|
16 | import { UpdatingElement } from './lib/updating-element.js';
|
17 | export * from './lib/updating-element.js';
|
18 | export * from './lib/decorators.js';
|
19 | export { html, svg, TemplateResult, SVGTemplateResult } from 'lit-html/lit-html';
|
20 | import { supportsAdoptingStyleSheets } from './lib/css-tag.js';
|
21 | export * from './lib/css-tag.js';
|
22 | export class LitElement extends UpdatingElement {
|
23 | /**
|
24 | * Array of styles to apply to the element. The styles should be defined
|
25 | * using the `css` tag function.
|
26 | */
|
27 | static get styles() { return []; }
|
28 | static get _uniqueStyles() {
|
29 | if (this._styles === undefined) {
|
30 | const styles = this.styles;
|
31 | // As a performance optimization to avoid duplicated styling that can
|
32 | // occur especially when composing via subclassing, de-duplicate styles
|
33 | // preserving the last item in the list. The last item is kept to
|
34 | // try to preserve cascade order with the assumption that it's most
|
35 | // important that last added styles override previous styles.
|
36 | const styleSet = styles.reduceRight((set, s) => {
|
37 | set.add(s);
|
38 | // on IE set.add does not return the set.
|
39 | return set;
|
40 | }, new Set());
|
41 | // Array.form does not work on Set in IE
|
42 | this._styles = [];
|
43 | styleSet.forEach((v) => this._styles.unshift(v));
|
44 | }
|
45 | return this._styles;
|
46 | }
|
47 | /**
|
48 | * Performs element initialization. By default this calls `createRenderRoot`
|
49 | * to create the element `renderRoot` node and captures any pre-set values for
|
50 | * registered properties.
|
51 | */
|
52 | initialize() {
|
53 | super.initialize();
|
54 | this.renderRoot = this.createRenderRoot();
|
55 | // Note, if renderRoot is not a shadowRoot, styles would/could apply to the
|
56 | // element's getRootNode(). While this could be done, we're choosing not to
|
57 | // support this now since it would require different logic around de-duping.
|
58 | if (window.ShadowRoot && this.renderRoot instanceof window.ShadowRoot) {
|
59 | this.adoptStyles();
|
60 | }
|
61 | }
|
62 | /**
|
63 | * Returns the node into which the element should render and by default
|
64 | * creates and returns an open shadowRoot. Implement to customize where the
|
65 | * element's DOM is rendered. For example, to render into the element's
|
66 | * childNodes, return `this`.
|
67 | * @returns {Element|DocumentFragment} Returns a node into which to render.
|
68 | */
|
69 | createRenderRoot() {
|
70 | return this.attachShadow({ mode: 'open' });
|
71 | }
|
72 | /**
|
73 | * Applies styling to the element shadowRoot using the `static get styles`
|
74 | * property. Styling will apply using `shadowRoot.adoptedStyleSheets` where
|
75 | * available and will fallback otherwise. When Shadow DOM is polyfilled,
|
76 | * ShadyCSS scopes styles and adds them to the document. When Shadow DOM
|
77 | * is available but `adoptedStyleSheets` is not, styles are appended to the
|
78 | * end of the `shadowRoot` to [mimic spec
|
79 | * behavior](https://wicg.github.io/construct-stylesheets/#using-constructed-stylesheets).
|
80 | */
|
81 | adoptStyles() {
|
82 | const styles = this.constructor._uniqueStyles;
|
83 | if (styles.length === 0) {
|
84 | return;
|
85 | }
|
86 | // There are three separate cases here based on Shadow DOM support.
|
87 | // (1) shadowRoot polyfilled: use ShadyCSS
|
88 | // (2) shadowRoot.adoptedStyleSheets available: use it.
|
89 | // (3) shadowRoot.adoptedStyleSheets polyfilled: append styles after
|
90 | // rendering
|
91 | if (window.ShadyCSS !== undefined && !window.ShadyCSS.nativeShadow) {
|
92 | window.ShadyCSS.ScopingShim.prepareAdoptedCssText(styles.map((s) => s.cssText), this.localName);
|
93 | }
|
94 | else if (supportsAdoptingStyleSheets) {
|
95 | this.renderRoot.adoptedStyleSheets =
|
96 | styles.map((s) => s.styleSheet);
|
97 | }
|
98 | else {
|
99 | // This must be done after rendering so the actual style insertion is done
|
100 | // in `update`.
|
101 | this._needsShimAdoptedStyleSheets = true;
|
102 | }
|
103 | }
|
104 | connectedCallback() {
|
105 | super.connectedCallback();
|
106 | // Note, first update/render handles styleElement so we only call this if
|
107 | // connected after first update.
|
108 | if (this.hasUpdated && window.ShadyCSS !== undefined) {
|
109 | window.ShadyCSS.styleElement(this);
|
110 | }
|
111 | }
|
112 | /**
|
113 | * Updates the element. This method reflects property values to attributes
|
114 | * and calls `render` to render DOM via lit-html. Setting properties inside
|
115 | * this method will *not* trigger another update.
|
116 | * * @param _changedProperties Map of changed properties with old values
|
117 | */
|
118 | update(changedProperties) {
|
119 | super.update(changedProperties);
|
120 | const templateResult = this.render();
|
121 | if (templateResult instanceof TemplateResult) {
|
122 | this.constructor
|
123 | .render(templateResult, this.renderRoot, { scopeName: this.localName, eventContext: this });
|
124 | }
|
125 | // When native Shadow DOM is used but adoptedStyles are not supported,
|
126 | // insert styling after rendering to ensure adoptedStyles have highest
|
127 | // priority.
|
128 | if (this._needsShimAdoptedStyleSheets) {
|
129 | this._needsShimAdoptedStyleSheets = false;
|
130 | this.constructor._uniqueStyles.forEach((s) => {
|
131 | const style = document.createElement('style');
|
132 | style.textContent = s.cssText;
|
133 | this.renderRoot.appendChild(style);
|
134 | });
|
135 | }
|
136 | }
|
137 | /**
|
138 | * Invoked on each update to perform rendering tasks. This method must return
|
139 | * a lit-html TemplateResult. Setting properties inside this method will *not*
|
140 | * trigger the element to update.
|
141 | */
|
142 | render() { }
|
143 | }
|
144 | /**
|
145 | * Ensure this class is marked as `finalized` as an optimization ensuring
|
146 | * it will not needlessly try to `finalize`.
|
147 | */
|
148 | LitElement.finalized = true;
|
149 | /**
|
150 | * Render method used to render the lit-html TemplateResult to the element's
|
151 | * DOM.
|
152 | * @param {TemplateResult} Template to render.
|
153 | * @param {Element|DocumentFragment} Node into which to render.
|
154 | * @param {String} Element name.
|
155 | * @nocollapse
|
156 | */
|
157 | LitElement.render = render;
|
158 | //# sourceMappingURL=lit-element.js.map |
\ | No newline at end of file |