Socket
Book a DemoInstallSign in
Socket

reactive-media-queries

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reactive-media-queries

Simple and declarative media queries triggers. Written in Typescript. Define breakpoints and get notified when it changes.

0.1.0
latest
npmnpm
Version published
Weekly downloads
52
-53.15%
Maintainers
1
Weekly downloads
 
Created
Source

reactive-media-queries

Simple and declarative media queries triggers. Written in Typescript. Define breakpoints and get notified when it changes.

Installation

npm install reactive-media-queries
#or
yarn add reactive-media-queries

Usage

This library exports a single function with the following signature: reactToBreakpoints(breakpoints, callback) => teardownFunction It returns a teardown function that you can use to remove all the DOM listeners.

Example

import reactToBreakpoints from 'reactive-media-queries'

const teardownListeners = reactToBreakpoints({
  phone: 768,
  tablet: 1024,
  desktop: 1440,
  desktopXL: Infinity,
}, (key) => {
  // whenever a matched breakpoint changes this callback is triggered and the key argument is the current breakpoint id
  // eg: key == phone | tablet | desktop | desktopXL
  console.log('Current breakpoint is: ' + key);
})

// if you need to unsubscribe to media queries and detach event listeners just call the function return by reactToBreakpoints()
teardownListeners()

Need dynamic breakpoints?

Just teardown previous listeners and run reactToBreakpoints(newBreakpoints, callback)

Usage with hooks (Vue, React, etc...)

Vue composition API Example

import ReactiveMediaQueries from 'reactive-media-queries';
import { ref } from '@vue/composition-api';

const currentBreakpoint = ref(null);

reactToBreakpoints({
  phone: 768,
  tablet: 1024,
  desktop: 1440,
  desktopXL: Infinity,
}, (key) => {
  currentBreakpoint.value = key;
})();

export default function useMQ () {
  return { currentBreakpoint };
}

FAQs

Package last updated on 03 Dec 2019

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.