media-query-fns
Functions to read media queries from a string/ast and:
Based on media-query-parser which means:
Quickfire examples
import { compileQuery, matches, toEnglishString } from "media-query-fns";
const maxWidthQuery = compileQuery(`@media (max-width: 1200px)`);
const testEnv = (widthPx = 1280, heightPx = 800) => ({
widthPx,
heightPx,
deviceWidthPx: widthPx,
deviceHeightPx: heightPx,
dppx: 2,
});
console.log(matches(maxWidthQuery, testEnv(1280)));
console.log(matches(maxWidthQuery, testEnv(1000)));
const complexQuery = compileQuery(
`@media screen and (monochrome) and (orientation)`
);
console.log(matches(complexQuery, testEnv()));
console.log(toEnglishString(maxWidthQuery));
console.log(toEnglishString(complexQuery));
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
License
MIT