Git Product home page Git Product logo

rezponsive's Introduction

rezponsive

React decorator for responsive behaviors.

When developing applications for multiple screen sizes and devices we often need our components to know the current matched mediaquery in order to adapt its aspect and behavior.

Rezponsive is a decorator for React components that will inject those informations to its children props and/or context.

Usage

Add rezponsive as a dependency

$ npm install --save rezponsive

then simply decorate your root container component

import React from 'react';
import Rezponsive from 'rezponsive';

@Rezponsive // when stage-3 is available
class MyApp extends Component {
    render() {
        // currentMedia and isTouch are available here
        ...
    }
}

export default MyApp;

// or without stage-3
export default Rezponsive(MyApp);

props.currentMedia will be an object in the format:

{
    mediaqueryName1: bool (true if matches current mediaquery),
    mediaqueryName2: bool (true if matches current mediaquery),
    ...
}

To define your mediaqueries you should provide an mq object in the formats supported by the mediaquery library

<MyApp
    mq={{
        small: 300,
        medium: 600,
        tablet: 'tablet media query',
        big: 1024,
        huge: Infinity
    }}
    isTouchOnServer // if true will treat server environment as touch devices
    serverMedia={
        small: true,
        medium: false
    } // if not defined will default to { all: true }
    clientMedia={
        small: true,
        medium: false
    } // if not defined will default null
/>

on isomorphic app you probably want to define a severMedia to choose what to render on server side and eventually provide the isTouchOnServer property in case you want to render as it is a touch device.

MatchMedia detection will cause an additional rendering when the component is mounted in order to update the state accordingly to the matched queries. For heavy components this may lead to performance issues on first loading, to avoid that you can provide an clientMedia prop that will be used for the initial rendering and the state will only change if the current mediaquery changes. Of course however you have to be sure that clientMedia reflect the current media query, otherwise your app will start with the wrong settings and will keep them until a resize big enough to trigger matchmedia updates.

Sometimes you may want to have mediaquery informations on really deep nested object, rezponsive will inject currentMedia and isTouch not only on props, but also on the context of the underlying react subtree.

You can easily access the context on a child component using the exported RezponsiveContext

import Rezponsive, { RezponsiveContext } from 'rezponsive';

const Component = Rezponsive(({ currentMedia, isTouch }) => {
  return (
    <RezponsiveContext.Consumer>
      {({ currentMedia, isTouch }) => (
        <div>{JSON.stringify({ currentMedia, isTouch })}</div>
      )}
    </RezponsiveContext.Consumer>
  );
});

or using the provided helper decorator RezponsiveConsumer

const Component = RezponsiveConsumer(({ currentMedia, isTouch }) => (
  <div>{JSON.stringify({ currentMedia, isTouch })}</div>
));

// assuming that MyApp is decorated with Rezponsive
<MyApp>
  <Component />
</MyApp>

In case you want to use an external listener to update clientMedia, you can set the disableListeners prop to true and manually update clientMedia and isTouch.

rezponsive's People

Contributors

addityasingh avatar axyz avatar shuhei avatar

Stargazers

 avatar

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.