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

@squirrel-forge/mediaquery-events

v0.8.2

Published

Use css media queries as javascript event names.

Downloads

3

Readme

@squirrel-forge/mediaquery-events

Use css media queries as javascript event names. Simply don't worry about matching css media queries with custom javascript code, just bind a listener with the query as the event name and be done, the actual module, depending on how you use or implement it will be ~5kb in size.

Installation

npm i @squirrel-forge/mediaquery-events

Technical background and compatibility

Under the hood this uses window.matchMedia, MediaQueryListEvent and CustomEvent, it is, therefore only compatible with the more recent browsers, see details on caniuse MediaQueryListEvent if the module would work for you.

There is a ponyFill available for browsers, for example: Safari < 14, that do not allow MediaQueryListEvent objects to be constructed manually, but it must be included explicitly and is not included in the precompiled version.

Polyfilling the missing parts is possible, please feel free to add a polyfill setup to make it compatible with older browsers such as IE11. The big desktop and mobile browsers such as Edge, Chrome, Opera, Firefox all support the required abilities of the api. There are currently no explicit plans to supply any complete polyfill for browsers that do not support all required features beyond the optional ponyFill for the MediaQueryListEvent constructor.


Recommended usage:

Using es10 modules

Import the required class and or ponyFill from the module, you have full control.

import { MediaQueryEvents, MediaQueryListEventPonyFill } from '@squirrel-forge/mediaquery-events';

Create an instance with your preferred options.

const media = new MediaQueryEvents( {
    
    // Default target is the window object
    //  target can be anything that provides the basic event methods:
    //  addEventListener, removeEventListener, dispatchEvent
    target : ( window | document | HTMLElement | Object ),
    
    // By default the ponyfill is null and not available
    //  if set it will be used instead when the MediaQueryListEvent constructor cannot be called natively. 
    ponyfill : ( null | MediaQueryListEventPonyFill ),
    
    // By default debug is null
    //  if set to console, it will supply error and event related information for debugging.
    debug : ( null | console ),
} );

Now you can start binding events.


Legacy usage:

Using the precompiled and minified webpack build

Import or include a precompiled webpack version that attempts to bind to window.media and window.mqe as long as these are previously undefined.

import '@squirrel-forge/mediaquery-events/build/mediaquery-events.min.js';

Or:

<script src=".../@squirrel-forge/mediaquery-events/build/mediaquery-events.min.js"

The media binding can be overridden by setting your own names as following:

window.mqe_OVERRIDE = [ 'reference' ];

The window.mqe binding is always made unless defined and therefore blocked before loading the module. Now you can start binding events.


Binding and using media query events

for es10 and legacy

Define your event handlers.

/**
 * Event handler 
 * @private
 * @param {CustomEvent} event - Mediaquery custom event
 * @return {void}
 */
const _event_handler = ( event ) => {

    // The event name the listener was bound to
    //  if this is equal to event.detail.query this is not a global event
    const listener = event.type;

    // The media query string that is subject to this event
    const query = event.detail.query;

    // The query state
    //  note this can be overridden when manually using MediaQueryEvents.dispatchEvent() to trigger a media query event
    const matches = event.detail.event.matches;
    
    // Do something with this information
    console.log( { listener, query, matches } );
};

For example, bind your handlers to queries that cover mobile, tablet and desktop:

media.addEventListener( '(max-width: 767px)', _event_handler );
media.addEventListener( '(min-width: 768px) and (max-width: 1024px)', _event_handler );
media.addEventListener( '(min-width: 1025px)', _event_handler );

You may also bind global events, these will fire for any query that was previously bound or required and changes it's state.

media.addEventListener( 'media.on', _event_handler );
media.addEventListener( 'media.off', _event_handler );
media.addEventListener( 'media.change', _event_handler );

Note, that these only fire when at least one actual media query was bound or required.

All events can be bound to the given event target even before initializing the MediaQueryEvents Object:

window.addEventListener( 'media.change', _event_handler );
window.addEventListener( '(max-width: 375px)', _event_handler );

but it is important to note that the events will only fire if the query is bound or required explicitly at some point:

media.addEventListener( '(max-width: 375px)', _event_handler );

// OR

media.requireQueries( [ '(max-width: 375px)' ] );

If you do not wish to bind the media queries directly and want to use the global events only, you can initialize multiple required queries easily without binding any handlers.

// Require an Array of queries
media.requireQueries( [
    '(max-width: 767px)',
    '(min-width: 768px) and (max-width: 1024px)',
    '(min-width: 1025px)',
] );

Using the MediaQueryEvents.matches() method returns the queries matching state. It will create the given media query like the requireQueries method if it does not exist.

// Check a queries state manually
if ( media.matches( '(max-width: 767px)' ) ) {
    // Do something
}

Check the example for working code demo.js source file.


Class definitions

MediaQueryEvents

MediaQueryEvents class - Supplies media query event functionality.

Class overview

class MediaQueryEvents {
    constructor( { target = window, ponyfill = null, debug = null } ) {}
    target : window|document|HTMLElement|Object // Read only
    debug : null|console // Settable Object
    matches( query ) {} // Boolean|Null
    requireQueries( queries ) {} // MediaQueryEvents
    dispatchEvent( query, matches = null ) {} // MediaQueryEvents
    addEventListener( query, callback, useCapture = false, dontFire = false ) {} // MediaQueryEvents
    removeEventListener( query, callback, useCapture = false ) {} // MediaQueryEvents
}

For more details check the MediaQueryEvents source file.

Media query events

  • {css media query} - Fired whenever a query changes its state and matches.

Global events

  • media.on - Fired once when a query goes active.
  • media.off - Fired once when a query goes inactive.
  • media.change - Fires in both cases described above.

MediaQueryListEventPonyFill

MediaQueryListEventPonyFill class - PonyFill class that provides a replacement object for when MediaQueryListEvent cannot be constructed.

Class overview

class MediaQueryListEventPonyFill {
    constructor( media, matches ) {}
    media : string // read only
    matches : boolean // ready only
}

For more details check the MediaQueryListEventPonyFill source file.


Issues

If you encounter any issues, please report here.


Check the sourcecode on github for detailed comments.