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

@stanko/react-window

v0.9.3

Published

React components that simplify the management of window and body event listeners.

Downloads

102

Readme

ReactWindow

npm version npm downloads

React components that simplify the management of window and body event listeners.

Instead of manually adding (and removing) listeners using useEffect, ReactWindow allows you to attach them in a more intuitive way:

// for window listeners
<ReactWindow
  onClick={() => {
    console.log("Hello world!");
  }}
/>

Demo and Changelog

The library is inspired by svelte and is available with the following features:

  • Simplifies management of window (and body) event listeners.
  • Attaches a single event per listener type (if you have multiple instances of ReactWindow with onClick in your app, only a single click event will be attached).
  • Supports conditional rendering.
  • Supports capture and passive listener options.
  • Tiny - around 0.5 kB minified and gzipped.
  • Fully typed.

Usage

Get it from npm:

$ npm install --save @stanko/react-window

Import and use it in your React app:

ReactWindow base example

import ReactWindow from "@stanko/react-window";

function Example() {
  return (
    <ReactWindow
      onClick={() => {
        console.log("Hello world!");
      }}
      onScroll={() => {
        console.log(`Wheeeeee! ${window.scrollY}px`);
      }}
    />
  );
}

ReactBody base example

ReactBody works same as ReactWindow but attaches listeners to document.body instead of window.

import { ReactBody } from "@stanko/react-window";

function Example() {
  return (
    <ReactBody
      onClick={() => {
        console.log("Hello!");
      }}
    />
  );
}

Conditional rendering

import { useState } from "react";
import ReactWindow from "@stanko/react-window";

function Example() {
  const [listenForScroll, setListenForScroll] = useState(true);

  return (
    <div>
      <button
        onClick={() => {
          setListenForScroll(!listenForScroll);
        }}
      >
        {listenForScroll ? "Disable" : "Enable"} scroll listener
      </button>

      {listenForScroll && (
        <ReactWindow
          onScrollPassive={() => {
            console.log(`Wheeeeee! ${window.scrollY}px`);
          }}
        />
      )}
    </div>
  );
}

Listener options

React Window supports capture and options listener options. These options can be set using the [onEventName]Capture and [onEventName]Passive variations.

For example, to set capture option for onClick, you would use onClickCapture.

And to set passive option for onScroll, you would use onScrollPassive.

Gotchas

There are a couple of gotchas to keep in mind when working with React Window:

Events are only added on mount

Events are only added on mount and removed on unmount. This means that if you change your handler dynamically, nothing will happen.

In other words, avoid the following pattern:

<ReactWindow
  onClick={
    someCondition ? () => {
      console.log("handling the event in one way");
    } : () => {
      console.log("handling the event in a different way");
    }
  }
/>

This behavior is intentional, as it improves performance and eliminates the need for unnecessary rerenders.

Double click

React Window uses onDblClick to match the native event name, which differs from the built-in React version that uses onDoubleClick.

Supported events

React Window has full TypeScript support and your IDE should provide you with an autocomplete.

Visual Studio autocomplete for ReactWindow

Here's a list of all supported events for reference:

Misc

| Event | Capture variant | | -------- | --------------- | | onLoad | onLoadCapture | | onSelect | onSelectCapture | | onError | onErrorCapture |

Scroll / Wheel

| Event | Capture variant | Passive variant | | -------- | --------------- | --------------- | | onScroll | onScrollCapture | onScrollPassive | | onWheel | onWheelCapture | onWheelPassive |

Focus / Blur

| Event | Capture variant | | ------- | --------------- | | onFocus | onFocusCapture | | onBlur | onBlurCapture |

Keyboard

| Event | Capture variant | | --------- | ---------------- | | onKeyDown | onKeyDownCapture | | onKeyUp | onKeyUpCapture |

Mouse

| Event | Capture variant | | ------------- | -------------------- | | onAuxClick | onAuxClickCapture | | onClick | onClickCapture | | onDblClick | onDblClickCapture | | onContextMenu | onContextMenuCapture | | onDrag | onDragCapture | | onDragEnd | onDragEndCapture | | onDragEnter | onDragEnterCapture | | onDragExit | onDragExitCapture | | onDragLeave | onDragLeaveCapture | | onDragOver | onDragOverCapture | | onDragStart | onDragStartCapture | | onDrop | onDropCapture | | onMouseDown | onMouseDownCapture | | onMouseMove | onMouseMoveCapture | | onMouseOut | onMouseOutCapture | | onMouseOver | onMouseOverCapture | | onMouseUp | onMouseUpCapture | | onMouseEnter | | | onMouseLeave | |

Touch

| Event | Capture variant | Passive variant | | ------------- | -------------------- | ------------------- | | onTouchCancel | onTouchCancelCapture | | | onTouchEnd | onTouchEndCapture | | | onTouchMove | onTouchMoveCapture | onTouchMovePassive | | onTouchStart | onTouchStartCapture | onTouchStartPassive |

Pointer

| Event | Capture variant | | -------------------- | --------------------------- | | onPointerDown | onPointerDownCapture | | onPointerMove | onPointerMoveCapture | | onPointerUp | onPointerUpCapture | | onPointerCancel | onPointerCancelCapture | | onPointerEnter | onPointerEnterCapture | | onPointerLeave | onPointerLeaveCapture | | onPointerOver | onPointerOverCapture | | onPointerOut | onPointerOutCapture | | onGotPointerCapture | onGotPointerCaptureCapture | | onLostPointerCapture | onLostPointerCaptureCapture |

Animation

| Event | Capture variant | | -------------------- | --------------------------- | | onAnimationStart | onAnimationStartCapture | | onAnimationEnd | onAnimationEndCapture | | onAnimationIteration | onAnimationIterationCapture | | onAnimationCancel | onAnimationCancelCapture |

Transition

| Event | Capture variant | | ------------------ | ------------------------- | | onTransitionStart | onTransitionStartCapture | | onTransitionEnd | onTransitionEndCapture | | onTransitionRun | onTransitionRunCapture | | onTransitionCancel | onTransitionCancelCapture |

Development

Install dependencies and run npm start. It will spin up a development server on http://localhost:8000