Last active
February 7, 2018 17:40
-
-
Save kyle-mccarthy/c997173d360bab3d2a94050cc9149e50 to your computer and use it in GitHub Desktop.
GSAP HOC React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { TimelineLite, TweenLite } from 'gsap'; | |
const asGsapped = (WrappedComponent) => { | |
return class GsappedComponent extends React.PureComponent { | |
constructor(props) { | |
super(props); | |
} | |
to = (opts, duration = 0, offset = 0) => { | |
this._to(this.ref, opts, duration, offset); | |
} | |
fromTo = (fromOpts, toOpts, duration = 0, offset = 0) => { | |
this._fromTo(this.ref, fromOpts, toOpts, duration); | |
} | |
_fromTo = (ref, fromOpts, toOpts, duration = 0, offset = 0) => { | |
try { | |
TweenLite.fromTo(ref, duration, fromOpts, toOpts, offset) | |
} catch (e) { | |
console.trace(); | |
console.log('Attempted to tween null target'); | |
} | |
} | |
_to = (ref, opts, duration = 0, offset = 0) => { | |
try { | |
TweenLite.to(ref, duration, opts, offset); | |
} catch (e) { | |
console.trace(); | |
console.log('Attempted to tween null target'); | |
} | |
} | |
render() { | |
return <WrappedComponent | |
tlTo={this.to} | |
tlFromTo={this.fromTo} | |
innerRef={ref => this.ref = ref} | |
tlToRef={this._to} | |
tlFromToRef={this._fromTo} | |
{...this.props} /> | |
} | |
} | |
} | |
export { asGsapped }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment