npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-spaniel

v0.7.9

Published

Ember addon wrapping spaniel and providing viewport and requestAnimationFrame utilities.

Downloads

17

Readme

ember-spaniel

Build Status npm version

Ember addon wrapping spaniel, a viewport tracking library, IntersectionObserver polyfill, and requestAnimationFrame task utility.

Compatibility

  • Ember.js v3.4 or above
  • Ember CLI v2.13 or above
  • Node.js v8 or above

Installation

ember install my-addon

Usage

Including this addon will add Spaniel to your application, available for direct use in the app.

import * as spaniel from 'spaniel';

The rest of the API is contained in a service.

viewport service API

You can set your ember-spaniel config in config/environment.js, there are 3 options defaultRootMargin, watcherTime and watcherRatio.

The viewport service will look for a defaultRootMargin object property on the application config. If not found, will default to 0, 0, 0, 0. The watcherTime and watcherRatio will be used as the config of watcher return by getWatcher().

// environment.js
module.exports = {
  ...
  'ember-spaniel': {
    watcherTime: 100,
    watcherRatio: 0.8,
    defaultRootMargin: {
      top: 100,
      bottom: 200
    }
  }
}

onInViewportOnce(el, callback, { context, rootMargin, ratio, root, ALLOW_CACHED_SCHEDULER }) => Function

Register a callback that will be called when the provided element first enters the viewport. Will get called on the next requestAnimationFrame if the element is already in the viewport. Returns a function that, when called, will cancel and clear the callback.

Optionally includes the ability to specify a custom root, which defaults to window. When passing a custom root, the common case would include handling state invalidation (referenced below invalidate()).

An optional flag ALLOW_CACHED_SCHEDULER which defaults to true. This feature flag when passed as false will disable caching of getBoundingClientRect on elements within the Spaniel#ElementScheduler.

export default Ember.Component.extend({
  viewport: Ember.inject.service(),
  didInsertElement() {
    let viewport = this.get('viewport');
    let el = this.get('element');
    this.clearViewportCallback = viewport.onInViewportOnce(el, () => {
      console.log('I am in the viewport');
    });
  },
  willDestroyElement() {
    this._super(...arguments);
    this.clearViewportCallback();
  }
});
export default Ember.Component.extend({
  viewport: Ember.inject.service(),
  didInsertElement() {
    let viewport = this.get('viewport');
    let fooChildElement = this.get('element');

    this.clearViewportCallback = viewport.onInViewportOnce(fooChildElement, () => {
      console.log('I am in the viewport');
    }, {
      root: fooCustomRoot,
      ALLOW_CACHED_SCHEDULER: false
    });
  },
  willDestroyElement() {
    this._super(...arguments);
    this.clearViewportCallback();
  }
});

isInViewport(el, { ratio, rootMargin } = {}) => Promise

Returns a promise that resolves if the element is in the viewport, otherwise rejects.

export default Ember.Component.extend({
  viewport: Ember.inject.service(),
  didInsertElement() {
    let viewport = this.get('viewport');
    let el = this.get('element');
    viewport.isInViewport(el).then(() => {
      console.log('In the viewport');
    }, () => {
      console.log('Not in the viewport');
    });
  }
});

getWatcher()

The service has a Watcher instance available for direct use.

export default Ember.Component.extend({
  viewport: Ember.inject.service(),
  didInsertElement() {
    let watcher = this.get('viewport').getWatcher();
    let el = this.get('element');
    watcher.watch(el, (e) => {
      console.log(`${e} happened`);
    });
  }
});

invalidate()

Triggers Spaniel#invalidate on the viewport to invalidate cached state. Due to negative performance implications, this method should not be abused and as such should handle edge case scenarios only, such as when leveraging a custom root. The recommended pattern below binds an event to viewports custom root, that when fired triggers the viewports invalidate() method. Optionally leveraging Ember's Util #debounce method for improved performance and/or https://github.com/ember-lifeline/ember-lifeline#addeventlistener.

export default Ember.Component.extend({
  viewport: Ember.inject.service(),
  didInsertElement() {
    let viewport = this.get('viewport');
    let el = this.get('element');
    viewport.isInViewport(el).then(() => {
      console.log('In the viewport');
    }, () => {
      console.log('Not in the viewport');
    });

    fooCustomRoot.addEventListener('foo-event', this.onFooMethod.bind(this), false);
  },
  onFooMethod() {
    let viewport = this.get('viewport');

    viewport.invalidate();
  },
  willDestroyElement() {
    this._super(...arguments);
    let viewport = this.get('viewport');

    fooCustomRoot.removeEventListener('foo-event', this.onFooMethod.bind(this), false);
  }
});

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.