Browse Source

fix: onSelector not triggering & allow all options

Sven 1 year ago
parent
commit
d0162ec024
2 changed files with 6 additions and 13 deletions
  1. 6 6
      lib/onSelector.ts
  2. 0 7
      lib/types.ts

+ 6 - 6
lib/onSelector.ts

@@ -1,4 +1,4 @@
-import type { InitOnSelectorOpts, OnSelectorOpts } from "./types";
+import type { OnSelectorOpts } from "./types";
 
 const selectorMap = new Map<string, OnSelectorOpts[]>();
 
@@ -39,7 +39,7 @@ function checkSelectorExists<TElem extends Element = HTMLElement>(selector: stri
   options.forEach((option, i) => {
     try {
       const elements = option.all ? document.querySelectorAll<HTMLElement>(selector) : document.querySelector<HTMLElement>(selector);
-      if(elements) {
+      if((elements !== null && elements instanceof NodeList && elements.length > 0) || elements !== null) {
         // I don't feel like dealing with intersecting types, this should work just fine at runtime
         // @ts-ignore
         option.listener(elements);
@@ -66,18 +66,18 @@ function checkSelectorExists<TElem extends Element = HTMLElement>(selector: stri
 
 /**
  * Initializes a MutationObserver that checks for all registered selectors whenever an element is added to or removed from the `<body>`
- * @param opts For fine-tuning when the MutationObserver checks for the selectors
+ * @param options For fine-tuning what triggers the MutationObserver's checking function - `subtree` and `childList` are set to true by default
  */
-export function initOnSelector(opts: InitOnSelectorOpts = {}) {
+export function initOnSelector(options: MutationObserverInit = {}) {
   const observer = new MutationObserver(() => {
     for(const [selector, options] of selectorMap.entries())
       checkSelectorExists(selector, options);
   });
 
   observer.observe(document.body, {
-    ...opts,
-    // subtree: true, // this setting applies the options to the childList (which isn't necessary in this use case)
+    subtree: true,
     childList: true,
+    ...options,
   });
 }
 

+ 0 - 7
lib/types.ts

@@ -1,12 +1,5 @@
 //#SECTION selector exists
 
-export type InitOnSelectorOpts = {
-  /** Set to true if mutations to any element's attributes are to also trigger the onSelector check (warning: this might draw a lot of performance on larger sites) */
-  attributes?: boolean;
-  /** Set to true if mutations to any element's character data are to also trigger the onSelector check (warning: this might draw a lot of performance on larger sites) */
-  characterData?: boolean;
-}
-
 export type OnSelectorOpts<TElem extends Element = HTMLElement> = SelectorOptsOne<TElem> | SelectorOptsAll<TElem>;
 
 type SelectorOptsOne<TElem extends Element> = SelectorOptsBase & {