{"id":26339681,"url":"https://github.com/kmaida/ng-mediacheck","last_synced_at":"2026-05-10T10:18:36.486Z","repository":{"id":57310701,"uuid":"117474522","full_name":"kmaida/ng-mediacheck","owner":"kmaida","description":"NPM package providing a service for working with mediaqueries in Angular","archived":false,"fork":false,"pushed_at":"2018-03-01T23:22:07.000Z","size":78,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-19T15:07:46.248Z","etag":null,"topics":["angular","matchmedia","mediaqueries","npm","npm-package"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kmaida.png","metadata":{"files":{"readme":"README.MD","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-14T23:06:38.000Z","updated_at":"2019-09-13T04:28:57.000Z","dependencies_parsed_at":"2022-08-26T06:20:33.493Z","dependency_job_id":null,"html_url":"https://github.com/kmaida/ng-mediacheck","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/kmaida%2Fng-mediacheck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmaida%2Fng-mediacheck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmaida%2Fng-mediacheck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kmaida%2Fng-mediacheck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kmaida","download_url":"https://codeload.github.com/kmaida/ng-mediacheck/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243818414,"owners_count":20352665,"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":["angular","matchmedia","mediaqueries","npm","npm-package"],"created_at":"2025-03-16T03:18:48.468Z","updated_at":"2026-05-10T10:18:31.424Z","avatar_url":"https://github.com/kmaida.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ng-mediacheck\n\n`ng-mediacheck` provides a service that **adds media query event listeners to your [Angular](https://angular.io) application**. It can be used to manipulate component properties, templates, and behavior when matching different media queries. It is a spiritual successor to Angular v1.x [angularjs-mediaCheck](https://github.com/kmaida/angularjs-mediaCheck), but has been revamped and greatly simplified for a more modern Angular implementation.\n\n## Installation\n\n```bash\nnpm install ng-mediacheck --save\n```\n\n## Setup\n\nAdd the module and service to your Angular app:\n\n```js\n// src/app/app.module.ts\nimport { NgMediacheckModule } from 'ng-mediacheck';\nimport { MediacheckService } from 'ng-mediacheck';\n\nimport { AppComponent } from './app.component';\n\n@NgModule({\n  imports: [\n    NgMediacheckModule.forRoot()\n  ],\n  providers: [\n    MediacheckService\n  ],\n  bootstrap: [AppComponent]\n})\nexport class AppModule { }\n```\n\n## How it Works\n\nCheck out the service source code here: [`mediacheck.service.ts`](https://github.com/kmaida/ng-mediacheck/blob/master/src/mediacheck.service.ts).\n\n### Methods\n\nThe following methods are provided by `MediacheckService`:\n\n#### setQueries(customMqs)\n\nOut of the box, the service currently provides two simple media queries. They are defined in the service like so:\n\n```\nmqueries = {\n  small: '(max-width: 767px)',\n  large: '(min-width: 768px)'\n};\n```\n\nYou may, of course, provide your own different breakpoints in your app that the service will then use. You can do so by passing them to the `setQueries(customMqsObj)` method like so:\n\n```\n  customMqs = {\n    mobile: '(max-width: 480px)',\n    tablet: '(min-width: 481px) and (max-width: 768px)',\n    desktop: '(min-width: 769px)'\n  };\n\n  constructor(private mediacheck: MediacheckService) {\n    this.mediacheck.setQueries(this.customMqs);\n  }\n```\n\n#### initSubject()\n\nThis method initializes a [subject](https://medium.com/@benlesh/on-the-subject-of-subjects-in-rxjs-2b08b7198b93): `mq$`. This subject provides a _stream_ that emits a value whenever the browser's media query changes. If you wish to use subscriptions to execute functionality when the breakpoint changes, then run the `initSubject()` method in your component's constructor and then subscribe to the `mq$` subject that is subsequently created.\n\nThis can be done like so:\n\n```\n  constructor(private mediacheck: MediacheckService) {\n    this.mediacheck.initSubject();\n  }\n\n  ngOnInit() {\n    this.mediacheck.mq$.subscribe(mq =\u003e {\n      console.log('current mq:', mq);\n    });\n  }\n```\n\n\u003e **Note:** If you want to use your own custom media queries, you must pass them to the `setQueries(customMqsObj)` method _before_ calling `initSubject()`. If you do not, the subject will initialize using the default media queries defined in MediacheckService.\n\n#### check(mqName) \n\n`check(mqName)` expects a `string` parameter with the name of the media query you'd like to match, e.g., `small`, `large`, etc. \n\n* This is a shortcut for `window.matchMedia('mediaquerystring').matches`.\n* It will return `true` if the media query currently matches and `false` if it doesn't.\n* It will output a warning if it can't find a media query registered with the `mqName` provided.\n\n#### getMqName()\n\n`getMqName()` returns the string key for the currently active media query, e.g., `small`, `large`, etc.\n\n#### onMqChange(mqName, callback)\n\n`onMqChange(mqName, callback)` expects a `string` parameter with the name of the media query you'd like to match, e.g., `small`, `large`, etc. It also expects a callback `function`. This function will execute when the media query activates.\n\n* This method [adds a MediaQueryList listener](https://msdn.microsoft.com/library/hh772467.aspx) with the `callback` parameter.\n* On media query change, it executes the callback function and passes the [`MediaQueryList`](https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList) parameter so your components can utilize it.\n* It implements [zones](http://blog.thoughtram.io/angular/2016/02/01/zones-in-angular-2.html) for Angular change detection.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmaida%2Fng-mediacheck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkmaida%2Fng-mediacheck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkmaida%2Fng-mediacheck/lists"}