-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hiccup-css): add injectStyleSheet()
- Loading branch information
1 parent
244bf21
commit 8d6e6c8
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |