Giter VIP home page Giter VIP logo

media-query-sync's Introduction

Media Query Sync

Re-use CSS media queries in JavaScript.

Motivation

  • DRY: All media queries are stored in one place. Especially when using tools as LESS CSS or SASS where the design breakpoints may be defined in the global variables file. Using this JS plug-in, one don't need to repeat the CSS media queries in a JS file.
  • Fewer event actions for the layout: We may also listen to the resize event, but in many cases a redraw of the UI or a re-computing of layout options is not necessary on each resize action, but most times only if a design breakpoint was reached.

Documentation

This plug-in is implemented as AMD and has a fallback to a global variable.

Requirements

The plug-in uses the JavaScript CustomEvent. If you target browsers that do not know this object, you have to include the appropriate polyfill. (For example IE 10 on Microsoft Surface tablets)

    if( typeof window.CustomEvent !== 'function' ) {
        /* include polyfill (using your preferred script loading mechanism) */
    }

Design breakpoints

The breakpoints are the following:

  1. 'xs'
  2. 'sm'
  3. 'md'
  4. 'lg'
  5. 'xl'

The numbers correspond to the z-index of the element that is used for the tests. Those z-indizes are set via CSS inside a given @media query. See the attached CSS for an example. David Walsh also proposed to use pseudo elements and their content property to store the media query breakpoint identifiers directly. But older versions of the iOS Safari and native Android browsers do not support getComputedStyle on pseudo-elements.

There is a sister-repository at https://github.com/votum/media-queries from which may take the CSS media queries as those are defined more thorough.

API

Custom event

The first option to read a design breakpoint is to listen to the event deviceStateChanged, which has a property deviceState. This property contains a string naming the breakpoint that was reached as the event was fired.

Public method

The MediaQuerySync module has one public method: getDeviceState(). This method returns the current design breakpoint. This can be used if your layout changes do not only depend on screen size changes.

Usage

General

Include the provided CSS file in the HTML HEAD of your template or append it to your custom CSS/LESS.

    <link rel="stylesheet" href="media-query-sync.css">

Event based usage

You need to set up the event handler before the script is included to capture the initial event which is fired upon executing the module.

AMD

    window.addEventListener( 'deviceStateChanged', function( event ) {
        console.log( 'another breakpoint was reached: ' + event.deviceState );
    }, false );
    
    require( [ 'media-query-sync' ] );

Browser global

    <script type="text/javascript">
        window.addEventListener( 'deviceStateChanged', function( event ) {
            console.log( 'another breakpoint was reached: ' + event.deviceState );
        }, false );
    </script>
    <script type="text/javascript" src="media-query-sync.js"></script>

Direct usage

AMD

    require( [ 'media-query-sync' ], function( MediaQuerySync ) {
        console.log( 'the current breakpoint is: ' + MediaQuerySync.getDeviceState() );
    } );

Browser global

    <script type="text/javascript" src="media-query-sync.js"></script>
    <script type="text/javascript">
        console.log( 'the current breakpoint is: ' + mediaQuerySync.getDeviceState() );
    </script>

Demo

You can run the usage example via rawgit.com: https://rawgit.com/votum/media-query-sync/master/demo.htm (Watch the JS console output.)

media-query-sync's People

Stargazers

 avatar  avatar

Watchers

 avatar  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.