Socket
Socket
Sign inDemoInstall

media-query-fns

Package Overview
Dependencies
3
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    media-query-fns

Useful functions for working with CSS media queries in JS/TS


Version published
Weekly downloads
1.6K
decreased by-19.68%
Maintainers
1
Install size
1.30 MB
Created
Weekly downloads
 

Readme

Source

media-query-fns

npm version npm bundle size test coverage GitHub Workflow Status

Functions to read media queries from a string/ast and:

  • Check if a media query is true for a custom environment
  • Converts a media query to a human friendly description

Based on media-query-parser which means:

  • parses any correct CSS media queries
  • spec-compliant everything - https://www.w3.org/TR/mediaqueries-5/ (as of 2022-12-04)
  • TypeScript friendly
  • all valid queries parsed and interpreted, even newer syntax like @media (100px < width < 200px) or complex ones like @media not screen and ((not (update: none)) and (prefers-reduced-motion: reduce))

Quickfire examples

import { compileQuery, matches, toEnglishString } from "media-query-fns";

// returns data that can be used to interpret the query
const maxWidthQuery = compileQuery(`@media (max-width: 1200px)`);
// (throws if invalid query syntax)

const testEnv = (widthPx = 1280, heightPx = 800) => ({
  widthPx,
  heightPx,
  deviceWidthPx: widthPx,
  deviceHeightPx: heightPx,
  dppx: 2,
});
console.log(matches(maxWidthQuery, testEnv(1280))); // false
console.log(matches(maxWidthQuery, testEnv(1000))); // true

const complexQuery = compileQuery(
  `@media screen and (monochrome) and (orientation)`
);
console.log(matches(complexQuery, testEnv()));
// true

console.log(toEnglishString(maxWidthQuery));
// 'if width ≤ 1200px'
console.log(toEnglishString(complexQuery));
// 'if (is screen AND monochrome)'
// note: (orientation) without "landscape" or "portrait" is always true, so it's removed for brevity

Considerations & Caveats

This library doesn't support calc because it follows the spec.

Many browsers do support calc to some extent, but they probably shouldn't as it opens up a Pandora's box of nasty edge cases that would lead to browser inconsistencies (without a spec to unify them, anyway).

Installation

npm install media-query-fns --save
# yarn add media-query-fns

License

MIT

FAQs

Last updated on 04 Dec 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc