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

wiggle.js

v1.3.1

Published

Media Queries Change Listener

Downloads

19

Readme

wiggle

NPM Package

Small wrapper around matchMedia to easily react on changes in page layout. Define your own layout breakpoints and attach listeners to them.


  /**
  * In order to use wiggle we need to initialize it first.
  * Listeners are attached on wiggle instance.
  **/
  Wiggle.init([{
    name:String // Need to be unique per the instance. It can be any string that is valid object property name.
    minWidth:String|Number // If number wiggle assumes measurement unit is in px. If string any valid CSS measurement unit can be defined instead of px (like em).
    minWidth:String|Number
    mediaQuery:String // Raw media query. item can have ether minWidth, maxWidth combination or rawQuery but not both.
  }])

  // Real world example

  var screen = Wiggle.init([{
      // Screen width >= 992px is defined as a desktop.
      name: 'desktop', // The name is arbitrary and can be anything e.g 'large-screen'
      minWidth: 992 // same as writing '992px' because number default to px unit
    }, {
      name: 'desktop-menu',
      minWidth: '820px'
    }, {
      // Tablet have overlaps with menu-breakpoint.
      name: 'tablet',
      minWidth: 768,
      maxWidth: 991
    }, {
      name: 'mobile',
      maxWidth: '62em' // Any valid CSS measurement unit can be used
    }]);

  // We are subscribing to names defined during initialization of wiggle.
  screen.on('mobile', function() {
    console.log('Called if screen size is mobile and ever time after screen size changes to mobile');
  });

  // Difference between .on and .on.change listener is that .on listener will be triggered
  // immediately when defined if condition is meet while on.change will be triggered only after change happen.
  screen.on.change('mobile', function() {
    console.log('Called every time after screen size changes to mobile');
  });

  screen.off('tablet', function() {
    console.log('Called if screen size is not tablet and ever time after screen size stops being tablet');
  });

  if(screen.is('desktop')) {
    console.log('Current screen size is desktop');
  }

  if(screen.is('tablet') && screen.is('desktop-menu')) {
    console.log('Based on configuration multiple screens can be active.')
  }

  // Multiple instances of wiggle can be created
  var orientation = Wiggle.init([{
    name: 'portrait',
    mediaQuery: 'orientation: portrait' // Raw media query
  }, {
    name: 'landscape',
    mediaQuery: 'orientation: landscape'
  }]);

  orientation.on('portrait', function() {
    console.log('Called if screen is in portrait mode and every time screen changes to portrait mode');
  });


  // Unsubscribe

  // In order to unsubscribe we have to have reference to function used to subscribe to event.
  function landscapeListener() {
    console.log('Listener will be active until we unsubscribe from it');
  }

  orientation.on('landscape', landscapeListener);

  onSomeEvent(function() {
    // Remove landscapeListener subscription.
    orientation.unsubscribe(landscapeListener);
  });

Supported browsers

Wiggle is using matchMedia to detect layout changes. matchMedia is not supported in IE9 and below.