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

@deleteagency/device-observer

v3.0.5

Published

When you style you components in CSS using media queries it's quite often when for a particular component CSS is just not powerful enough to fulfill you needs. Then you have to use JS for that purpose so you copy your breakpoint and use something like win

Downloads

559

Readme

Device Observer

When you style you components in CSS using media queries it's quite often when for a particular component CSS is just not powerful enough to fulfill you needs. Then you have to use JS for that purpose so you copy your breakpoint and use something like window.addEventListener('resize', ...) and rely on window.outerWidth/innerWidth.

Besides in some cases window.outerWidth/innerWidth can be inconsistent in different browsers (see here) so relying on them is not very safe, adding another addEventListener and "window.innerWidth > 1280" just makes your code less maintainable.

Device Observer allows you to configure your breakpoints once and then write media queries conditions in JS the same way you are used to in CSS It uses window.matchMedia that always returns correct results. Supports both mobile and desktop-first approaches (can be configured via options).

Live Demo

Installation

Use the package manager npm for installation.

$ npm install @deleteagency/device-observer

Usage

const devices = {
	'mobile': 0,
	'tablet': 768,
	'desktop': 1024,
}

const deviceObserver = new DeviceObserver(devices)

deviceObserver.subscribeOnResize(() => {
	// this will triggered not more than ones at 50ms by default
	console.log('Do some logic on every resize');
});

deviceObserver.subscribeOnChange((newDevice, oldDevice) => {

	if (deviceObserver.is('<', 'desktop')) {
		console.log('Do some logic for smaller devices');
	} else {
		console.log('Do some logic for bigger devices');
	}
});

API

constructor(devices, options = {})

Sets options described in Options section

options

Required Type: Object

resizeDebounce

Debounce time in milliseconds for handling window resize event.
Default value - 50. Type: number

mobileFirst

Whether to use mobileFirst approach in current device determining. Default value - true. Type: Boolean

subscribeOnResize(cb)

Subscribe on viewport changing with debounce time. Passed callback function will be called every time viewport is changes

cb

Required Type: (currentDevice) => void

subscribeOnChange(cb)

Subscribe on device change. Passed callback function will be called every time device is changes (in terms of user devices passed as the first argument to deviceObserver.init())

cb

Required Type: (newDevice, oldDevice) => void

is(operator, deviceName)

Returns Boolean

Just an alias for the above methods

operator

Required Type: string Allowed values: '=', '>', '>=', '<', '<='

deviceName

Required Type: string

License

MIT