Security News
Highlights from the 2024 Rails Community Survey
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
media-observer
Advanced tools
Javascript observable (RxJS) resize (event listener) heavily inspired by Angular Flex Layout Media Observer
This is basically the same Media Observer you can find into @angular/flex, with an additional method like the one you
can find in @angular/cdk, in order to check custom media queries (such as portrait
, or simply media queries not
declared initially. The difference is that this is for javascript/typescript use (e.g. React maybe).
I think that it's very useful to avoid listening multiple times for window resizes and centralize responsive behaviours (when CSS media queries aren't enough, of course).
In Angular you can inject the instance, so this version is set for working as a singleton. You won't need to create an
instance calling new MediaObserverService()
(although in non-typescript projects you may be able to do so). You can
access the singleton instance using the static getter INSTANCE
.
E.g.:
import {MediaObserverService} from "media-observer";
const mosInstance = MediaObserverService.INSTANCE;
The MediaObserverService will provide mediaQuery activations notifications for all registered breakpoints.
This service is essentially an Observable that exposes both features to subscribe to mediaQuery changes and a validator
method .isActive()
to test if a mediaQuery (or alias) is currently active.
Only mediaChange activations (not deactivations) are announced by the ObservableMedia!
The class MediaObserverService service has three (3) APIs:
asObservable(): Observable<MediaChange>
isActive(query: string | string[]): boolean
isMatched(query: string | string[]): boolean
This notifies when the current breakpoint is changed and returns the list of all active media queries
mosInstance.asObservable()
.pipe(filter((changes: MediaChanges) => changes[0].mqAlias === BreakpointAliases.xs))
.subscribe(() => this.loadMobileContent());
This allows to check if one of the registered media queries is active, by alias or by media query
if (mosInstance.isActive(BreakpointAliases.xs)) {
this.loadMobileContent();
}
This is basically identical to isActive
, except it allows to check any media query, even if not registered.
So you can use the method to evaluate one or more media queries against the current viewport size.
const PRINT_MOBILE = 'print and (max-width: 599px)';
if (mosInstance.isMatched(PRINT_MOBILE)) {
this.doSomePrintStuff();
}
Default breakpoints and aliases are accessible through the following vars:
DEFAULT_BREAKPOINT_QUERIES
: links aliases and media queriesDEFAULT_BREAKPOINTS
: the default breakpoint items that will be watched as soon as MediaObserverService is instantiatedBreakpointAliases
: contains all the standard aliases (you can use your own if you prefer)To change the default breakpoints and use your own, just call the static method MediaObserverService.setBreakpoints
,
passing the list (an Array of BreakPoint
) of your breakpoints, **before anyone calls MediaObserverService.INSTANCE
.
To change the breakpoints after initialization you can call MediaObserverService.INSTANCE.destroy
, which will destroy the active instance.
FAQs
Javascript observable (RxJS) resize (event listener) heavily inspired by Angular Flex Layout Media Observer
The npm package media-observer receives a total of 4 weekly downloads. As such, media-observer popularity was classified as not popular.
We found that media-observer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A record 2,709 developers participated in the 2024 Ruby on Rails Community Survey, revealing key tools, practices, and trends shaping the Rails ecosystem.
Security News
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.