matchmediaquery

0.4.2 · abandoned · verified Sun Apr 19

matchmediaquery is an open-source JavaScript library providing isomorphic media query functionality, allowing developers to check CSS media queries both in browser environments and server-side contexts. Originally forked from the `matchmedia` project due to its maintainer's publishing delays, this library aimed to provide an active alternative. However, its last stable version (0.4.2) was published in late 2015, and the project has seen no significant updates or maintenance since. As a result, it is considered abandoned and may not be suitable for modern web development, lacking support for contemporary JavaScript module systems (ESM), TypeScript definitions, or up-to-date media query specifications. Its primary differentiator was its isomorphic capability, which is now often handled by more modern, actively maintained libraries or frameworks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and use `matchmediaquery` to evaluate media queries, including server-side simulation of viewport properties.

const matchMediaQuery = require('matchmediaquery');

// Example 1: Basic media query check (client-side/browser context)
// In a browser, this will use window.matchMedia. On the server,
// it simulates based on provided or default properties.
const isSmallScreen = matchMediaQuery('(max-width: 600px)');
console.log(`Is small screen (browser context): ${isSmallScreen}`);

// Example 2: Server-side simulation with specific viewport properties
// This allows you to test media queries on the server without a DOM.
const simulateMobile = matchMediaQuery('(min-width: 320px) and (max-width: 768px)', {
  type: 'screen',
  width: 375, // Simulate iPhone width
  height: 667,
  devicePixelRatio: 2
});
console.log(`Is mobile (server simulated): ${simulateMobile}`);

// Example 3: Another server-side check for a larger desktop screen
const simulateDesktop = matchMediaQuery('(min-width: 1024px)', {
  type: 'screen',
  width: 1280,
  height: 800,
  devicePixelRatio: 1
});
console.log(`Is desktop (server simulated): ${simulateDesktop}`);

view raw JSON →