Skip to content

Instantly share code, notes, and snippets.

@anoblet
Created November 15, 2019 22:56
Show Gist options
  • Save anoblet/30a31ae87015476634f3a51f3014c176 to your computer and use it in GitHub Desktop.
Save anoblet/30a31ae87015476634f3a51f3014c176 to your computer and use it in GitHub Desktop.
import { CSSResult, LitElement } from "lit-element";
/**
* Takes a node, CSSResult and appends it
*/
export const applyStyle = (node: HTMLElement, style: CSSResult) => {
if ("adoptedStyleSheets" in document && node.shadowRoot) {
const shadowRoot: any = node.shadowRoot;
const sheets = shadowRoot.adoptedStyleSheets;
shadowRoot.adoptedStyleSheets = [...sheets, style.styleSheet];
} else {
const styleNode = document.createElement("style");
styleNode.textContent = style.cssText;
if (node.shadowRoot) node.shadowRoot.appendChild(styleNode);
else node.appendChild(styleNode);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment