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

@yoozly/ngrx-mediaqueries

v0.0.8

Published

Angular Lib that binds CSS media queries and @ngrx/store together.

Downloads

13

Readme

NgrxMediaqueries npm version

A lib for Angular that binds media-queries and @ngrx/store together. The idea behind the creation of the library cames from a talk of Michael Madsen at Rxjs Live called "Reactive Responsive Design" (Source).

Possible uses cases are :

  • display / hide a component or enable a functionnality according to a specific media query
  • compose selectors with media queries to make the UI more responsive (ie: if the screen width is lower than 400px AND the menu is open, then put the content on two columns)

By default, the following

Installation

Npm : npm i @yoozly/ngrx-mediaqueries

Yarn : yarn add @yoozly/ngrx-mediaqueries

Setup - Root Module

Add the reducer

In the root module of your application, add the reducer ngrxMediaQueriesReducer(). In order to merge ngrxMediaQueriesReducer() in the map of root reducers, use MERGE_REDUCERS injection token, like below :

import {
  MERGE_REDUCERS,
  ngrxMediaQueriesReducer
} from "@yoozly/ngrx-mediaqueries";

@NgModule({
  StoreModule.forRoot(MERGE_REDUCERS, {
    metaReducers: [someMetaReducers]
  }),
  providers: [
    {
      provide: MERGE_REDUCERS,
      useFactory: (): ActionReducerMap<any> => ({
        ...reducers,
        ...ngrxMediaQueriesReducer()
      })
    }
  ],
})
export class RootModule {}

Initialize the library

Use APP_INITIALIZER token to start the library automatically.

import { APP_INITIALIZER } from "@angular/core";
import { MediaQueriesService } from "@yoozly/ngrx-mediaqueries";

@NgModule({
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: (init: MediaQueriesService) => _ => init,
      deps: [MediaQueriesService],
      multi: true
    }
  ]
})
export class RootModule {}

Include the module

Add NgrxMediaqueriesModule in the array of imports.

import { NgrxMediaqueriesModule } from "@yoozly/ngrx-mediaqueries";

@NgModule({
  imports: [NgrxMediaqueriesModule]
})
export class RootModule {}

By default, the following media queries are watched :

  • xxsmall: "(min-width:375px)"
  • xsmall: "(min-width:480px)"
  • small: "(min-width:600px)"
  • medium: "(min-width:768px)"
  • large: "(min-width:1024px)"
  • xlarge: "(min-width:1280px)"
  • xxlarge: "(min-width:1440px)"
  • portrait: "(orientation: portrait)"
  • landscape: "(orientation: landscape)"
  • speech: "speech"
  • touchscreen: "(hover: none) and (pointer: coarse)"
  • highres: "(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)"

You can override that behavior and add your own set of media queries by using the NgrxMediaqueriesModule.forRoot({...}) and by passing a configuration object.

import { NgrxMediaqueriesModule } from "@yoozly/ngrx-mediaqueries";

@NgModule({
  imports: [
    NgrxMediaqueriesModule.forRoot({
      small: "(min-width:480px)",
      medium: "(min-width:720px)",
      large: "(min-width:1024px)"
    })
  ]
})
export class RootModule {}

Here is the full configuration :

import { APP_INITIALIZER } from "@angular/core";
import {
  MERGE_REDUCERS,
  ngrxMediaQueriesReducer,MediaQueriesService,NgrxMediaqueriesModule
} from "@yoozly/ngrx-mediaqueries";

@NgModule({
  imports: [NgrxMediaqueriesModule],
  StoreModule.forRoot(MERGE_REDUCERS, {
    metaReducers: [someMetaReducers]
  }),
  providers: [
    {
      provide: MERGE_REDUCERS,
      useFactory: (): ActionReducerMap<any> => ({
        ...reducers,
        ...ngrxMediaQueriesReducer()
      })
    },
    {
      provide: APP_INITIALIZER,
      useFactory: (init: MediaQueriesService) => _ => init,
      deps: [MediaQueriesService],
      multi: true
    }
  ],

API

Selectors

A set of selectors is provided by default. In order to use it, use the NgrxMediaQueriesFacade service, like that :

import { NgrxMediaQueriesFacade } from "@yoozly/ngrx-mediaqueries";
constructor(
  private mqService: NgrxMediaQueriesFacade
) {}

By default, the following selectors are available :

  • getMediaQueryStateXXSmall();
  • getMediaQueryStateXSmall();
  • getMediaQueryStateSmall();
  • getMediaQueryStateMedium();
  • getMediaQueryStateLarge();
  • getMediaQueryStateXLarge();
  • getMediaQueryStateXXLarge();
  • getMediaQueryStatePortrait();
  • getMediaQueryStateLandscape();
  • getMediaQueryStateSpeech();
  • getMediaQueryStateTouchscreen();
  • getMediaQueryStateHighres();

If you have a custom configuration, there is a special selector that accepts the name of the media query as a param :

// in the root module
NgrxMediaqueriesModule.forRoot({
  small: "(min-width:480px)",
  medium: "(min-width:720px)",
  large: "(min-width:1024px)"
});

// then in a component
this.mqService.getMediaQueryState("small");
this.mqService.getMediaQueryState("medium");
this.mqService.getMediaQueryState("large");