Code Monkey home page Code Monkey logo

redux-viewport's Introduction

redux-viewport

WIP

Keep the browser viewport sync with your redux state.

Introduction

First of all, if you want to do reponsive things with React and Redux, take a look at redux-responsive.

When use redux-viewport ?

redux-viewport is designed to dynamically set viewport listeners in your application.

examples :

  • responsive breakpoints are not the same between pages.
  • responsive breakpoints are sent by the server.
  • disable some responsive breakpoints in specific situation.

Installation

npm install --save guillaumearm/redux-viewport

Setting up store

import { createStore, applyMiddleware } from 'redux';
import { viewportMiddleware, viewportReducer } from 'redux-viewport';
import * as reducers from './reducers';

const rootReducer = {
    ...reducers,
    viewport: viewportReducer, // this is a reducer, so you can put it everywhere you want in the state
};

const initialState = {};

const store = createStore(rootReducer, initialState, applyMiddleware(viewportMiddleware));

Usage

Listen a single media

import { listenMedia } from 'redux-viewport';
const { dispatch, getState } = store;
const getIsLandScape = () => getState().viewport.isLandscape;

getIsLandScape() // undefined
dispatch(listenMedia('isLandscape', '(orientation: landscape)'));
getIsLandScape() // true/false

state.viewport.isLandscape will be keep in sync with the browser.

Stop to listen a single media

  • you can use clearMedia() action creator to stop listening media.
import { clearMedia } from 'redux-viewport';

dispatch(clearMedia('isLandscape'))
getIsLandScape() // undefined
  • if you want to keep last boolean value in the store when clear a media, use the optional parameter of clearMedia()
import { clearMedia } from 'redux-viewport';

dispatch(clearMedia('isLandscape'), { keepValue: true });
getIsLandScape() // true/false

Listen multiple media in one dispatch

import { listenMedia } from 'redux-viewport';

dispatch(listenMedia({
    'isLandscape': '(orientation: landscape)',
    'isPortrait': '(orientation: portrait)',
}))

Stop to listen multiple media in one dispatch

import { clearMedia } from 'redux-viewport';

dipatch(clearMedia([
    'isLandscape,'
    'isPortrait',
]))

note you can use keepValue option here too.

Actions

redux-viewport actions are FSA compliant :

  • @@viewport/LISTEN_MEDIA is created by listenMedia()
import { listenMedia, LISTEN_MEDIA } from 'redux-viewport';

listenMedia('isLandscape', '(orientation: landscape)');
/* return this action
{
    type: LISTEN_MEDIA, // @@viewport/LISTEN_MEDIA
    payload: {
        'isLandscape': '(orientation: landscape)',
    },
}
*/
  • @@viewport/CLEAR_MEDIA is created by clearMedia()
import { clearMedia, CLEAR_MEDIA } from 'redux-viewport';

clearMedia('isLandscape');
/* return this action
{
    type: CLEAR_MEDIA, // @@viewport/CLEAR_MEDIA
    payload: ['isLandscape'],
    meta: { keepValue: false },
}
*/
  • @@viewport/UPDATE_MEDIA is dispatched by the viewport middleware, you can catch them in your reducers.
import { UPDATE_MEDIA } from 'redux-viewport';

/*
{
    type: UPDATE_MEDIA, // @@viewport/UPDATE_MEDIA
    payload: {
        'isLandscape': true,
    },
}
*/

Contributing

Feel free to contribute. please see CONTRIBUTING.MD

redux-viewport's People

Watchers

 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.