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

@dominguesgm/react-gsap

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dominguesgm/react-gsap

React components for GSAP

  • 1.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-gsap

React components for GSAP

NPM JavaScript Style Guide

Introduction

react-gsap lets you use the GreenSock Animation Platform (GSAP) in React in a fully declarative way. It abstracts away the direct use of the GSAP classes TweenMax and TimelineMax.

If you need the full control it's possible by getting low level access to the underlying objects.

In addition to that it has it's own SVG drawing Plugin and some useful helper components.

Install

npm install --save react-gsap

Usage

import React from 'react';
import { Tween, Timeline } from 'react-gsap';

const TweenComponent = () => (
  <Tween from={{ x: '100px', rotation: -360 }}>
    <div>This element gets tweened</div>
  </Tween>
);

const TimelineComponent = () => (
  <Timeline
    target={
      <div>Target element which will be visible and gets tweened</div>
    }
  >
    <Tween from={{ opacity: 0 }} to={{ opacity: 1 }} />
    <Tween to={{ x: '50px' }} />
  </Timeline>
);

Examples

https://bitworking.github.io/react-gsap/

https://github.com/bitworking/react-gsap/tree/master/example/src/components

You can play with these examples at StackBlitz.io. Because learning by doing is the King:

Tween

Timeline

Svg

Transition

Documentation

react-gsap exports the following components:

Tween

Timeline

SplitWords

SplitLetters

Controls

If you need the full control:

Low level access

It also includes a Tween Plugin which let's you easily draw a SVG:

SvgDraw PlugIn

About GSAP

GreenSock Animation Platform (GSAP) is a set of some JavaScript classes which let you tween a value/attribute/css property over time and insert these tweens into a timeline for more complex animations.

react-gsap uses the classes TweenMax and TimelineMax internally. That means that the following Plugins and tools are available, too:

Tween

The Tween component uses the TweenMax class internally. You can use the following props:

nametypedefaultmore info
durationnumber1Duration in seconds (or frames if prop useFrames = true). Can be changed on-the-fly
fromobjectnullThe vars or fromVars object
toobjectnullThe vars or toVars object. Can be changed on-the-fly
staggerFromobjectnullThe vars or fromVars object
staggerToobjectnullThe vars or toVars object. Can be changed on-the-fly
staggernumber0The stagger parameter for the staggerFrom, staggerTo and staggerFromTo methods
onStartAllFunctionnullParameter for the stagger methods. Get called when the first tween starts
onCompleteAllFunctionnullParameter for the stagger methods. Get called when the last tween stops
wrapperNodenullThis component gets wrapped around the Tween component. Useful for svg's or lists for example.
progressnumbernull0 - 1
totalProgressnumbernull0 - 1
playStatestringnull"play", "reverse", "pause" or "stop" possible
disabledbooleannullon-the-fly (to, staggerTo) and progress, totalProgress or playState changes and are no more possible
onlyInvalidateTobooleannullon-the-fly to preserve the animation origin and only affect the to object
[prop: string]anynullAll other props will get merged with the vars object. So you can use for example useFrames property as prop for the Tween component instead of defining it in the from, to, staggerFrom or staggerTo objects.
childrenNodenullOnly HTML elements, styled-components or React.forwardRef components are getting tweened. Stateless or stateful components need to be wrapped in a HTML element.

How from, to, staggerFrom and staggerTo work?

If you only define the "from" prop then TweenMax.from is called and the prop is passed as the vars object. If you only define the "to" prop then TweenMax.to is called and the prop is passed as the vars object. If you define both props then TweenMax.fromTo is called and "from" is passed as the fromVars and "to" as the toVars. In this case the additional props are also merged with the toVars.

The "staggerFrom" and "staggerTo" props are working analogous and call the following methods: TweenMax.staggerFrom, TweenMax.staggerTo and TweenMax.staggerFromTo.

If you don't define any of these props then TweenMax.from is used and all additional props are passed as the vars object.

