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

[![npm version](https://img.shields.io/npm/v/media-query-fns.svg?style=flat-square)](https://www.npmjs.com/package/media-query-fns) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/media-query-fns?style=flat-square) [![test coverage](https://


Version published
Weekly downloads
1.5K
decreased by-47%
Maintainers
1
Install size
825 kB
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-4/
  • 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 (monochrome))

Quickfire examples

import { compileQuery, matches } from "media-query-fns";
const query = compileQuery(`@media (max-width: 1200px)`);
const env = (widthPx: number) => ({
  widthPx,
  heightPx: 800,
  dppx: 2,
  deviceWidthPx: 1280,
  deviceHeightPx: 800,
});
console.log(matches(query, env(1280) }));
// false
console.log(matches(query, env(900) }));
// true

Considerations & Caveats

This library:

  • converts units to a base unit (px for lengths, dppx for resolution, hz for frequency)
    • there's currently no way to pass in how special units like rem and vw should be converted to px
  • doesn't support calc - following the spec
    • many browsers do support calc, but they probably shouldn't - not just because of the spec but because it opens up a Pandora's box of nasty edge cases

Finally, this behavior is currently not supported in matches:

If a media feature references a concept which does not exist on the device where the UA is running (for example, speech UAs do not have a concept of “width”), the media feature must always evaluate to false.

Installation

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

License

MIT

FAQs

Last updated on 21 Mar 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