Ver código fonte

feat: addGlobalStyle now returns the style element (closes #31)

Sv443 1 ano atrás
pai
commit
4a58caa
3 arquivos alterados com 9 adições e 1 exclusões
  1. 5 0
      .changeset/mean-elephants-pull.md
  2. 2 1
      README.md
  3. 2 0
      lib/dom.ts

+ 5 - 0
.changeset/mean-elephants-pull.md

@@ -0,0 +1,5 @@
+---
+"@sv443-network/userutils": minor
+---
+
+`addGlobalStyle` now returns the created style element

+ 2 - 1
README.md

@@ -506,10 +506,11 @@ addParent(element, newParent);
 ### addGlobalStyle()
 Usage:  
 ```ts
-addGlobalStyle(css: string): void
+addGlobalStyle(css: string): HTMLStyleElement
 ```
   
 Adds a global style to the page in form of a `<style>` element that's inserted into the `<head>`.  
+Returns the style element that was just created.  
 ⚠️ This function needs to be run after the DOM has loaded (when using `@run-at document-end` or after `DOMContentLoaded` has fired).  
   
 <details><summary><b>Example - click to view</b></summary>

+ 2 - 0
lib/dom.ts

@@ -40,11 +40,13 @@ export function addParent(element: Element, newParent: Element) {
  * Adds global CSS style in the form of a `<style>` element in the document's `<head>`  
  * This needs to be run after the `DOMContentLoaded` event has fired on the document object (or instantly if `@run-at document-end` is used).
  * @param style CSS string
+ * @returns Returns the created style element
  */
 export function addGlobalStyle(style: string) {
   const styleElem = document.createElement("style");
   styleElem.innerHTML = style;
   document.head.appendChild(styleElem);
+  return styleElem;
 }
 
 /**