An ES6 module for emitting custom events when the browser window crosses predefined thresholds. Kinda like media queries for JavaScript.
boundaryEvents
is an event emitter, dispatching the event named cross
at user-defined thresholds. You will need to initiate an event listener to handle incoming events.
import boundaryEvents from './boundaryEvents.js';
const boundary = boundaryEvents();
boundary.registerBoundary('id',500);
window.addEventListener('cross',(evt)=>{
console.log(evt);
});
boundaryEvents
accepts the following config options as an object literal as the sole parameter. All properties are optional.
// Defaults defined in the example below.
boundaryEvents({
parent : window,
eventName : 'cross',
eventNameReturn : 'cross',
initOnCall : true,
bubbles : true,
throttle : 66
})
Name | Type | Required | Description |
---|---|---|---|
parent | DOM Node | The DOM element boundaryEvents dispatches events from. |
|
eventName | String | The name of the custom event when the viewpoint is sized beyond the boundary threshold. | |
eventNameReturn | String | The name of the custom event fired when the viewport is is sized back below the boundary threshold. Defaults to the same event name, as there are properties in the event payload to help discern direction. | |
initOnCall | Boolean | Optionally prevent the resize listener from initiating on call. You can manually start the listener via child methods. | |
bubbles | Boolean | Set whether the boundary event bubbles up the DOM or not. | |
throttle | Number | Customize the length of time, in millisecods, between resize calls. No need to spam the event handler. |
boundaryEvents
features five methods for registering boundaries and controlling event listeners.
Add a new boundary to the object store. Returns true
.
Name | Type | Required | Description |
---|---|---|---|
id | String | √ | User-defined key name. Passed back on event call. |
boundary | Number | √ | Pixel-width of boundary line. Passed back on event call. |
isHeight | Boolean | Set to true if listening for window height resize. |
|
data | Any | Accepts any number of artibary parameters. Returned on event call as an array. |
const boundary = boundaryEvents();
boundary.registerBoundary('breakpointOne', 960, false, {optionalData: 'abc'})
Deletes an existing entry from the roster. Returns true
;
Name | Type | Required | Description |
---|---|---|---|
id | String | √ | User-defined key name. Must match id in registerBoundary . |
boundary.removeBoundary('breakpointOne');
Adds a resize
event listener to window
. Will not add redundant listeners. Takes no params.
boundary.listenStart();
Removes resize
listener from window, if listener exists. Takes no params.
boundary.listenEnd();
Reports whether there is an active listener or not. Takes no params. Returns Boolean.
boundary.isListening();
Takes no params. Returns an object literal {height: [Number], width: [Number]}
of the current page dimensions.
import { getDimensions } from './boundaryEvents';
getDimensions();
boundaryEvents
creates an object store, fed by user-supplied keys. On a throttled resize event, the script cycles through the stored object, and compares the current window size to the registered boundaries. If a boundary is crossed, the script emits an event with the relevant data, including the key and any passed arguments, in the payload.
made with ❤️ and ☕️ by
Matthew Smith @niftinessafoot