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-window-decorators

v1.0.8

Published

Two decorators (higher order components) that inject 'window' scroll position, dimensions, orientation and breakpoint to your component's props.

Downloads

1,018

Readme

React Window Decorators

npm version npm downloads

Two decorators (higher order components) that inject window scroll position, dimensions, orientation, breakpoint* and isTouchDevice to your component's props.

If you are not sure what it does, play with the demo.

All modern browsers and IE10+.

* You need to pass breakpoint data (check below).

Changelog

Usage

Library is made as ES module, and you should use it with a module bundler (tested with webpack).

withScroll decorator

Using decorator syntax (my preferred way).

import { withScroll } from 'react-window-decorators';

@withScroll
export default class YourComponent extends Component {
  render() {
    return (
      <div>
        Vertical scroll position is: { this.props.scrollPositionY }
      </div>
    );
  }
}

Or without decorator syntax

import { withScroll } from 'react-window-decorators';

class YourComponent extends Component {
  render() {
    return (
      <div>
        Vertical scroll position is: { this.props.scrollPositionY }
      </div>
    );
  }
}

export default withScroll(YourComponent);

If you run it on the server, withScroll will return 0 as the initial value.

withWindow decorator

withWindow internally uses WindowManager for tracking resize events. If you want to use breakpoints feature you need to set it by creating new WindowManager and passing it array with breakpoints data. Each breakpoint object must contain a name and media query which will be passed to matchMedia.

Second argument is debounceTime which determines resize event's debounce time. Default is 250.

WindowManager is a singleton, so this should be done only once before using decorator.

import { WindowManager } from 'react-window-decorators';

// Example breakpoints data
const BREAKPOINTS = [
  {
    name: 'small',
    media: '(min-width: 0)',
  },
  {
    name: 'medium',
    media: '(min-width: 600px)',
  },
];

// Set breakpoints data
// Somewhere in your application bootstrap
new WindowManager(BREAKPOINTS);

If you don't pass breakpoints data, breakpoint prop will always be null.

Using decorator syntax (my preferred way).

import { withWindow } from 'react-window-decorators';

@withWindow
export default class YourComponent extends Component {
  render() {
    return (
      <div>
        <div>Window dimensions are: { this.props.dimensions.width }/{ this.props.dimensions.height }</div>
        <div>Window orientation is: { this.props.orientation }</div>
        <div>Window breakpoint is: { this.props.breakpoint }</div>
        <div>Device is touch enabled: { this.props.isTouchDevice.toString() }</div>
      </div>
    );
  }
}

Or without decorator syntax

import { withWindow } from 'react-window-decorators';

class YourComponent extends Component {
  render() {
    return (
      <div>
        <div>Window dimensions are: { this.props.dimensions.width }/{ this.props.dimensions.height }</div>
        <div>Window orientation is: { this.props.orientation }</div>
        <div>Window breakpoint is: { this.props.breakpoint }</div>
        <div>Device is touch enabled: { this.props.isTouchDevice.toString() }</div>
      </div>
    );
  }
}

export default withWindow(YourComponent);

If you run it on the server, withWindow will return these initial values

{
  dimensions: {
    width: 0,
    height: 0,
  },
  breakpoint: null,
  orientation: null,
  isTouchDevice: false,
};

Chaining Decorators

@withWindow
@withScroll
export default class YourComponent extends Component {
  ...
}

or

class YourComponent extends Component {
  ...
}

export default withWindow(withScroll(Demo));

License

Released under MIT License.