Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

spaniel

linkedin293Apache-2.02.10.2TypeScript support: included

LinkedIn's viewport tracking library

readme

Spaniel Build Status npm version

LinkedIn's JavaScript viewport tracking library and IntersectionObserver polyfill. Track what the user actually sees.

import { IntersectionObserver } from 'spaniel';

new IntersectionObserver((entries) => { console.log('I see you') }, {
  threshold: 0.5
}).observe(document.getElementById('my-element'));

Practical uses included:

  • Determining advertisement impressions
  • Impression discounting feedback for relevance systems
  • Occlusion culling - Don't render an object until user is close to scrolling the object into the viewport

Usage and API Docs

Spaniel provides additional abstractions on top of IntersectionObserver, provides APIs for hooking into the low-level internals, and has some limitations as a non-complete polyfill. Learn more by reading the Usage and API Docs.

Why use Spaniel?

  • Provides the future-proofing of a WCIG API, but with an expanded feature-set built upon said API.
  • Tested and iterated upon in production by LinkedIn since late 2014
  • Highly performant, only relies on requestAnimationFrame
  • Extensive requestAnmiationFrame task/utility API

How is it tested?

Spaniel has both unit tests and a headless test suite. The headless tests are run using Nightmare.

How big is Spaniel?

Checkout size.txt to see the current minified UMD gzipped size.

You can also run npm run stats to measure locally.

Installation

Spaniel is a standard NPM/CommonJS module. You can use a build tool like browserify or webpack to include Spaniel in your application.

If you're using rollup, an ES6 version is built at /exports/es6/spaniel.js (as noted by jsnext:main in package.json).

Alternatively, running npm run build will generate a UMD file at /exports/spaniel.js, and a minified UMD file at /exports/min/spaniel.js. You can use the minified file in production.

Development setup

The Spaniel source code is written in TypeScript.

You will need testem installed globally to run the tests.

npm install -g testem

You will also need to install phantom.js globally.

// Install dependencies
npm install

// Run build
npm run build

// Watch and auto-rebuild
npm run watch

// Serve test app
npm run serve

// Run the tests
npm run test

IntersectionObserver Resources

Copyright 2017 LinkedIn Corp. All rights reserved.

changelog

spaniel Changelog

2.9.0 (August 19, 2021)

  • Add the threshold field to SpanielObserverEntry so that entries denote parameters of the threshold that was crossed

2.8.0 (August 18, 2021)

  • Allow specifying the SpanielObserver payload type via a generic, instead of the any type

2.7.0 (November 17, 2020)

  • Expose intersectionRect in watcher callback metadata parameter
  • Expose visibleTime in watcher callback metadata parameter for all event types, not just impression-complete

2.6.1 (October 19, 2020)

  • Add feature flag (USE_NATIVE_IO) to use native intersection observer when available.

2.5.2 (October 9, 2018)

  • Backport PR #59. Guard against race condition.
  • Travis config changes to test against release branches
  • Trigger closing events on beforeunload instead of unload as beforeunload can be too late in some situations

2.5.1 (June 28, 2018)

  • Suppress Rollup AOT Warnings

2.5.0 (May 29, 2018)

  • Fix Security Fix #82. Nightmare (Electron) Security Fix Bump.
  • Fix PR #80. Backporting the custom root API. New memoization for CPU and redraw perf improvements.

2.4.7 (February 12, 2018)

  • Fix scheduler now handles window proxy listeners state properly.
  • Fix Bug #76. Watcher support for elements not within the DOM.
  • Fix Bug #75. IE 11 may throw an exception when calling getBoundingClientRect on detached elements.

2.4.6 (January 8, 2018)

  • Fix performance issue with CPU on idle, scroll and resize.
  • Updated dependencies
  • Add pkg.module

2.4.0 (August 22, 2017)

  • Add boundingClientRect parameter to Watcher callback.

2.3.0 (August 4, 2017)

  • Add SpanielObserver.destroy() to cleanup memory.
  • Add Watcher.destroy() to cleanup memory.

2.2.0 (March 1, 2017)

  • Better TypeScript support
    • typings is now defined in package.json
    • Use Element instead of SpanielTrackedElement for public APIs
  • Publish TypeDoc API docs
  • Various bug fixes

2.1.0 (November 17, 2016)

Add api for one-time determining an element's viewport state

  • elementSatisfiesRatio()
  • queryElement()

2.0.0 (November 11, 2016)

Breaking changes

  • Remove default time value for Watcher constructor option. impressed and impression-complete will no longer fire without time option.
  • Remove default ratio value for Watcher constructor option. visible will no longer fire without ratio option.

1.1.0 (November 8, 2016)

Add public utility API

  • scheduleRead()
  • scheduleWork()
  • on()
  • setGlobalEngine()

1.0.0 (September 29, 2016)

Initial open source release