In this way you can define a FadeIn component for example like this:

import React from 'react';
import { Tween } from 'react-gsap';

const FadeIn = ({ children, duration }) => (
  <Tween
    opacity={0}
    duration={duration}
  >
    {children}
  </Tween>
);

The most performant option would be to use the css property (CSSPlugin):

const FadeIn = ({ children, duration }) => (
  <Tween
    css={{ opacity: 0 }}
    duration={duration}
  >
    {children}
  </Tween>
);

// equivalent with

const FadeIn = ({ children, duration }) => (
  <Tween
    from={{ css: { opacity: 0 }}}
    duration={duration}
  >
    {children}
  </Tween>
);

Timeline

The Timeline component uses the TimelineMax class internally. You can use the following props:

nametypedefaultmore info
targetNodenullThe target component that gets outputted and tweened from all childless Tween child components
wrapperNodenullThis component gets wrapped around the Tween component. Useful for svg's or lists for example.
durationnumbernullAdjusts the timeline's timeScale. Can be changed on-the-fly
progressnumbernull0 - 1
totalProgressnumbernull0 - 1
playStatestringnull"play", "reverse", "pause" or "stop" possible
[prop: string]anynullAll other props are passed as the vars object for the TimelineMax constructor
childrenNodesnullTween and other Timeline components are added to this Timeline via the TimelineMax.add method. On the children you can define the extra props "position", "align" or "stagger" which are passed as additional parameters.

SplitWords

Easy component which splits text by words and clones the wrapper component for every word.

Example use: https://github.com/bitworking/react-gsap/blob/master/example/src/components/Tween.js

SplitLetters

Easy component which splits text by letters and clones the wrapper component for every letter.

Example use: https://github.com/bitworking/react-gsap/blob/master/example/src/components/Tween.js

Controls

You can wrap this component around one Tween or Timeline component and you get a nice control panel with play, reverse, pause, stop and a slider controls.

Low level access

You are able to use the complete API of the underlying GSAP objects. Just add a reference to the Tween or Timeline components and get the TweenMax or TimelineMax objects by calling the getGSAP() method on it:

import React, { Component } from 'react';
import { Tween } from 'react-gsap';

class LowLevelAccess extends Component {
  tween;

  componentDidMount() {
    // tween is now a TweenMax class instance
    const tween = this.tween.getGSAP();

    // You can call any method on it
    tween.timeScale(0.5);
  }

  render() {
    return(
        <Tween
          to={{
            x: 200,
            y: 200,
          }}
          duration={2}
          ease="Back.easeOut"
          ref={ref => this.tween = ref}
        >
          <div>This div gets tweened</div>
        </Tween>
    );
  }
}

export default LowLevelAccess;

Remember: If you use the stagger props on a Tween component (staggerTo, staggerFrom) then getGSAP() will return a TimelineMax and not a TweenMax object.

SvgDraw Plugin

With this Plugin you can draw the following SVG elements: path, circle, rect, line, polyline and polygon. It works similar to the DrawSVGPlugin from GreenSock but the parameters are different.

It can be called with the "svgDraw" property and takes a single number (0-1) value or an array with two numbers ([(0-1), (0-1)]).

The single or first number is the length of the stroke.

So you can animate a line drawing from start to finish like that:

<Tween
  wrapper={
    <svg width="100" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" />
  }
  from={{
    svgDraw: 0,
  }}
  to={{
    svgDraw: 1,
  }}
>
  <circle cx="25" cy="75" r="20" stroke="red" strokeWidth="5" />
</Tween>

The second value is the position/offset on the path. (default = 0)

An animation from the middle to the outside:

<Tween
  wrapper={
    <svg width="100" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" />
  }
  from={{
    svgDraw: [0, 0.5],
  }}
  to={{
    svgDraw: [1, 0],
  }}
>
  <circle cx="25" cy="75" r="20" stroke="red" strokeWidth="5" />
</Tween>

License

MIT © bitworking

FAQs

Package last updated on 20 Aug 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