Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ekolabs/react-animation-orchestrator

Package Overview
Dependencies
Maintainers
2
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ekolabs/react-animation-orchestrator

A react-based library for managing complex animations

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
2
Weekly downloads
 
Created
Source

React Animation Orchestrator

A react-based library for managing complex animations

state = (oldState, action) => newState

but

animation = (time) => frame


React Animation Orchestrator is a library that solves the problem of discrepancy between the fact that state changes are nearly instantaneous but animations, by definition, take time to complete.

It provides higher order components to manage multiple, complex animations in an app that contains a lot of state changes that affect its animations, oftentimes even while other animations are running

Based on the timeline feature of the incredible GSAP animation library it offers:

  • Different modes of resolving conflicting animations (queueing or fast-forwarding animations)
  • Support for Static/Dynamic Animations
  • Triggering animations from a state change or from user-initiated events (mouse click, scroll etc)
  • Taking care of annoying edge cases, like how components shouldn't be removed from the DOM until they've performed their requested animations.

See a live demo live!


Overview

React Animation Orchestrator is used in two major steps: Defining animations and Describing scenarios that decide when to run these animations.

  1. First, a user decorates a React component to become an AnimatedComponent. An AnimatedComponent can use its registerAnimation function to register animation specific to the component domain, to be later used within a scenario.

  2. An AnimatedComponent is configured with a set of Scenarios with the attachAnimation function, to act as a "controller" of sorts to its domain-specific animations. Once the props of an AnimatedComponent change, all triggers in all of its scenarios are evaluated. If one of the triggers is evaluated to be triggered, the animations associated with the scenario the trigger belongs to are added to a timeline.

Woah that was a mouthful - in practice this should be much clearer:

Usage example

npm install @ekolabs/react-animation-orchestrator

Defining and registering animation for a component:

import React from "react";
import { TimelineMax } from 'gsap';
import { attachAnimation } from "@ekolabs/react-animation-orchestrator";

// an example of an animation generator function
const lookAtMeAnimation = (ref, options) => {
    let tl = new TimelineMax();   
    let myEl = ref.current;
    
    tl.to(myEl, 0.5, {
            scale: 1.5,
            rotating: '45deg',
            transformOrigin: 'center',
            opacity: 0.7,          
        })
        .to(myEl, 0.2, {
            scale: 1,
            rotating: '0deg',
            transformOrigin: 'center',
            opacity: 1,   
        });

    return tl;
};

class FancyComponent extends React.Component {
    constructor(props){
        super(props);
        this.ref = React.createRef();
        this.props.registerAnimation('lookAtMe', lookAtMeAnimation, this.ref);       
    }
    

    render(){
        return (
            <div ref={this.ref}>Attention-grabbing element</div>
        )
    }
}

export default attachAnimation(FancyComponent);

Configuring scenarios to trigger animations

import React from "react";
import { attachAnimation } from "@ekolabs/react-animation-orchestrator";
import FancyComponent from "./FancyComponent";

class PageComponent extends React.Component {
  
    render(){
        return (
            <div>
                <FancyComponent/>
                <AnotherComponent />
                <MoreComponents />
            </div>
        )
    }
}

export default attachAnimation(PageComponent, [
    // when the grabAttention prop changes from false to true, 
    // we want to queue the lookAtMe animation
    {
        id: 'someChange',
        trigger: {          
             select: props => props.grabAttention,
             value: false,
             nextValue: true
        },
        animations: 'lookAtMe'
    }
]);

API

registerAnimation(animationId, animationGeneratorFunction, elementReference)

Registers a new animation for an AnimatedComponent. This method is a available in the props of an attached component.

ParameterTypeValue
animationId stringA unique id for this animation
animationGeneratorFunctionAnimationGenratorThe animation generator function for this reference
elementReferenceReact referenceA React reference for the DOM object being animated
// example
class FancyComponent extends React.Component {
    constructor(props){
        super(props);
        this.ref = React.createRef();
        this.props.registerAnimation('lookAtMe', lookAtMeAnimation, this.ref);       
    }
    

    render(){
        return (
            <div ref={this.ref}>Attention-grabbing element</div>
        )
    }
}

attachAnimation(WrappedComponent, scenariosConfig)

Creates a higher-order component AnimatedComponent based on the supplied component, along with its scenario configurations.

ParameterTypeValue
WrappedComponentReact.ComponentA react component class (not an instance)
scenariosConfigScenario ConfigurationAn array of scenario configurations to be managed by this component (optional)

addAnimation(animations, timelineOrTimelineId, options)

Manually add animations to a timeline

ParameterTypeValue
animationsAn array of AnimationConfigurationThe animations to run
timelineOrTimelineIdA timeline id or a timeline instanceThe timeline to add the animations to. If a timeline with such id does not exist, a new timeline will be created.
optionsobjectThe options will be passed as the second parameter of the animation generator function

