evented-viewport
v0.0.1
Published
Viewport width and height utility for JavaScript modules.
Downloads
2
Readme
evented-viewport
Viewport width and height utility for JavaScript modules.
Install
$ npm i evented-viewport --save
Usage
Pass evented-viewport
an options object with debounce
and emitter
properties:
import knot from 'knot.js'
import viewport from 'evented-viewport'
// have your emitter handy (create or import one)
const emitter = knot()
// (optionally) subscribe to the resize event
// note that all handlers will be passed an object containing the viewport width/height
emitter
.on('resize', viewport => console.log(`width: ${ viewport.width }`))
.on('resize', viewport => console.log(`height: ${ viewport.height }`))
// define your options
const options = {
debounce: 250, // time, in ms, that must pass before updating the viewport width/height on resize
emitter // object with an "emit" method
}
// create an instance (should be a singleton)
const instance = viewport(options)
// export (and enjoy in other modules)
export default instance
See Also
- knot.js - A browser-based event emitter, for tying things together.
License
MIT. © 2017 Michael Cavalea