# media-query-fns

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

Latest version **2.1.2** (published 2025-08-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install media-query-fns
pnpm add media-query-fns
yarn add media-query-fns
bun add media-query-fns
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2025-08-23 |
| First published | 2022-03-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=16.0.0 \|\| ^14.13.1 |
| Dependencies | 1 |
| Unpacked size | 65.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 12 |
| Author | Tom Golden |
| Maintainers | tbjgolden |
| Keywords | media, query, functions, tools, parser, compiler, token, typescript |

## Links

- npm: https://www.npmjs.com/package/media-query-fns
- Repository: https://github.com/tbjgolden/media-query-fns
- Issues: https://github.com/tbjgolden/media-query-fns/issues
- npm.io page: https://npm.io/package/media-query-fns

## Dependencies (1)

- [media-query-parser](https://npm.io/package/media-query-parser.md) 3.0.0-beta.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.1.2 (latest) — 2025-08-23
- 2.1.1 — 2025-08-20
- 2.1.0 — 2025-06-11
- 2.0.0 — 2023-06-13
- 1.4.0 — 2022-12-04
- 1.3.2 — 2022-03-28
- 1.3.1 — 2022-03-27
- 1.3.0 — 2022-03-22
- 1.2.0 — 2022-03-22
- 1.1.0 — 2022-03-21
- 1.0.0 — 2022-03-21

## README

# `media-query-fns`

![npm](https://img.shields.io/npm/v/media-query-fns)
![npm type definitions](https://img.shields.io/npm/types/media-query-fns)
![license](https://img.shields.io/npm/l/media-query-fns)
![npm downloads](https://img.shields.io/npm/dw/media-query-fns)
[![install size](https://packagephobia.com/badge?p=media-query-fns)](https://packagephobia.com/result?p=media-query-fns)

![](screen.gif)

Cool functions for media queries. [**Spec-compliant**](https://www.w3.org/TR/mediaqueries-5).

---

## Install

This package is available from the `npm` registry.

```sh
npm install media-query-fns
```

## Usage

Supports JavaScript + TypeScript:

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

// returns data that can be used to interpret the query
const maxWidthQuery = compileQuery(`(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(`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
```

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

## 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

---
_Source: https://npm.io/package/media-query-fns · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
