![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
ember-spaniel
Advanced tools
Ember addon wrapping spaniel and providing viewport and requestAnimationFrame utilities.
Ember addon wrapping spaniel, a viewport tracking library, IntersectionObserver polyfill, and requestAnimationFrame
task utility.
ember install my-addon
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 APIYou 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);
}
});
See the Contributing guide for details.
This project is licensed under the MIT License.
FAQs
Ember addon wrapping spaniel and providing viewport and requestAnimationFrame utilities.
The npm package ember-spaniel receives a total of 0 weekly downloads. As such, ember-spaniel popularity was classified as not popular.
We found that ember-spaniel demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.