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

backbone.mq

v1.2.0

Published

Backbone plugin to detect and listen to media queries.

Downloads

6

Readme

backbone.mq

Backbone plugin to detect and listen to media queries.

Usage

Setup

Create a new MQ object to get started.

import MQ from 'backbone.mq'

const mq = new MQ()

Then add your media queries you would like to use.

mq.add('desktop', 'only screen and (min-width:60em)')

Or add multiple media queries.

mq.add({
	'desktop': 'screen and (min-width:60em)',
	'mobile': 'handheld or screen and (max-width:59.99em)'
})

Or add your media queries when you initialize your MQ object.

const mq = new MQ({
	'desktop': 'screen and (min-width:60em)',
	'mobile': 'handheld or screen and (max-width:59.99em)'
})

You can also remove media queries.

mq.remove('mobile')

Detection

At any point you can see if your media query matches.

const isDesktop = mq.matches('desktop') // -> boolean

if (isDesktop) {
	// Do some desktopy things
}

Alternatively this works too.

mq.matches('desktop', function() {
	// Also do some desktopy things
})

Events

You can also listen for when a media query becomes matched or unmatched. The MQ object extends Backbone.Events, so enjoy making the most of it.

Events which are namespaced by :match or :unmatch are only triggered if the media query is matched or unmatched respectively.

mq.on('desktop', function (media) {
	if (media.matches) {
		// Do some desktopy things
	} else {
		// Or not
	}
})

mq.once('mobile:match', function() {
	// I'm on a phone!
})

backboneThing.listenTo(mq, 'desktop:unmatch', function() {
	// Wait, we're not on a desktop any more? How did that happen?
})

mq.off('desktop')

Chainable

Most methods on MQ are chainable.

mq
.add({
	'desktop': 'screen and (min-width:60em)',
	'mobile': 'handheld or screen and (max-width:59.99em)'
})
.matches('desktop', function() { /* ... */ })
.once('mobile', function() { /* ... */ })
.remove('desktop')

Except mq.matches() with a single parameter, which returns a boolean :wink:

Fallback

When matchMedia or media queries isn't supported, MQ will fall back to a single media query. By default the first added media query will be used, but you're welcome to override it:

mq.fallback = 'mobile'; // Mobile first yo!

Compatibility

Backbone.MQ requires matchMedia to work properly, so you might need a polyfill for older browsers.

License

MIT - see license