Skip to content
Ember addon for using spaniel
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
addon
app
config
tests
vendor
.editorconfig
.ember-cli
.eslintrc.js
.gitignore
.jshintrc
.npmignore
.travis.yml
.watchmanconfig
CHANGELOG.md
LICENSE.md
README.md
ember-cli-build.js
index.js
package.json
prepublish.js
testem.js
yarn.lock

README.md

ember-spaniel Build Status npm version

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

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

import spaniel from 'spaniel';

The rest of the API is contained in a service.

viewport service API

The viewport service will look for a defaultRootMargin object property on the application config. If not found, will default to 0, 0, 0, 0.

// environment.js
module.exports = {
  ...
  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);
  }
});

Requirements

  • Ember 2.x.x is required. Ember 2.16-LTS and up is officially supported.
  • Node 8+

Installation

  • git clone https://github.com/asakusuma/ember-spaniel.git this repository
  • cd ember-spaniel
  • yarn install

Linting

  • yarn lint:js
  • yarn lint:js -- --fix

Running

Running Tests

  • yarn test (Runs ember try:each to test your addon against multiple Ember versions)
  • ember test – Runs the test suite on the current Ember version
  • ember test --server – Runs the test suite in "watch mode"

Building

  • ember build

For more information on using ember-cli, visit http://ember-cli.com/.

You can’t perform that action at this time.