Skip to content

Commit

Permalink
feat(hiccup-css): add injectStyleSheet()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jun 8, 2018
1 parent 244bf21 commit 8d6e6c8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/hiccup-css/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./comment";
export * from "./conditional";
export * from "./css";
export * from "./import";
export * from "./inject";
export * from "./keyframes";
export * from "./media";
export * from "./namespace";
Expand Down
27 changes: 27 additions & 0 deletions packages/hiccup-css/src/inject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// https://davidwalsh.name/add-rules-stylesheets

/**
* Injects given CSS string as global stylesheet in DOM head. If `first`
* is true, inserts it as first stylesheet, else (default) appends it.
*
* Returns created style DOM element.
*
* @param css
* @param first
*/
export const injectStyleSheet = (css: string, first = false) => {
const head = document.getElementsByTagName("head")[0];
const sheet = document.createElement("style");
sheet.setAttribute("type", "text/css");
if ((<any>sheet).styleSheet !== undefined) {
(<any>sheet).styleSheet.cssText = css;
} else {
sheet.textContent = css;
}
if (first) {
head.insertBefore(sheet, head.firstChild);
} else {
head.appendChild(sheet);
}
return sheet;
};

0 comments on commit 8d6e6c8

Please sign in to comment.