npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

media-query-parser

v3.0.2

Published

Parse CSS media queries (spec-compliant)

Downloads

2,914,923

Readme

media-query-parser

npm npm type definitions license npm downloads install size

  • Create a JS object from a CSS media queries
  • Create a CSS media query from a JS object
  • Returns a ParserError for invalid CSS media queries
  • Spec-compliant - https://www.w3.org/TR/mediaqueries-5/
    • All valid queries parsed
      e.g. (100px < width < 200px)
  • Zero-dependencies
  • Well tested - every single line
  • TypeScript friendly

demo
Try it!

Why?

Other CSS parsers (e.g. css-tree and postcss) do not support all media query syntax out of the box.

Further, the only other media query parser that I'm aware of is postcss-media-query-parser - which is specific to postcss and doesn't parse newer syntax like range expressions (i.e. (width >= 768px)).

This package is a spec-compliant media query parser that can be used in Node/Deno/etc, or on the client that precisely matches the spec right down to the quirks.

These are valid media queries that this library supports:

@media (768px <= width < 1200px);
@media only print and (color);
@media not (not (not (((hover) or ((not (color)))))));
@media (🐈: 😸 /* if cat happy */) {
  /* this query has valid syntax, but is clearly not a real feature 😿 */
  /* For extensions to this project, check out "Libraries that use this" at the bottom of this README */
}

These are invalid media queries that this library will detect:

@media (color) or (hover); /* or cannot be at top level */
@media (min-width: calc(50vw + 10px)); /* functions aren't valid values */
@media not((color)); /* whitespace must follow `not` */
@media (768px < = width < 1200px); /* cannot have a space between `<` and `=` */

Install

This package is available from the npm registry.

npm install media-query-parser

Usage

Supports JavaScript + TypeScript:

import { parseMediaQuery } from "media-query-parser";

const mediaQuery = parseMediaQuery("screen and (min-width: 768px)");
if (!isParserError(mediaQuery)) {
  console.log(mediaQuery);
  // {
  //   _t: "query",
  //   type: "screen",
  //   condition: {
  //     _t: "condition",
  //     op: "and",
  //     nodes: [
  //       {
  //         _t: "in-parens",
  //         node: {
  //           _t: "feature",
  //           context: "value",
  //           feature: "min-width",
  //           value: {
  //             _t: "dimension",
  //             value: 768,
  //             unit: "px",
  //           },
  //         },
  //       },
  //     ],
  //   },
  // }
  // // start/end omitted for brevity

  console.log(stringify(mediaQuery.condition));
  // "(min-width: 768px)"
}

Can also be imported via require("media-query-parser").

v3 (Current) Docs

v2 API docs

Libraries that use this

  • (See GitHub for dynamically updated list)

Node versions

This source code of this library supports node >=6.5.0 via require() and node >=16.0.0 || ^14.13.1 via import. (Below v6.5.0 is possible but you'd need to transpile the CommonJS code from ES6 yourself)

Contributing

  • PRs welcome and accepted, simply fork and create
  • Issues also very welcome
  • Treat others with common courtesy and respect 🤝

Dev environment (for contributing) requires:

  • node >= 16.14.0
  • npm >= 6.8.0
  • git >= 2.11

Licence

MIT