Git Product home page Git Product logo

mediaquery's Introduction

Build Status Coverage Status Dependencies Status Code Climate

Join the chat at https://gitter.im/axyz/mediaquery

mediaquery

Turns human readable breakpoints into correct mediaqueries.

This is particularly useful when working with window.matchMedia or when you work with CSS dinamically using JavaScript.

Install

If you are using nodejs (but also browserify, webpack, etc...) you can simply use npm:

npm install mediaquery

then you can

var MQ = require('mediaquery');

By the way you if you are not using node, you may use bower

bower install mediaquery

then you can use it from window.MQ or require it using AMD

Show me the code!

Assuming you have an object to describe the mediaqueries you need such as

var myMedias = {
  small: 300,
  medium: 600,
  tablet: 'tablet media query',
  big: 1024,
  huge: Infinity
};

Note that all the numerical values (Infinity included) will be automatically sorted in the correct order, while all the strings will be considered custome media queries; every other type of values will be stripped out.

So small: 300 means that my small media query will span from 0 to 300px, while medium will be from 301px to 600px; finally huge will go from 1025px to... Infinitely large screens.

If you do not specify an Infinity breakpoint a default breakpoint will be created spanning from the biggest available breakpoint to infinity.

asObject(obj)

You can use asObject to get back a similar object where the numbers for your breakpoints are substituted with correct mediaqueries.

e.g.

var MQ = require('mediaqueries');

MQ.asObject(myMedias);

will return

{
  small: 'screen and (max-width: 300px)',
  medium: 'screen and (min-width: 301px) and (max-width: 600px)',
  big: 'screen and (min-width: 601px) and (max-width: 1024px)',
  huge: 'screen and (min-width: 1025px)',
  tablet: 'tablet media query'
}

asArray(obj)

You can also have an array of couples ['name', 'mediaquery'] in output.

e.g.

MQ.asArray(myMedias);

will return

[
  ['small', 'screen and (max-width: 300px)'],
  ['medium', 'screen and (min-width: 301px) and (max-width: 600px)'],
  ['big', 'screen and (min-width: 601px) and (max-width: 1024px)'],
  ['huge', 'screen and (min-width: 1025px)'],
  ['tablet', 'tablet media query']
]

getBreakPoints(obj)

If you need to filter out the numerical breakpoints from a mixed object you can use getBreakPoints e.g.

MQ.getBreakPoints(myMedias);

will return

{
  small: 300,
  medium: 600,
  big: 1024,
  huge: Infinity // remember that Infinity is a number
}

getCustomQueries(obj)

In the same way you may need to filter out all the custom strings e.g.

MQ.getCustomQueries(myMedias);

will return

{
  tablet: 'tablet media query'
}

mediaquery's People

Contributors

axyz avatar gitter-badger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.