npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@financial-times/o-viewport

v5.1.2

Published

Utility for attaching debounced listeners to resize, scroll, orientation and visibility events on window

Downloads

9,292

Readme

o-viewport MIT licensed

Utility for attaching debounced listeners to resize, scroll, orientation and visibility events on window.

Usage

Check out how to include Origami components in your project to get started with o-viewport.

Note: within the component's API and in the documentation below orientation and visibility are used instead of orientationchange or visibilitychange, but the actual browser event listened to is orientationchange or visibilitychange

Methods

o-viewport#listenTo(eventType)

Attaches a debounced/throttled (as appropriate) listener to events on window [resize, scroll, orientation, visibility or all] which in turn fires events within the oViewport namespace (see Events below).

Note: all will enable all o-viewport events.

import oViewport from '@financial-times/o-viewport';

// Fire for orientation events.
oViewport.listenTo('orientation');

// Listener for debounced orientation events via o-viewport.
document.body.addEventListener('oViewport.orientation', function(event) {
	console.log(event.type); // oViewport.orientation
	console.log(event.viewport); // { height, width }
	console.log(event.orientation); // 'portrait' or 'landscape'
	console.log(event.originalEvent); // the original browser event
});

See events for more examples.

o-viewport#stopListeningTo(eventType)

Remove the attached listener from the window for the named event [resize, scroll, orientation, visibility or all].

Note: all will disable all o-viewport events.

// Stop listening to the orientation event.
oViewport.stopListeningTo('orientation');

o-viewport#getOrientation()

Provides a reasonably reliable way (more so than window.orientation) of obtaining the current orientation of the viewport.

oViewport.getOrientation(); // 'portrait' or 'landscape'

o-viewport#getVisibility()

Provides a reasonably reliable way of obtaining the current visibility of the viewport.

oViewport.getVisibility(); // boolean, true if visible

o-viewport#getSize(ignoreScrollbars)

Provides a reliable way of obtaining the current dimensions of the browser window. Returns an object with the properties width and height.

By default or if no parameters are passed the method will return the size of the viewport inclusive of the scrollbars. However in certain cases (e.g. adverts) you may want to get the size of the viewport without the scroll bars. In such case pass true to the method in order to ignore the scrollbars.

oViewport.getSize(); // {width: 108, height: 100} including scrollbar width
oViewport.getSize(true); // {width: 100, height: 100} without scrollbars

o-viewport#getScrollPosition()

Provides a reliable way of obtaining the current scroll position of the viewport. returns an object with the properties width, height, left and top

oViewport.getScrollPosition(); // {width: 100, height: 100, left: 0, top: 10}

o-viewport#setThrottleInterval(eventType, interval) Product use only

Sets the debounce/throttle interval for a given event [scroll, resize or orientation]. As a shorthand, calling setThrottleInterval with 1 - 3 numbers will set the intervals for scroll, resize and orientation in that order e.g. setThrottleInterval(100, undefined, 300) is equivalent to:

setThrottleInterval('scroll', 100)
setThrottleInterval('resize') // which does nothing
setThrottleInterval('orientation', 300)
setThrottleInterval('visibility', 30)

The default value for each of these is 100ms

o-viewport#debug()

Turns on debug mode (logging event details to the console).

Events

Each of these custom events are fired on document.body. For each custom event event.detail.originalEvent contains a reference to the original browser event and event.detail.viewport the result of o-viewport#getSize(). For example:

import oViewport from '@financial-times/o-viewport';

// Fire for all viewport events.
oViewport.listenTo('all');

// Listener for debounced visibility events via o-viewport.
document.body.addEventListener('oViewport.visibility', function(event) {
	console.log(event.type); // oViewport.resize
	console.log(event.detail.viewport); // { height, width }
	console.log(event.detail.hidden); // boolean
});

Note event.detail.hidden is unique to the oViewport.visibility event. Additional unique properties for o-viewport events are detailed below.

oViewport.resize

  • No additional properties.

oViewport.orientation

  • data.orientation: 'portrait' or 'landscape'

oViewport.visibility

  • data.hidden: true or false

oViewport.scroll

  • data.scrollLeft: unitless px value of scroll position
  • data.scrollTop: unitless px value of scroll position
  • data.scrollHeight: unitless px value of scroll height
  • data.scrollWidth: unitless px value of scroll width

Throttling

  • oViewport.resize, oViewport.orientation and oViewport.visibility are debounced i.e. if the native event fires several times in quick succession the custom event will fire only once n milliseconds after the last event, where n is the throttle interval
  • oViewport.scroll is throttled i.e. if the native scroll event fires several times in quick succession the custom event will fire at most once every n milliseconds, where n is the throttle interval

Use the setThrottleInterval method to customise throttling.

Migration

State | Major Version | Last Minor Release | Migration guide | :---: | :---: | :---: | :---: ✨ active | 5 | N/A | migrate to v5 | ⚠ maintained | 4 | 4.0.5 | migrate to v4 | ╳ deprecated | 3 | 3.3 | migrate to v3 | ╳ deprecated | 2 | 2.3 | migrate to v2 | ╳ deprecated | 1 | 1.5 | N/A |


Licence

Copyright (c) 2016 Financial Times Ltd. All rights reserved.

This software is published under the MIT licence.