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

react-responsive-window

v1.0.8

Published

React Component for managing screen breakpoints using the window resize event and context api.

Downloads

21

Readme

react-responsive-window

React component that provides globally accessible window breakpoint information.

This component can be wrapped around all or just specific react components giving all child components access to the responsive context. This context includes Boolean breakpoints that are set via the window resize event both when the window first loads and every time the window resize event is triggered. This is perfect if you would like to set global window size breakpoints and access them conditionally relative to the window size anywhere in your app.

The are 10 default breakpoints:

desktopLg: 1400
desktopMd: 1300
desktopSm: 1200
tabletLg: 1040
tabletMd: 991
tabletSm: 840
mobileXl: 800
mobileLg: 650
mobileMd: 540
mobileSm: 400

Every time the window resize event is called, the component updates state variables tied to the context by comparing the window size to each break point (breakpoints can be overridden via props). The window resize event is also 'debounced' with an adjustable delay.

For example if the window size is 1250 the variables accessible via the context would be:

desktopLg: true
desktopMd: true
desktopSm: false
tabletLg: false
tabletMd: false
tabletSm: false
mobileXl: false
mobileLg: false
mobileMd: false
mobileSm: false

Install

npm install react-responsive-window
yarn add react-responsive-window

Usage

This component is super easy to use. To include just import the component from the package and wrap around the components that require access to the responsive context.

import React from 'react';
import Responsive from 'react-responsive-window';
import SomeComponent from './SomeComponent';

function App() {
  return (
    <Responsive>
        <SomeComponent/>
    </Responsive>
  );
}

export default App;

Using responsive context in child components

Just 2 lines of code are required to give a child component access to the responsive context.

import {ResponsiveContext} from 'react-responsive-window';

&


const [responsiveObj] = useContext(ResponsiveContext);

In the following example the above lines have been added and the mobile medium breakpoint has been referenced in a conditional that sets the class of the div. If the window size is less than the mobile medium break point, responsiveObj.mobileMd will update causing a state update that changes the class of the div element.

import React from 'react'
import {ResponsiveContext} from 'react-responsive-window';

export default function SomeComponent() {

    const [responsiveObj] = useContext(ResponsiveContext);

    return (
        <div className={(responsiveObj.mobileMd ? "someMobileClass" : "someDesktopClass")}>
            
        </div>
    )
}

or another example of how this may be used to switch in different componets dependant of the window size.

export default function SomeComponent() {

    const [responsiveObj] = useContext(ResponsiveContext);

    let main;

    if (responsiveObj.mobileXl) {
        main = <MobileComponent/>
    } else {
        main = <DesktopComponet/>
    }

    return (
        <React-Fragment>
            {main}
        </React-Fragment>
    )
}

Overriding breakpoints

Breakpoints are set by default to the values shown in the introduction. These breakpoints can be overridden by included values in the component props. In the example below you can see how the breakpoints for desktop and mobile have all been increased by 1.

import React from 'react';
import Responsive from 'react-responsive-window';
import SomeComponent from './SomeComponent';

function App() {
  return (
    <Responsive
      desktopLg= { 1401 }
      desktopMd= { 1301 }
      desktopSm= { 1201 }
      mobileXl= { 801 }
      mobileLg= { 651 }
      mobileMd= { 541 }
      mobileSm= { 401 }
    >
      <SomeComponent/>
    </Responsive>
  );
}

export default App;

Setting debounce and using state update indicator

The default debounce delay set to the window resize event listener call back is 20ms. This can be overridden in the props of the component.

function App() {
  return (
    <Responsive
      debounce={50}
    >
      <SomeComponent/>
    </Responsive>
  );
}

Additionally, it is sometimes useful to keep track of state updates and visually access how the debounce is impacting the application as the window size is adjusted. You can toggle devMode in the props to console.log the a state update counter every time the event listener call-back is executed.

function App() {
  return (
    <Responsive
      debounce={50}
      devMode={true}
    >
      <SomeComponent/>
    </Responsive>
  );
}

Responsive Component Props

desktopLg: Number - Default: 1400
desktopMd: Number - Default: 1300
desktopSm: Number - Default: 1200
tabletLg: Number - Default: 1040
tabletMd: Number - Default: 991
tabletSm: Number - Default: 840
mobileXl: Number - Default: 800
mobileLg: Number - Default: 650
mobileMd: Number - Default: 540
mobileSm: Number - Default: 400
debounce: Number - Default: 20
devMode: Boolean - Default: false

Responsive Context State Variables

responsiveObj.desktopLg : Boolean
responsiveObj.desktopMd : Boolean
responsiveObj.desktopSm : Boolean
responsiveObj.tabletLg : Boolean
responsiveObj.tabletMd : Boolean
responsiveObj.tabletSm : Boolean
responsiveObj.mobileXl : Boolean
responsiveObj.mobileLg : Boolean
responsiveObj.mobileMd : Boolean
responsiveObj.mobileSm : Boolean

License

Released under MIT license.

© 2019 Sean Gynane.