Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

react-viewport-utils

1.12.1 • Public • Published

React Viewport Utils

A set of low level utility components for react to make working with the viewport (e.g scroll position or size of the page) easy to use and performant by default.

Build Status Coverage Status

See the example folder for more information about what you can build with it.

Why?

On a website with more sophisticated user interactions a lot of components need access to viewport information to e.g. know whether they are in the viewport, should resize or trigger an animation.

Most of the libraries reimplement the required functionality for that kind of features on its own over and over again. Those functionalities are not just hard to implement but can also, if not done well, cause the UX to suffer by introducing layout thrashing and therefore jank and will also cause the bundle size to grow which reduce the time to interaction. Further its hard to prioritize between highly and less important events if the implementation is not bundled in one central position.

This library solves all those issues by

  • using one central event handler per event to collect data
  • triggers updates to components using request animation frame
  • allows to prioritize the importance of updates at runtime which allows to drop frames for less important updates in case the main thread is busy
  • implements patterns like onUpdate callbacks, render props, higher order components and hooks which make the developer experience as simple as possible and allows the developer to concentrate on the application and not on global event handling.

Installation/ requirements

Please note that react version 16.3 or higher is required for this library to work because it is using the context as well as references api.

npm install --save react-viewport-utils

By default the library ships with Typescript definitions, so there is no need to install a separate dependency. Typescript is no a requirement, all type definition are served within separate files.

For detection of some resize events the ResizeObserver API is used internally which is not supported in some browsers. Please make sure to implement a polyfill on your own in case its required for your application.

Supported Environments

Browsers

The goal is to support the most recent versions of all major browsers (Internet Explorer, Edge, Safari, Chrome and Firefox).

We try to be downward compatible with older browsers when possible to at least not throw errors, but older versions will not be test at all.

In case you have specific requirements, please fill an issue or create a PR so we can discuss about them.

NodeJS

The project aims to support recent releases of v8 and v10 and higher of NodeJS.

Documentation

API

Concepts

License

Licensed under the MIT License.

Install

npm i react-viewport-utils

DownloadsWeekly Downloads

2,289

Version

1.12.1

License

MIT

Unpacked Size

274 kB

Total Files

28

Last publish

Collaborators

  • avatar