backbone.mq
Backbone plugin to detect and listen to media queries.
Usage
Setup
Create a new MQ object to get started.
const mq =
Then add your media queries you would like to use.
mq
Or add multiple media queries.
mq
Or add your media queries when you initialize your MQ object.
const mq = 'desktop': 'screen and (min-width:60em)' 'mobile': 'handheld or screen and (max-width:59.99em)'
You can also remove media queries.
mq
Detection
At any point you can see if your media query matches.
const isDesktop = mq // -> boolean if isDesktop // Do some desktopy things
Alternatively this works too.
mq
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 mq backboneThing mq
Chainable
Most methods on MQ are chainable.
mq
Except mq.matches()
with a single parameter, which returns a boolean 😉
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:
mqfallback = '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