triggerScenario(scenarioId)

Manually triggers the scenario's animations

ParameterTypeValue
scenarioIdstringThe id of the scenario to trigger

setGlobalOptions(options)

Sets global options for React Animation Orchestrator. Options are:

// example
{
    onScenarioTriggered: (matchedScenario) => {},
    onScenarioStart: (refs, scenarioConfig, triggerConfig) => {},
    onScenarioComplete: (refs, scenarioConfig, triggerConfig) => {}
}

##Configuration

###Scenario A scenario describes a set of animations to be added to a timeline once a certain trigger has been met.

PropertyTypeValue
idstringThe scenario ID
triggerTriggerConfiguration / array of TriggerConfigurationTriggers associated with this scenario
timelinestringThe id of the animation where the animations will be inserted to. If not specified the default master timeline is used.
animationsAnimationConfigurationDescribes which animations will be added once a trigger is met
interruptbooleanif true, all other animations currently present the timeline will complete immediately before adding this scenario's animations

###Trigger Configuration

A trigger describes a certain change in props that if evaluated to be true will ultimately result in addition of animations to a timeline

Can be either an object or a function

As an object

When testing the trigger, the select function will be executed with the props as its parameter once on the previous props and once on the next (changed) props. If the result of the previous props selection is equal to value and the next props selection is equal to nextValue the trigger is considered, well, triggered.

{
    // example
    {
        select: props => props.varToCheck,
        value: 'oldValue',
        nextValue: 'newValue'
    }
}

As a function

This allows more custom logic of props comparison to determine how to evaluate the trigger.

// example
(triggerComponent, prevProps, nextProps) => {   
            nextProps.VarToCheck === prevProps.varToCheck + 1;                
        },

Trigger function Parameters:

PropertyTypeValue
triggerComponentReact.Component instanceThe instance of the attached component
prevPropsobjectThe props before the change
nextPropsobjectThe props after the change

###Animation

An animation is a configuration which is ultimately resolved into a GSAP sub-timeline using an Animation Generator function, and then added to a Animation Orchestrator timeline

Can be either a string, an object, a function or an array containing these types for multiple animations.

As a string

'fadeIn'

As an Object

// example
{
 animation: 'fadeIn',
 position: 'withPrev',
 immediate: false,
 onComplete: ()=>{ console.log()}
}

PropertyTypeValue
animationstringThe animation ID
timelinestringThe id of timeline to insert the animation. If not specified the default master timeline is used.
positionstring / number / 'withPrev'Where in the timeline to place the animation. Maps to GSAP position paramater, See documentation. Also accepts a special withPrev value that places this animation at the start time of the previous animation in the timeline.
immediatebooleanif true, animation will completely instantly (duration ~0)
onStartfunctionA callback that will fire when the animation starts. See callback function signature
onCompletefunctionA callback that will fire when the animation ends. See callback function signature
animationOptionsobjectThis object will be passed to the animation generator as a second parameter. Useful for passing data to dynamic animations.

As a function

The animation configuration function will be evaluated when a trigger condition is met. This is useful for dynamic animations. Must return an object or a string (as described above)

// example
animatedComponentInstance => {
                 animation: 'fadeIn',
                 position: '+=2',
                 animationsOptions" {
                    myVar: 12
                 }
             }

As an array of animations

['fadeIn', 'expand']

Usage:

import { attachAnimation } from "@ekolabs/react-animation-orchestrator";

attachAnimation(WrappedComponent, [
    {
        id: 'myAnimation1'
        trigger: ...,
        animations: ...
    },
    {
        id: 'myAnimation2',
        trigger: ...,
        animations: ...
    },
    ...
    ])

###Animation Callback Function

The function that gets executed once when onStart or onComplete is defined for an animation.

PropertyTypeValue
references.animatedComponentAnimatedComponentA reference to the component being animated
references.triggerComponentAnimatedComponentA reference to the component that triggered the scenario (will be null if animation was triggered manually via addAnimation)
references.tweenTweenA reference to GSAP tween object
// example
(refs) => {}

###Animation Generator Function

A function that generates a sub-timeline that describes the animation. Returns a GSAP timeline

PropertyTypeValue
refReact referenceThe element reference passsed in registerAnimation
optionsobjectThe value of animationOptions as configured in an AnimationConfiguration (optional)
// example
(ref, options) => {
    let tl = new TimelineMax();   
    let myEl = ref.current;
    
    tl.to(myEl, 0.5, {
            scale: 1.5,
            rotating: '45deg',
            transformOrigin: 'center',
            opacity: 0.7,          
        });       

    return tl;
};

Contributing

When contributing to this repository, please first discuss the change you wish to make via issue, email, or any other method before submitting a PR.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Keywords

FAQs

Package last updated on 31 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc