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

rezponsive

v2.0.0

Published

React decorator for responsive behaviors

Downloads

15,828

Readme

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.