{"id":19653002,"url":"https://github.com/csstools/js-custom-media","last_synced_at":"2025-04-28T17:31:10.027Z","repository":{"id":57282980,"uuid":"152092999","full_name":"csstools/js-custom-media","owner":"csstools","description":"Use Custom Media Queries in JS","archived":false,"fork":false,"pushed_at":"2019-05-13T11:52:06.000Z","size":10,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-05-21T18:31:59.320Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://csstools.github.io/js-custom-media","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc0-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/csstools.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-08T14:23:03.000Z","updated_at":"2023-05-19T11:41:57.000Z","dependencies_parsed_at":"2022-09-17T13:52:02.784Z","dependency_job_id":null,"html_url":"https://github.com/csstools/js-custom-media","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csstools%2Fjs-custom-media","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csstools%2Fjs-custom-media/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csstools%2Fjs-custom-media/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/csstools%2Fjs-custom-media/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/csstools","download_url":"https://codeload.github.com/csstools/js-custom-media/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222505607,"owners_count":16994762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-11T15:12:52.985Z","updated_at":"2024-11-11T15:12:53.856Z","avatar_url":"https://github.com/csstools.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JS Custom Media [\u003cimg src=\"http://jonathantneal.github.io/js-logo.svg\" alt=\"\" width=\"90\" height=\"90\" align=\"right\"\u003e][JS Custom Media]\n\n[![NPM Version][npm-img]][npm-url]\n[![Build Status][cli-img]][cli-url]\n[![Support Chat][git-img]][git-url]\n\n[JS Custom Media] lets you use Custom Media Queries in JS, following the\n[CSS Media Queries] specification.\n\n```bash\nnpm install js-custom-media\n```\n\n## Usage\n\n```js\nimport { customMedia, polyfillMatchMedia } from 'js-custom-media';\n\n// define a custom media query\ncustomMedia.set('--md', '(width \u003e 640px)');\n\n// polyfill `window.matchMedia`\nconst matchMedia = polyfillMatchMedia(window.matchMedia);\n\n// match a custom media query\nmq = matchMedia('(--md)');\nmq.addListener(update);\n\n// apply the custom media query\nfunction update() {\n  // do something with `mq.matches`\n}\n\nupdate();\n```\n\nA typical implementation of this script is about 576 bytes minified and gzipped.\n\n### Usage with PostCSS Custom Media\n\n[PostCSS Custom Media] lets you import Custom Media from external files. These\nfiles can be used by this library as well.\n\n```js\nimport { extensions, polyfillMatchMedia } from 'js-custom-media';\n\n// get variables that include `'custom-media'` (or `customMedia`)\nimport variables from 'path/to/variables.json';\n\n// add variables’ custom media to `extensions`\nObject.assign(extensions, variables['custom-media']);\n\n// get built-in support for variables’ custom media\nwindow.matchMedia = polyfillMatchMedia(window.matchMedia);\n```\n\n## Features\n\n### polyfillMatchMedia\n\nThe `polyfillMatchMedia` method returns a wrapped version of `matchMedia` that\ncan parse custom media queries.\n\n```js\nimport { polyfillMatchMedia } from 'js-custom-media';\n\nconst matchMedia = polyfillMatchMedia(window.matchMedia); // works like window.matchMedia\n```\n\n### customMedia\n\nThe `customMedia` object includes 2 methods for getting and setting custom\nmedia queries.\n\n#### set\n\nThe `set` method adds a custom media query that is readable by the wrapped\nversion of `matchMedia`.\n\n```js\nimport { customMedia } from 'js-custom-media';\n\ncustomMedia.set('--md', '(min-width: 640px)');\n```\n\n#### get\n\nThe `get` method returns the value of a custom media query.\n\n```js\nimport { customMedia } from 'js-custom-media';\n\nCSS.customMedia.get('--md'); // (min-width: 640px)\n```\n\n### extensions\n\nThe `extensions` object is the internal map of custom media queries used by\n[customMedia](#custommedia) and [polyfillMatchMedia](#polyfillmatchmedia).\n\nThis can only be used for non-standard behavior, such as\n[Usage with PostCSS Custom Media](#usage-with-postcss-custom-media).\n\n```js\nimport { extensions } from 'js-custom-media';\n\nextensions['--some-mq'] = '(width \u003e= 960px)'; // add custom media queries in a non-standard way\n```\n\n[cli-img]: https://img.shields.io/travis/csstools/js-custom-media.svg\n[cli-url]: https://travis-ci.org/csstools/js-custom-media\n[git-img]: https://img.shields.io/badge/support-chat-blue.svg\n[git-url]: https://gitter.im/postcss/postcss\n[npm-img]: https://img.shields.io/npm/v/js-custom-media.svg\n[npm-url]: https://www.npmjs.com/package/js-custom-media\n\n[CSS Media Queries]: https://drafts.csswg.org/mediaqueries-5/#custom-mq\n[JS Custom Media]: https://github.com/csstools/js-custom-media\n[PostCSS Custom Media]: https://github.com/postcss/postcss-custom-media/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsstools%2Fjs-custom-media","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcsstools%2Fjs-custom-media","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcsstools%2Fjs-custom-media/lists"}