Awesome Media Queries in JavaScript

Overview

enquire.js - Awesome Media Queries in JavaScript

Build Status

enquire.js is a lightweight, pure javascript library (with no dependencies) for programmatically responding to media queries.

Getting enquire.js

Download

Get the latest build, ready to go:

Install via Bower

To install via bower, enter the following at the command line:

bower install enquire

Install via npm

To install via npm, enter the following at the command line:

npm install enquire.js

Build From Source

If you want build from source (and run all unit tests etc):

git clone git://github.com/WickyNilliams/enquire.js.git
cd enquire.js
npm install
grunt

Booya!

Quick Start

The main method you will be dealing with is register. It's basic signature is as follows:

enquire.register(query /* string */, handler /* object || array  || function */);

query is the CSS media query you wish to respond to, and handler is an object containing any logic to handle the query. An example of usage is as follows:

enquire.register("screen and (max-width:1000px)", {

    match : function() {},      // OPTIONAL
                                // If supplied, triggered when the media query transitions
                                // *from an unmatched to a matched state*

    unmatch : function() {},    // OPTIONAL
                                // If supplied, triggered when the media query transitions
                                // *from a matched state to an unmatched state*.
                                // Also may be called when handler is unregistered (if destroy is not available)

    setup : function() {},      // OPTIONAL
                                // If supplied, triggered once immediately upon registration of the handler

    destroy : function() {},    // OPTIONAL
                                // If supplied, triggered when handler is unregistered. Place cleanup code here

    deferSetup : true           // OPTIONAL, defaults to false
                                // If set to true, defers execution the setup function
                                // until the media query is first matched. still triggered just once
});

This should be enough to get you going, but please read the full enquire.js documentation if you wish to learn about the other cool features.

Contributing

  • Got an awesome idea?
  • Found a not-so-awesome bug?
  • Wish to get my attention through an inappropriate communication channel?!

Then please don't hesitate to raise an issue, they will all be looked at and tended to.

And for all the cool cats who are prepared to give their time to contribute code, feel free to open a pull request. If you could write unit tests to accompany your pull request that would be pretty sweet, but no worries if not - if it's good enough to be merged in, it's good enough for me to spend a little time to write tests on your behalf :-)

License

License: MIT (http://www.opensource.org/licenses/mit-license.php)

Comments
  • Removing a function/plugin on unmatch

    Removing a function/plugin on unmatch

    First off: great little library!

    I'm struggling with something which is probably really simple to fix.

    I build Mobile first so I only want the JS for non-mobile viewports to be loaded conditionally, I've got that part working fine:

    var BPpalm = 'screen and (min-width: 40em)';
    
    enquire.register(BPpalm, [{
    
        deferSetup: true,
    
        setup: function setup() {
            var conditionalScript = '/js/conditionally-loaded.js';
            $.getScript(conditionalScript, function() {
                $('.js-menu-accessible-ddown').accessibleDropDownMenu();
            }, true);
        },
    
        match: function() {
            $('body').css('background', 'gold');
        },
    
        unmatch: function() {
            $('body').css('background', 'plum');
        }
    
    }]).listen();
    

    What I'm struggling with is that I need to kill the function: accessibleDropDownMenu() for mobile i.e. viewports under 40em which would be the unmatch. Any ideas?

    opened by chris-pearce 21
  • Wrap enquire.js to allow its consumption as an AMD module.

    Wrap enquire.js to allow its consumption as an AMD module.

    We wanted to be able to use enquire.js internally in our all-AMD environment, so I wrote this little patch to conditionally wrap enquire as an AMD module using the UMDjs pattern

    It currently uses grunt-rigger for in-place replacement of source files in the AMD version (https://github.com/buildjs/rigger / https://github.com/DamonOehlman/grunt-rigger) and outputs to dist/amd/. To be honest I only really used this to avoid touching the core source in order to make maintaining our fork easier.

    The UMD template output works both as a global and as an AMD module, so if you were willing to accept a version that updated the default to use this pattern let me know and I'll submit a pull request that does that instead.

    opened by tomdavies 19
  • unregister method, or multiple instances or similar

    unregister method, or multiple instances or similar

    I'm currently looking at integrating enquire into a Backbone app, but want to allow separate views to control their own media queries... To do so successfully, I need a way to unregister the queries as a view is disposed of.

    Would it be possible to add something like this, and/or is it already do-able in the current API and I'm just not seeing it?

    Thanks-

    enhancement 
    opened by davidkaneda 17
  • Unmatch not firing in Chrome on Nexus 7

    Unmatch not firing in Chrome on Nexus 7

    Hi,

    I've been using enquie a bit and have just been testing it on a couple of tablets but have noticed that match and unmatch events dont seem to fire when using Chrome on the Nexus 7.

    I have a fairly simple test page with the following enqire registrations:

    enquire.register("screen and (orientation: portrait)", { match: function () { document.write("
    match portrait"); }, unmatch: function () { document.write("
    unmatch portrait"); } });

    enquire.register("screen and (orientation: landscape)", { match: function () { document.write("
    match landscape"); }, unmatch: function () { console.log("
    unmatch landscape"); } });

    that simply writes out matches and unmatches as you change the orientation. This seems to work fine on an iPad and when using Firefox on the Nexus 7.

    When using Chrome the first match seems to happen when the page loads but after that neither the unmatch or the match of the other orientation seems to fire.

    CSS media queries seem to be working fine .

    Device info: Nexus 7 on Android 4.2.2 Chrome 18.0.1025469

    opened by davinderkainth 16
  • enquire with mediaQuery polyfill causes ie8 to auto close selects

    enquire with mediaQuery polyfill causes ie8 to auto close selects

    when i use enquire register in my ie8 with matchMedia polyfill and i have a