Giter VIP home page Giter VIP logo

simple-breakpoints's Introduction

Simple Breakpoints

npm npm

A simple breakpoints plugin based off of four simple breakpoint sizes ('mobile', 'tablet', 'small_desktop', 'large_desktop') with support to listen for breakpoint change events

Installation and usage

$ npm install simple-breakpoints --save
    import SimpleBreakpoints from 'simple-breakpoints'

    // default breakpoint sizes: { mobile: 480, tablet: 640, small_desktop: 1024, large_desktop: 1180 }
    var breakpoints = new SimpleBreakpoints();

    if(breakpoints.isMobile()) {
        // do something for mobile
    }

    if(breakpoints.isSmallDesktop()) {
        // do something for small desktop
    }

    if(breakpoints.isBetween('small_desktop', 'large_desktop')) {
        // do something between small and large desktop
    }

Listening to events

    // on all breakpoint changes
    breakpoints.on('breakpointChange', (from, to) => {
        console.log(`change from ${from} to ${to}`);
        // change from small_desktop to large_desktop
        // change from tablet to mobile
    });

    // on breakpoint changes from small to large
    breakpoints.on('breakpointChangeUp', (from, to) => {
        console.log(`change Up from ${from} to ${to}`);
        // change from small_desktop to large_desktop
    });

    // on breakpoint changes from large to small
    breakpoints.on('breakpointChangeDown', (from, to) => {
        console.log(`change Down from ${from} to ${to}`);
        // change from large_desktop to small_desktop
    });

    // remove all events from `breakpointChangeDown`
    breakpoints.off('breakpointChangeDown');

    // fire an event only once
    breakpoints.once('breakpointChange', (from, to) => {
        console.log(`firing an event ${from} to ${to} only once`);
        // change from large_desktop to small_desktop
    });

Defining your own breakpoints

    import SimpleBreakpoints from 'simple-breakpoints'

    var breakpoints = new SimpleBreakpoints({
        mobile        : 320,
        tablet        : 640,
        small_desktop : 900,
        large_desktop : 1200
    });

API

Function Params Description
getViewportSize returns the viewport size as object { width: 1024, height: 768 } this is fired on window.resize and is stored in breakpoints.viewport
currentBreakpoint returns current breakpoint size ('mobile', 'tablet', 'small_desktop', 'large_desktop')
isBetween smallBreakpoint, largeBreakpoint ('mobile', 'tablet', 'small_desktop', 'large_desktop') Check if viewport is between two breakpoints
isLessThan breakpoint ('mobile', 'tablet', 'small_desktop', 'large_desktop') Check if viewport is less than the width of the breakpoint
isGreaterThan breakpoint ('mobile', 'tablet', 'small_desktop', 'large_desktop') Check if viewport is greater than the width of the breakpoint
isLessThanEqualTo breakpoint ('mobile', 'tablet', 'small_desktop', 'large_desktop') Check if viewport is less than or equal the width of the breakpoint
isGreaterThanEqualTo breakpoint ('mobile', 'tablet', 'small_desktop', 'large_desktop') Check if viewport is greater than or equal the width of the breakpoint
isMobile Check if the viewport is within the mobile breakpoint
isTablet Check if the viewport is within the tablet breakpoint
isSmallDesktop Check if the viewport is within the small_desktop breakpoint
isLargeDesktop Check if the viewport is within the large_desktop breakpoint
on (event, callback) (`'breakpointChange' 'breakpointChangeUp'
once (event, callback) (`'breakpointChange' 'breakpointChangeUp'
off (event) (`'breakpointChange' 'breakpointChangeUp'

For using with Vue.js, see vue-simple-breakpoints

simple-breakpoints's People

Contributors

drewjbartlett avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

simple-breakpoints's Issues

NextJS Support

for someone who want use this in NEXTJS (window is not defined)

//import 
const SimpleBreakpoints =  require('simple-breakpoints');

function isMobile(){
  if (process.browser && typeof window !== 'undefined') {
    // client-side-only code
    // ------- Use following code -------
    let SimpleBreakpointsNode = (SimpleBreakpoints.default);
    let breakpoints = new SimpleBreakpointsNode();
    return breakpoints.isMobile();
  }
  else{
    return false
  }

}

isTablet

Hi,

we use this plugin in Vue.Js (wrap with vue-simple-breakpoints).

It's really usefull, thanks!!!

One of the frontend developer had problem with the tablet breakpoint. Now it's working when I change the code this way :

key: 'isBetween', value: function isBetween(smallBreakpoint, largeBreakpoint) { return this.viewport.width **>** this.breakpoints[smallBreakpoint] && this.viewport.width <= this.breakpoints[largeBreakpoint]; }
and :
}, { key: 'isTablet', value: function isTablet() { return this.isBetween('mobile', 'tablet'); } }, { key: 'isSmallDesktop', value: function isSmallDesktop() { return this.isBetween('tablet', 'small_desktop'); } }, { key: 'isLargeDesktop', value: function isLargeDesktop() { return this.isGreaterThan('small_desktop') }

I have to test this a little bit more. Do you want me to send you a PR ? Maybe I miss something in the logic ?

Thanks,

Hugo

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.