All Projects → jamesseanwright → react-animation-frame

jamesseanwright / react-animation-frame

Licence: other
A React higher-order component that invokes a callback in a wrapped component via requestAnimationFrame

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-animation-frame

React Nprogress
⌛️ A React primitive for building slim progress bars.
Stars: ✭ 173 (+268.09%)
Mutual labels:  higher-order-component, hoc
Recompact
⚛ A set of React higher-order components utilities. Drop-in replacement for recompose.
Stars: ✭ 419 (+791.49%)
Mutual labels:  higher-order-component, hoc
react-drip-form
☕ HoC based React forms state manager, Support for validation and normalization.
Stars: ✭ 66 (+40.43%)
Mutual labels:  higher-order-component, hoc
react-app-loader
Production ready library for handling Microfrontends
Stars: ✭ 75 (+59.57%)
Mutual labels:  higher-order-component, hoc
Next Nprogress
Next.js HOC to integrate NProgress inside your app
Stars: ✭ 145 (+208.51%)
Mutual labels:  higher-order-component, hoc
React With Direction
Components to provide and consume RTL or LTR direction in React
Stars: ✭ 176 (+274.47%)
Mutual labels:  higher-order-component, hoc
Neoform
✅ React form state management and validation
Stars: ✭ 162 (+244.68%)
Mutual labels:  higher-order-component, hoc
addhoc
Handy little helper to create proper React HOC functions complete with hoisted statics and forwarded refs
Stars: ✭ 40 (-14.89%)
Mutual labels:  higher-order-component, hoc
Flagged
Feature Flags for React made easy with hooks, HOC and Render Props
Stars: ✭ 97 (+106.38%)
Mutual labels:  higher-order-component, hoc
Incompose
A inferno utility belt for function components and higher-order components
Stars: ✭ 76 (+61.7%)
Mutual labels:  higher-order-component, hoc
React Bps
🔱 Create breakpoints to your component props
Stars: ✭ 64 (+36.17%)
Mutual labels:  higher-order-component, hoc
rrx
⚛️ Minimal React router using higher order components
Stars: ✭ 69 (+46.81%)
Mutual labels:  higher-order-component, hoc
Hocs
🍱 Higher-Order Components for React
Stars: ✭ 1,784 (+3695.74%)
Mutual labels:  higher-order-component, hoc
react-table-hoc-draggable-columns
ReactTable HOC for draggable columns
Stars: ✭ 27 (-42.55%)
Mutual labels:  higher-order-component, hoc
animate
👾 Create and manage animation functions with AnimationFrame API.
Stars: ✭ 11 (-76.6%)
Mutual labels:  requestanimationframe
react-listener-provider
Create a ReactListenerProvider and use HOC (Higher Order Components) to listen for Events in one place.
Stars: ✭ 19 (-59.57%)
Mutual labels:  higher-order-component
react-portalgun
Lightweight portal system for React. Mega seeds included 🔫
Stars: ✭ 75 (+59.57%)
Mutual labels:  higher-order-component
hoc-react-animate
Add a CSS class whenever a props change (or/and at mount)
Stars: ✭ 14 (-70.21%)
Mutual labels:  hoc
Use Url State
Lift a React component's state into the url
Stars: ✭ 154 (+227.66%)
Mutual labels:  higher-order-component
react-combine-contexts
🚀Use multiple React Contexts without pain.
Stars: ✭ 23 (-51.06%)
Mutual labels:  hoc

React Animation Frame

Build Status Coverage Status npm version

A React higher-order component for invoking component repeating logic using requestAnimationFrame. It works in both the browser and React Native.

The module follows the CommonJS format, so it is compatible with Browserify and Webpack. It also supports ES5 and beyond.

Motivation

Take a look at the example project; this contains a Timer component that should animate a progress bar until the configured end time is surpassed:

'use strict';

const React = require('react');
const ReactAnimationFrame = require('react-animation-frame');

class Timer extends React.Component {
    onAnimationFrame(time) {
        const progress = Math.round(time / this.props.durationMs * 100);
        this.bar.style.width = `${progress}%`;

        if (progress === 100) {
            this.props.endAnimation();
        }
    }

    render() {
        return (
            <div className="timer">
                <p>{this.props.message}</p>
                <div className="timer__bar" ref={node => this.bar = node}></div>
            </div>
        );
    }
}

module.exports = ReactAnimationFrame(Timer);

The onAnimationFrame method will be called on each repaint, via requestAnimationFrame, by the higher-order ReactAnimationFrame component. Once the progress of our animation reaches 100%, we can kill the underlying loop using the endAnimation method passed to the props of the wrapped component.

The loop can also be throttled by passing a second parameter to ReactAnimationFrame, which represents the number of milliseconds that should elapse between invocations of onAnimationFrame:

module.exports = ReactAnimationFrame(Timer, 100);

Installation

npm i --save react-animation-frame

API

ReactAnimationFrame(Component[, throttleMs])

Wraps Component and starts a requestAnimationFrame loop. throttleMs if specified, will throttle invocations of onAnimationFrame by any number of milliseconds.

Inside a wrapped component

onAnimationFrame(timestamp, lastTimestamp)

Called on each iteration of the underlying requestAnimationFrame loop, or if the elapsed throttle time has been surpassed. timestamp is the same DOMHighResTimeStamp with which requestAnimationFrame's callback is invoked.

The previous timestamp is also passed, which can be useful for calculating deltas. For n calls, lastTimestamp will be the value of timestamp for call n - 1.

this.props.endAnimation()

Cancels the current animation frame and ends the loop.

this.props.startAnimation()

Function to restart the animation after it was ended by endAnimation.

Local development

Run npm i to install the dependencies.

  • npm run build - transpile the library
  • npm test - lints the code and runs the tests
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].