Skip to content

Commit 4a58caa

Browse files
committed
feat: addGlobalStyle now returns the style element (closes #31)
1 parent 068d391 commit 4a58caa

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

.changeset/mean-elephants-pull.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sv443-network/userutils": minor
3+
---
4+
5+
`addGlobalStyle` now returns the created style element

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -506,10 +506,11 @@ addParent(element, newParent);
506506
### addGlobalStyle()
507507
Usage:
508508
```ts
509-
addGlobalStyle(css: string): void
509+
addGlobalStyle(css: string): HTMLStyleElement
510510
```
511511

512512
Adds a global style to the page in form of a `<style>` element that's inserted into the `<head>`.
513+
Returns the style element that was just created.
513514
⚠️ This function needs to be run after the DOM has loaded (when using `@run-at document-end` or after `DOMContentLoaded` has fired).
514515

515516
<details><summary><b>Example - click to view</b></summary>

lib/dom.ts

+2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ export function addParent(element: Element, newParent: Element) {
4040
* Adds global CSS style in the form of a `<style>` element in the document's `<head>`
4141
* This needs to be run after the `DOMContentLoaded` event has fired on the document object (or instantly if `@run-at document-end` is used).
4242
* @param style CSS string
43+
* @returns Returns the created style element
4344
*/
4445
export function addGlobalStyle(style: string) {
4546
const styleElem = document.createElement("style");
4647
styleElem.innerHTML = style;
4748
document.head.appendChild(styleElem);
49+
return styleElem;
4850
}
4951

5052
/**

0 commit comments

Comments
 (0)