-
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
Adds static get styles()
#401
Changes from 3 commits
670eb4c
b593e1d
fd881dc
8ae6692
8faa0c7
56bfa0b
58bd5be
6a4649e
ca6206d
dcbffef
583f515
352494a
6f6afec
80737c0
27eb2ad
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
@license | ||
Copyright (c) 2017 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
*/ | ||
export const supportsAdoptedStyleSheets = ('adoptedStyleSheets' in Document.prototype); | ||
|
||
interface ConstructableStyleSheet extends CSSStyleSheet { | ||
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. According to w3c/csswg-drafts#3433 these additions are going to be merged into 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. |
||
replaceSync(cssText: string): void; | ||
replace(cssText: string): Promise<unknown>; | ||
} | ||
|
||
class CSSLiteral { | ||
|
||
value: string; | ||
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.
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. |
||
|
||
constructor(value: string) { | ||
this.value = value.toString(); | ||
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. Why 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. Removed. |
||
} | ||
|
||
toString() { | ||
return this.value; | ||
} | ||
} | ||
|
||
const cssLiteralValue = (value: CSSLiteral) => { | ||
if (value instanceof CSSLiteral) { | ||
return value.value; | ||
} else { | ||
throw new Error( | ||
`Non-literal value passed to 'css' function: ${value}.` | ||
); | ||
} | ||
}; | ||
|
||
export type CSSStyleSheetOrCssText = { | ||
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 don't think 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. Changed. |
||
cssText?: CSSLiteral, | ||
styleSheet?: ConstructableStyleSheet} | ||
; | ||
|
||
export const css = (strings: TemplateStringsArray, ...values: CSSLiteral[]): CSSStyleSheetOrCssText => { | ||
const cssText = values.reduce((acc, v, idx) => | ||
acc + cssLiteralValue(v) + strings[idx + 1], strings[0]); | ||
const result: CSSStyleSheetOrCssText = {}; | ||
if (supportsAdoptedStyleSheets) { | ||
result.styleSheet = new CSSStyleSheet() as ConstructableStyleSheet; | ||
result.styleSheet.replaceSync(cssText); | ||
} else { | ||
result.cssText = new CSSLiteral(cssText); | ||
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. you can always assign this field |
||
} | ||
return result; | ||
}; | ||
|
||
export const cssLiteral = (strings: TemplateStringsArray, ...values: any[]) => { | ||
return new CSSLiteral(values.reduce((acc, v, idx) => | ||
acc + cssLiteralValue(v) + strings[idx + 1], strings[0])); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,19 @@ | |
* subject to an additional IP rights grant found at | ||
* http://polymer.github.io/PATENTS.txt | ||
*/ | ||
import {supportsAdoptedStyleSheets, CSSStyleSheetOrCssText} from './css-tag.js'; | ||
export * from './css-tag.js'; | ||
|
||
// Augment existing types with bleeding edge API. | ||
declare global { | ||
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 we may want these here: https://github.com/Polymer/lit-element/blob/master/src/env.d.ts 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. Added types to |
||
interface ShadyCSS { | ||
prepareAdoptedCssText(cssText: string[], name: string): void; | ||
} | ||
|
||
interface ShadowRoot { | ||
adoptedStyleSheets: CSSStyleSheet[]; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the property descriptor for a property on this prototype by walking | ||
|
@@ -215,6 +228,14 @@ export abstract class UpdatingElement extends HTMLElement { | |
|
||
static properties: PropertyDeclarations = {}; | ||
|
||
/** | ||
* Array of styles to apply to the element. The styles should be defined | ||
* using the `css` tag function. | ||
*/ | ||
static get styles(): CSSStyleSheetOrCssText[] { | ||
return []; | ||
} | ||
|
||
/** | ||
* Returns a list of attributes corresponding to the registered properties. | ||
*/ | ||
|
@@ -462,7 +483,39 @@ export abstract class UpdatingElement extends HTMLElement { | |
* @returns {Element|DocumentFragment} Returns a node into which to render. | ||
*/ | ||
protected createRenderRoot(): Element|ShadowRoot { | ||
return this.attachShadow({mode : 'open'}); | ||
const shadowRoot = this.attachShadow({mode : 'open'}); | ||
this.createRenderRootStyles(shadowRoot); | ||
return shadowRoot; | ||
} | ||
|
||
/** | ||
* Applies styling to the element shadowRoot using the `static get styles` | ||
* property. Styling will apply using `shadowRoot.adoptedStyleSheets` where | ||
* available and will fallback otherwise. | ||
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. Let's describe the fallback. Specifically that it appends 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. Added. |
||
*/ | ||
protected createRenderRootStyles(shadowRoot: ShadowRoot) { | ||
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. The name is too generic, since this doesn't work with any render root, but only ShadowRoots. How about 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. Changed to |
||
const styles = (this.constructor as typeof UpdatingElement).styles; | ||
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 (weakly) feel like all this should be in LitElement, since it does the actual rendering. 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 seems like the |
||
// There are three separate cases here based on Shadow DOM support: | ||
// (1) shadowRoot polyfilled: use ShadyCSS | ||
// (2) shadowRoot.adoptedStyleSheets available: use it. | ||
// (3) shadowRoot.adoptedStyleSheets polyfilled: add styles after rendering. | ||
if (window.ShadyCSS !== undefined && !(window.ShadyCSS as any).nativeShadow) { | ||
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. comment the three cases 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. |
||
window.ShadyCSS.prepareAdoptedCssText(styles.map((s) => s.cssText!.toString()), this.localName); | ||
} else if (supportsAdoptedStyleSheets) { | ||
shadowRoot.adoptedStyleSheets = styles.map((s) => s.styleSheet!); | ||
} else { | ||
// Ensure styling comes after rendering so styles are *after* all other rendered content. | ||
// This matches the spec'd behavior of `adoptedStyleSheets`. | ||
// Ensure an updated is requested and then render styling directly after the update. | ||
this.requestUpdate(); | ||
this._updatePromise.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. Changing 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. Since |
||
styles.forEach((s) => { | ||
const style = document.createElement('style'); | ||
style.textContent = s.cssText!.toString(); | ||
shadowRoot.appendChild(style); | ||
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 match Constructible StyleSheet ordering these have to come first in the ShadowRoot. Is that happening because you're directly waiting 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. Yup, comment added. Note, they come last in 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. Don't they have to come first in case there are 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. Ok, I see: https://wicg.github.io/construct-stylesheets/#using-constructed-stylesheets Let's include a link to the spec |
||
}); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
|
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.
Should be 2019 I assume