npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-mediacheck

v0.1.7

Published

`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 spiri

Downloads

40

Readme

ng-mediacheck

ng-mediacheck provides a service that adds media query event listeners to your Angular 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, but has been revamped and greatly simplified for a more modern Angular implementation.

Installation

npm install ng-mediacheck --save

Setup

Add the module and service to your Angular app:

// src/app/app.module.ts
import { NgMediacheckModule } from 'ng-mediacheck';
import { MediacheckService } from 'ng-mediacheck';

import { AppComponent } from './app.component';

@NgModule({
  imports: [
    NgMediacheckModule.forRoot()
  ],
  providers: [
    MediacheckService
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

How it Works

Check out the service source code here: mediacheck.service.ts.

Methods

The following methods are provided by MediacheckService:

setQueries(customMqs)

Out of the box, the service currently provides two simple media queries. They are defined in the service like so:

mqueries = {
  small: '(max-width: 767px)',
  large: '(min-width: 768px)'
};

You 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:

  customMqs = {
    mobile: '(max-width: 480px)',
    tablet: '(min-width: 481px) and (max-width: 768px)',
    desktop: '(min-width: 769px)'
  };

  constructor(private mediacheck: MediacheckService) {
    this.mediacheck.setQueries(this.customMqs);
  }

initSubject()

This method initializes a subject: 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.

This can be done like so:

  constructor(private mediacheck: MediacheckService) {
    this.mediacheck.initSubject();
  }

  ngOnInit() {
    this.mediacheck.mq$.subscribe(mq => {
      console.log('current mq:', mq);
    });
  }

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.

check(mqName)

check(mqName) expects a string parameter with the name of the media query you'd like to match, e.g., small, large, etc.

  • This is a shortcut for window.matchMedia('mediaquerystring').matches.
  • It will return true if the media query currently matches and false if it doesn't.
  • It will output a warning if it can't find a media query registered with the mqName provided.

getMqName()

getMqName() returns the string key for the currently active media query, e.g., small, large, etc.

onMqChange(mqName, callback)

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.

  • This method adds a MediaQueryList listener with the callback parameter.
  • On media query change, it executes the callback function and passes the MediaQueryList parameter so your components can utilize it.
  • It implements zones for Angular change detection.