Awesome Media Queries in JavaScript

2.1.6 · abandoned · verified Sun Apr 19

enquire.js is a lightweight, pure JavaScript library designed to programmatically respond to CSS media queries, enabling JavaScript logic to execute when media query conditions are met or unmet. It provides a clean API for registering handlers with `match`, `unmatch`, `setup`, and `destroy` callbacks, allowing developers to manage dynamic content or behavior based on responsive breakpoints. The library, currently at version 2.1.6, has no direct runtime dependencies, though it relies on the browser's native `window.matchMedia` API and would require a polyfill for environments that do not support it. Its primary differentiator was its small footprint and pure JavaScript approach without external dependencies, offering a robust solution for enhancing responsive web design beyond CSS alone. The project appears to be unmaintained, with its last significant update approximately nine years ago, indicating an abandoned status.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to register a media query handler using the global `enquire` object, reacting to viewport width changes with `match` and `unmatch` callbacks.

import 'enquire.js';

// Ensure enquire is available globally after import
if (typeof window !== 'undefined' && window.enquire) {
  window.enquire.register("screen and (max-width:1000px)", {

      match : function() {
          console.log("Media query (max-width:1000px) matched!");
          // Add your logic here when the query matches
      },

      unmatch : function() {
          console.log("Media query (max-width:1000px) unmatched!");
          // Add your logic here when the query unmatches
      },

      setup : function() {
          console.log("Handler setup once upon registration.");
      },

      destroy : function() {
          console.log("Handler destroyed and cleaned up.");
      },

      deferSetup : false
  });

  // Example of unregistering after some time (optional cleanup)
  // const myHandler = { /* ... */ };
  // window.enquire.register("query", myHandler);
  // setTimeout(() => {
  //   window.enquire.unregister("query", myHandler);
  // }, 5000);
} else {
  console.warn("enquire.js global object not found. Ensure it's correctly loaded.");
}

view raw JSON →