@csstools/media-query-list-parser
TypeScript icon, indicating that this package has built-in type declarations

4.0.2 • Public • Published

Media Query List Parser

npm version Build Status Discord

Implemented from : https://www.w3.org/TR/mediaqueries-5/

Usage

Add Media Query List Parser to your project:

npm install @csstools/media-query-list-parser @csstools/css-parser-algorithms @csstools/css-tokenizer --save-dev

Media Query List Parser depends on our CSS tokenizer and parser algorithms. It must be used together with @csstools/css-tokenizer and @csstools/css-parser-algorithms.

import { parse } from '@csstools/media-query-list-parser';

export function parseCustomMedia() {
	const mediaQueryList = parse('screen and (min-width: 300px), (50px < height < 30vw)');

	mediaQueryList.forEach((mediaQuery) => {
		mediaQuery.walk((entry, index) => {
			// Index of the current Node in `parent`.
			console.log(index);
			// Type of `parent`.
			console.log(entry.parent.type);

			// Type of `node`
			{
				// Sometimes nodes can be arrays.
				if (Array.isArray(entry.node)) {
					entry.node.forEach((item) => {
						console.log(item.type);
					});
				}

				if ('type' in entry.node) {
					console.log(entry.node.type);
				}
			}

			// stringified version of the current node.
			console.log(entry.node.toString());

			// Return `false` to stop the walker.
			return false;
		});
	});
}

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.0.22,176,585latest

Version History

VersionDownloads (Last 7 Days)Published
4.0.22,176,585
4.0.132,645
4.0.012,397
3.0.1556,748
3.0.039,329
2.1.13952,673
2.1.1216,027
2.1.11173,159
2.1.1076
2.1.9134,718
2.1.873,763
2.1.7206,890
2.1.617,144
2.1.5145,321
2.1.474,442
2.1.324,656
2.1.253,328
2.1.120,690
2.1.010,391
2.0.428,382
2.0.331
2.0.213,250
2.0.169,309
2.0.0712
1.0.01,178

Package Sidebar

Install

npm i @csstools/media-query-list-parser

Weekly Downloads

3,896,086

Version

4.0.2

License

MIT

Unpacked Size

113 kB

Total Files

7

Last publish

Collaborators

  • romainmenke
  • alaguna
  • jonathantneal