npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-breakpoint

v3.0.6

Published

Device width resize dispatcher to manage breakpoints for redux based application.

Downloads

2,116

Readme

redux-breakpoint

Make your component aware of breakpoint and device width just by connecting to store.

This is a lightweight library to help your redux based application to be more responsive.

Store State

Browser Support

This library is supported in all browsers where redux is supported. No extra dependencies are added.

Intro

redux-breakpoint library is useful for the following cases:

  1. If you have to write different rendering logic in your components for different device-width or breakpoint, this library will be handy. (You need this library because JS and CSS media-query are not mutual).

  2. If you are writing or using any component level responsive library to detect width-change or breakpoint-change, then you're against the core principle of Redux - Single source of truth . Device width-change / breakpoint-change is global for your application, So it must be in redux store.

  3. This library uses breakpoint definition of bootstrap4. It doesn't mean that, store will get dispatched only for those breakpoint change. Store gets dispatched for every width change (Performance is taken care with debouncing). So you have full flexibility to have your own unlimited breakpoint definition by using the width from store.

Theoritically, Every pixel is a breakpoint.

Note: This library can be used in React, Angular2 or in any other application where redux is used.

Live Demo

Installation

npm install redux-breakpoint --save

How to use

redux-breakpoint has just two functions:- syncBreakpointWithStore and breakpointReducer.

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import syncBreakpointWithStore, {breakpointReducer} from 'redux-breakpoint'

import reducers from '<file-path>/reducers'

// Add the breakpoint reducer to your store
const store = createStore(
  combineReducers({
    ...reducers,
    breakpoint: breakpointReducer
  })
)

// make your store sync with device width change.
syncBreakpointWithStore(store);

ReactDOM.render(
  <Provider store={store}>
    <App/>
  </Provider>,
  document.getElementById('root')
)

syncBreakPointWithStore(store,config)

This is similar to syncHistoryWithStore from react-redux-router. As soon as you created the store object, call syncBreakPointWithStore function by passing your store object as written in the above code.

The second parameter config is an optional parameter. Use it only if you want to change the default debounce settings. By default, dispatchHeight is False debounce is True and debounceTime is 100ms.

ex:

To enable height change dispatch:

 syncBreakPointWithStore(store, {
        dispatchHeight: true
     })

To change debounce time:

 syncBreakPointWithStore(store, {
        debounceTime: 200
     })

To disable debouncing: (Not recommended)

   syncBreakPointWithStore(store, {
          debounce: false
       })

breakpointReducer

This is similar to routerReducer from react-redux-router. Wherever you combine your other reducers, just add this along.

Example usage

We'll take one real use case. Let's say we want to use DateRangePicker component from airbnb's react-dates library. It has a property called orientation. And the value can be either "horizontal" or "vertical". To make this component responsive, I have to pass the property value as "vertical" For xs breakpoint (Mobile potrait), for other breakpoints I want to pass it as "horizontal". It's obvious that CSS media-query can't help in this case. redux-breakpoint is there to help us. Let's go..

import React from 'react'
import {connect} from 'react-redux';
import {DateRangePicker} from 'react-dates'  //Sample

class ExampleComponent extends React.Component{
  
   render(){
      let orientation = "horizontal";
      let {breakpoint} = this.props;
      if(breakpoint.isExtraSmall){
         orientation = "vertical";
      } 
       
      return(

     <DateRangePicker orientation={orientation} /> 
     )
   }

}
mapStateToProps(state){ 
  return {
    breakpoint: state.breakpoint
  }
}
)
export default connect(mapStateToProps,null)(ExampleComponent);

If you want to have your own breakpoint, use breakpoint.width .


 if(breakpoint.width < 400){
         orientation = "vertical";
      } 

That's all it takes. When your application grows, you might need more Components/Container to be aware of breakpoints. So it is better to keep the breakpoint/width data in store and the breakpoint calculation logic in reducer.

Available variables

width, isExtraSmall, isSmall, isMedium, isLarge, isExtraLarge. (The breakpoint calculation is same as bootstrap's breakpoint.

Since, store is dispatched for every width change, you have the full flexiblity to have your own breakpoint logic based on the width.

"Why store is dispatched for every width change instead of only breakpoint change ?" - You may ask.

Answer - If we dispatch only for breakpoint change (Small to Medium), we're taking away the flexibility of having our own breakpoint. We consider that every pixel is a breakpoint. That's why we named our action type as 'BREAKPOINT_CHANGE' instead of 'SIZE_CHANGE'.

Features

  1. It uses Debounce logic to avoid unwanted dispatches, in turn it'll avoid unwanted re-rendering.

  2. This library is not only for breakpoint, it also can be used as window resize trigger by enabling dispatchHeight. If any of your components/container has to use resize listener for xxx reason, it's very easy to subscribe to store rather than adding 'resize' listener to window and writing debounce or throttle logic again.

Next steps

  1. Adding support for server side rendering.