@blinkk/degu
    Preparing search index...

    Class DomWatcher

    A class that helps with DOM events. The main usecase for this class is to be able to watch the dom and then later remove a group of events all at once.

    Basic Usage

    let watcher = new DomWatcher();

    var scrollCallback = (event, done)=> {
    // on scroll events.
    };
    watcher.add({
    element: window,
    on: 'scroll',
    callback: scrollCallback,
    eventOptions: { passive: true }
    })

    watcher.add({
    element: element,
    on: 'click',
    callback: ()=> {},
    );


    # Add multiple events
    watcher.add({
    element: element,
    on: ['click', 'mousemove'],
    callback: ()=> {},
    );


    // Removes all watchers.
    watcher.removeAll();

    Advanced Usage

    let new watcher = new DomWatcher();

    // Removes by Id
    watcher.add({
    element: element,
    on: 'click',
    callback: ()=> {},
    id: 'abc'
    );


    // Runs the callback associated with abc. NOTE this is just dry running it
    // so it's not an actual browser event (hence no event data is provided).
    watcher.run('abc');

    watcher.removeById('abc');


    // Ids actually don't need to be unique.
    watcher.add({ element: element, on: 'click', callback: ()=> {}, id: 'group1');
    watcher.add({ element: anotherElement, on: 'mousemove', callback: ()=> {}, id: 'group1');
    watcher.removeById('group1');

    // Conditional execution
    watcher.add({
    element: window
    callback: ()=> {
    console.log('called only on mobile');
    }
    eventOptions: { passive: true }
    on: 'scroll',
    runWhen: window.innerWidth < 600
    });


    watcher.add({
    element: submitElement
    callback: ()=> {
    console.log('submitted');
    }
    on: 'click',
    runWhen: ()=> { return this.validate()}
    });

    https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver DomWatcher supports resizeObserver. To use it simply, watch the resize of an element that isn't the window. Using window will use the standard window resize events.

        watcher.add({
    element: myElement,
    on: 'resize',
    callback: (entries) => {
    ....
    },
    });

    https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver DomWatcher supports mutation observers. You can pass mutation config to the event options.


    const watchMutations = ()=> {
    watcher.add({
    element: myElement,
    id: 'mutations',
    on: 'mutation',
    callback: (mutationList) => {
    ....
    },
    { childList: true }
    });
    }

    // Temporarily stop watching mutations.
    this.watcher.removeById('mutations');
    ... (update dom, do something)
    // Rewatch mutations.
    watchMutations();
    // Add debouncing.
    watcher.add({
    element: document.body,
    on: 'mousemove',
    callback: func.debounce((event) => {
    console.log('movemove!!');
    }, 500),
    });

    How can I use this with bom.smartResize? Just use the 'smartResize' event instead.


    watcher.add({
    element: window,
    on: 'smartResize',
    callback: (event) => {
    console.log('smart resizing');
    },
    });

    Culling events only when an element is inview. See example/scroll-demo for actual example.

      let ev = elementVisibility.inview(element, {});
    let watcher = new DomWatcher();
    watcher.add({
    element: window,
    on: 'scroll',
    eventOptions: { passive: true },
    callback: ()=> {
    // Runs only when this element is inview.
    },
    // Tells to run scroll ONLY when the element is inview.
    runWhen: ()=> { return ev.state().inview; }
    });


    // Later
    ev.dispose();
    watcher.dispose();

    https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia DomWatcher supports MediaQueryLists that result from calls to matchMedia. That way DomWatcher can be configured to watch for changes across any/all breakpoints a site is designed around.

    const mediaQueries = new Map([
    [
    'desktop',
    window.matchMedia('(min-width: 1440px)')],
    [
    'laptop',
    window.matchMedia('(min-width: 1024px) and (max-width: 1439px)')],
    [
    'tablet',
    window.matchMedia('(min-width: 600px) and (max-width: 1023px)')],
    [
    'mobile',
    window.matchMedia('(max-width: 599px)')],
    ]);
    const watchBreakpoints = () => {
    Array.from(mediaQueries.entries())
    .forEach(([breakpoint, mediaQuery]) => {
    watcher.add({
    element: mediaQuery,
    on: 'change',
    callback: (e) => {
    if (e.matches) {
    // Handle the breakpoint
    }
    },
    });
    });
    }
    Index
    • Adds a watcher and immediately begins watching.

      Parameters

      • config: DomWatcherConfig

      Returns void

    • Removes a given watcher by id.

      Parameters

      • id: string

        The id of the watcher to remove.

      Returns void

    • Dry runs a callback by id. This just calls the callback and isn't the actual browser event so no event data will be available when using this method.

      watcher.add({
      element: element,
      callback: ()=> {
      console.log('yo');
      }
      on: 'click',
      id: 'yo'
      });

      watcher.run('yo');

      Parameters

      • id: string

      Returns void