css-mediaquery 0.1.0

Parse CSS media queries and test them against a context (width, resolution, orientation, …) — a faithful port of the css-mediaquery npm package. Zero dependencies.
Documentation
  • Coverage
  • 100%
    32 out of 32 items documented4 out of 24 items with examples
  • Size
  • Source code size: 42.4 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 558.0 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/css-mediaquery
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

css-mediaquery

Crates.io Documentation CI License

Parse CSS media queries and match them against a context — a headless matchMedia for servers and tooling. A faithful Rust port of the css-mediaquery npm package (used by react-responsive, styled-components SSR, and friends). Zero dependencies.

use css_mediaquery::{matches, Context};

let ctx = Context::new().media_type("screen").width(640.0);
assert!(matches("screen and (min-width: 600px)", &ctx));
assert!(!matches("screen and (min-width: 800px)", &ctx));

// units convert just like css-mediaquery: 30em = 480px, 2dppx = 192dpi
assert!(matches("(min-width: 30em)", &Context::new().width(481.0)));
assert!(matches("(min-resolution: 2dppx)", &Context::new().resolution(192.0)));

You can also inspect the parsed AST:

let q = css_mediaquery::parse("screen and (min-width: 600px)");
assert_eq!(q[0].media_type, "screen");
assert_eq!(q[0].expressions[0].feature, "width");

Why css-mediaquery?

Server-side rendering, static analysis, and headless tooling all need to evaluate @media queries without a browser. Rust had full CSS parsers but no small, focused "does this query match this context?" — so this ports the canonical JS one, verified against it across thousands of realistic (query, context) pairs.

[dependencies]
css-mediaquery = "0.1"

API

Item Purpose
parse(query) Parse into Vec<Query> (one per comma)
matches(query, &context) Whether the query matches the context
Query::matches(&context) Match a single parsed query
Context::new()… Builder: media_type, width, height, resolution, orientation, aspect_ratio, color, …

Behavior

This mirrors css-mediaquery's semantics, including its quirks:

  • Lengths convert to px (em = rem = 16px, in = 96px, cm = 96/2.54px, …); resolutions to dpi (dppx = ×96, dpcm = /2.54); ratios to decimals.
  • A feature matches only when the context supplies a non-zero value for it. Orientation and aspect-ratio are not derived from width/height — set them explicitly.
  • The media type is matched, and not quits early when the type matches (so not all and (…) is always false), mirroring the reference implementation.
  • , is OR; within a query, all expressions must hold (AND).

On well-formed queries it agrees with css-mediaquery exactly. It intentionally differs only on input that never appears in real stylesheets: numeric values use Rust parsing rather than JavaScript's Number/parseInt coercions (no Infinity, hexadecimal, or integers beyond i64), and malformed parentheses degrade gracefully instead of throwing.

License

Licensed under either of Apache-2.0 or MIT at your option.