README.md

ember-in-viewport

This Ember addon adds a simple, highly performant Ember Mixin to your app. This Mixin, when added to a Component, will allow you to check if that Component has entered the browser's viewport. By default, the Mixin uses the IntersectionObserver API if it detects it in your user's browser – failing which, it falls back to using requestAnimationFrame, then if not available, the Ember run loop and event listeners.

We utilize pooling techniques to reuse Intersection Observers and rAF observers in order to make your app as performant as possible and do as little works as possible.

didScroll(up,down,left,right)

The didScroll hook fires when an element enters the viewport. For example, if you scrolled down in order to move the element in the viewport, the didScroll hook would fire and also receive the direction as a string. You can then handle it like another hook as in the above example.

Set to false to have no listeners registered. Useful if you have components that function with either viewport listening on or off.

viewportUseIntersectionObserver: boolean

Default: Depends on browser

Read-only

The Mixin by default will use the IntersectionObserver API. If IntersectionObserver is not supported in the target browser, ember-in-viewport will fallback to rAF. We prevent users from explicitly setting this to true as browsers lacking support for IntersectionObserver will throw an error.

A single number or array of numbers between 0.0 and 1.0. A value of 0.0 means the target will be visible when the first pixel enters the viewport. A value of 1.0 means the entire target must be visible to fire the didEnterViewport hook.
Similarily, [0, .25, .5, .75, 1] will fire didEnterViewport every 25% of the target that is visible.
(https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#Thresholds)

Some notes:

If the target is offscreen, you will get a notification via didExitViewport that the target is initially offscreen. Similarily, this is possible to notify if onscreen when your site loads.

To get around the above issue and have didExitViewport fire, set your intersectionThreshold to [0, 1.0]. When set to just 1.0, when the element is 99% visible and still has isIntersecting as true, when the element leaves the viewport, the element isn't applicable to the observer anymore, so the callback isn't called again.

If your intersectionThreshold is set to 0 you will get notified if the target didEnterViewport and didExitViewport at the appropriate time.

scrollableArea

Default: null

A CSS selector for the scrollable area. e.g. ".my-list"

viewportUseRAF: boolean

Default: Depends on browser

As its name suggests, if this is true and the IntersectionObserver API is not available in the target browser, the Mixin will use requestAnimationFrame. Unless you want to force enabling or disabling this, you won't need to override this option.

viewportSpy: boolean

Default: false

When true, the Mixin will continually watch the Component and re-fire hooks whenever it enters or leaves the viewport. Because this is expensive, this behaviour is opt-in. When false, the Mixin will only watch the Component until it enters the viewport once, and then it sets viewportEntered to true (permanently), and unbinds listeners. This reduces the load on the Ember run loop and your application.

NOTE: If using IntersectionObserver (default), viewportSpy wont put too much of a tax on your application. However, for browsers (Safari) that don't currently support IntersectionObserver, we fallback to rAF. Depending on your use case, the default of false may be acceptable.

viewportDidScroll: boolean

Default: true

When true, the Mixin enables listening to the didScroll hook. This will become by default false in a future major release

viewportScrollSensitivity: number

Default: 1

This value determines the degree of sensitivity (in px) in which a DOM element is considered to have scrolled into the viewport. For example, if you set viewportScrollSensitivity to 10, the didScroll{...} hooks would only fire if the scroll was greater than 10px. Only applicable if IntersectionObserver and rAF are not applied.

viewportRefreshRate: number

Default: 100

If IntersectionObserver and requestAnimationFrame is not present, this value determines how often the Mixin checks your component to determine whether or not it has entered or left the viewport. The lower this number, the more often it checks, and the more load is placed on your application. Generally, you'll want this value between 100 to 300, which is about the range at which people consider things to be "real-time".

This value also affects how often the Mixin checks scroll direction.

viewportTolerance: object

Default: { top: 0, left: 0, bottom: 0, right: 0 }

This option determines how accurately the Component needs to be within the viewport for it to be considered as entered. Add bottom margin to preemptively trigger didEnterViewport.

For IntersectionObserver, this property interpolates to rootMargin.
For rAF, this property will use bottom tolerance and measure against the height of the container to determine when to trigger didEnterViewport.

Also, if your sentinel (component that uses this mixin) is a zero-height element, ensure that the sentinel actually is able to enter the viewport.

Global options

You can set application wide defaults for ember-in-viewport in your app (they are still manually overridable inside of a Component). To set new defaults, just add a config object to config/environment.js, like so:

Modifiers

Note - This is in lieu of a did-enter-viewport modifier, which we plan on adding in the future. Compared to the solution below, did-enter-viewport won't need a container (this) passed to it. But for now, to start using modifiers, this is the easy path.