New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

media-trigger

Package Overview
Dependencies
Maintainers
0
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

media-trigger

MediaTrigger is designed to control the response to changes in media queries, allowing you to perform specific actions when entering or exiting a particular media state, or any change to a media state.

latest
Source
npmnpm
Version
4.3.0
Version published
Maintainers
0
Created
Source

media-trigger

MediaTrigger is designed to control the response to changes in media queries, allowing you to perform specific actions when entering or exiting a particular media state, or any change to a media state.

npm GitHub package version NPM Downloads

0.5kB gzipped

Demo


Install

$ yarn add media-trigger

Import

import MediaTrigger from 'media-trigger';

Usage

const mediaTrigger = new MediaTrigger({
  media: '(min-width: 992px)',
  entry: (mq) => {
    console.log('Entered media query:', mq.media);
  },
  exit: (mq) => {
    console.log('Exited media query:', mq.media);
  },
  change: (mq) => {
    console.log('Media query change:', mq.media, 'matches:', mq.matches);
  }
});

mediaTrigger.init();

Options

OptionTypeDefaultDescription
mediastringRequiredThe media query string to be observed.
entryfunction | nullnullCallback function invoked when the media query starts matching (matches becomes true).
exitfunction | nullnullCallback function invoked when the media query stops matching (matches becomes false).
changefunction | nullnullCallback function invoked on any change in the media query's matching state.

Methods

MethodDescription
init()Initializes the media query listener and triggers the initial state callbacks.
destroy()Removes the media query listener to stop observing changes.

Example

// Initialize a MediaTrigger instance
const mediaTrigger = new MediaTrigger({
  media: '(max-width: 768px)',
  entry: (mq) => {
    console.log('Screen is now less than or equal to 768px wide.');
  },
  exit: (mq) => {
    console.log('Screen is now wider than 768px.');
  }
});

// Start listening to media query changes
mediaTrigger.init();

// Later, if you want to stop listening
mediaTrigger.destroy();

Browser Support

MediaTrigger relies on the window.matchMedia API, which is supported in all modern browsers. If window.matchMedia is not available, MediaTrigger will safely do nothing.


License

media-trigger is released under MIT license

Keywords

media

FAQs

Package last updated on 10 Mar 2025

Did you know?

Socket

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.

Install

Related posts