Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

js-media-query

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-media-query

CSS Media query in js

  • 1.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
27
decreased by-69.66%
Maintainers
1
Weekly downloads
 
Created
Source

CSS Media query in JS

npm npm

Installation

npm install js-media-query

Usage

import Media from "js-media-query";
import { up, sm, md } from "js-media-query";

See Examples

API

MethodDescription
queryCheck media query matching
minMinimum breakpoint width
maxMaximum breakpoint width
upMinimum breakpoint width
downMaximum breakpoint width
betweenCheck if screen between the from and to breakpoints
isTouchCheck if is touck device
isMouseCheck if is non-touch device
xsCheck if screen is bigger or equal to xs class defined breapoint
smCheck if screen is bigger or equal to sm class defined breapoint
mdCheck if screen is bigger or equal to md class defined breapoint
lgCheck if screen is bigger or equal to lg class defined breapoint
xlCheck if screen is bigger or equal to xl class defined breapoint
setBreakpointsSet Local class breapoints

Events

on(event, callback, media)
PropValueRequiredDescription
eventin , out , changedyesEvent type
callbackfunctionyesCallback function reference
mediafunctionyesFunction reference what return result of required media query
off(event, callback?, media?)
PropValueRequiredDescription
eventin , out , changedyesEvent type
callbackfunctionyesCallback function reference
mediafunctionyesFunction reference what return result of required media query

Examples

Basic Usage
import { min } from "js-media-query";

window.addEventListener("resize", () => {
  if (min(992)) {
    console.log("Screen size is equals or bigger than 992px");
  } else {
    console.log("Screen size is lower than 992px");
  }
});
Using with default breakpoints
import Media from "js-media-query";

if (Media.sm()) {
  // Your code
}
Using with custom breakpoints
import { Media } from "js-media-query";

const media = new Media({
  xs: 0,
  sm: 576,
  md: 768,
  lg: 992,
  xl: 1200,
});

if (media.md()) {
  // Execute code if screen size is bigger or equals to 768px
}
Event listening
import { Media } from "js-media-query";

const media = new Media();

const changedHandler = () => {
  console.log("changed");
};

const mediaFn = () => media.up(768);

// Add event listener
media.on("changed", changedHandler, mediaFn);

// Remove all event listeners of event type
media.off("changed");

// Remove event listener of event type
media.off("changed", changedHandler);

// Remove event listener of event type on with specific media function
media.off("changed", changedHandler, mediaFn);
@media (min-width: 768px) AND (max-width: 992px)
Media.between(768, 992); // true | false
@media (pointer: coarse)
Media.isTouch(); // true | false
@media (min-width: 992px)
Media.min(992); // true | false
@media (max-width: 991px)
Media.down(991); // true | false
@media (min-width: 1200px)
Media.xl(); // true | false

FAQs

Package last updated on 30 May 2020

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc