Jump to content
Search Community

How to implement GSAP HTML doc in React?

JamesMaddox test
Moderator Tag

Recommended Posts

Hi all-

 

I'm brand new to the GSAP space. I have an existing React project and want to implement a GSAP animation. The person that made it for me sent it in an HTML file. How would I go about implementing this into my React site? I know there's a react-gsap NPM package but figured there might be an easy way to just plug the HTML doc in and get it working that way? I've attached the file to this post; if you click when it loads, it should play the animation. Thanks!

index-v3-3 (1).html

Link to comment
Share on other sites

Hey James and welcome to the GreenSock forums.

 

You'll have to convert that HTML, CSS, and JavaScript into a React component. It's really pretty similar to making a React component in general. You don't need to use react-gsap to do this. 

 

We have an article about using GSAP with React but it currently uses the old syntax. There's also other resources that use the more modern format like this one (found in our learning section).

 

If you have a specific question let us know and we'll do our best to help out :) 

Link to comment
Share on other sites

Thanks so much for the help Zach!

 

So I broke the HTML file down into a JS file and it worked for about 30 seconds and when I went to make a change it gave me the error message "TypeError: Cannot read property '_gsap' of null" and is talking about harness. Any idea on how to fix this? This is how I have my JS set up:

 

const textTitle = document.querySelector('.hero-text-title');

const textSubtitle = document.querySelector('.hero-text-subtitle');

const textBtn = document.querySelector('.hero button');

const textBottom = document.querySelector('.hero-text-bottom');

const heartSVG = document.querySelector('.heart-svg');

const heartPath = document.querySelector('.heart-line');

 

let tl = gsap

.timeline({ paused: false })

 

.to(

textTitle,

{

duration: 0.3,

opacity: 1,

},

0

)

.from(

textTitle,

{

duration: 0.5,

yPercent: 50,

},

0

)

 

.to(

textSubtitle,

{

duration: 0.5,

opacity: 1,

},

0.4

)

.from(

textSubtitle,

{

duration: 1,

yPercent: 150,

},

0.4

)

 

.to(

heartPath,

{

duration: 1.5,

attr: { 'stroke-dashoffset': 0 },

ease: 'power1.in',

},

0.6

)

.to(

heartPath,

{

duration: 0.2,

opacity: 1,

},

0.7

)

 

.to(

[textBtn, textBottom],

{

duration: 0.6,

opacity: 1,

},

2

)

 

.to(

heartSVG,

{

duration: 0.15,

scale: 1.2,

transformOrigin: 'center center',

ease: 'power1.in',

},

2.8

)

.to(

heartSVG,

{

duration: 0.3,

scale: 1,

},

'>'

);

 

document.onclick = function () {

tl.play(0);

};

Screen Shot 2021-03-12 at 12.04.40 PM.png

Link to comment
Share on other sites

When you say refs, do you mean just pointing to the class instead of using queryselector? So I could just, for example, do:

.to(

'.textTitle',

{

duration: 0.3,

opacity: 1,

},

0

)

 

When I do that, the error goes away but the animation doesn't run. When I CTRL-Z and undo it and change it back to querySelectors, the animation starts working and if I refresh the page again it goes back to the error permanently lol I'm not sure what I'm doing wrong, it seems like it works but its very fragile and not consistent

Link to comment
Share on other sites

I'm definitely not a React guy, so this may not be helpful at all, but this sounds like a hotloading issue with your local environment. Like maybe GSAP was animating specific elements and then when you "reload", it isn't actually reloading the page fresh - it's hot-swapping new elements in there that are completely different instances. I'm totally guessing here. I assume this only happens in your local dev environment and you have hot-loading enabled? 

 

I can assure you that GSAP isn't flaky like that - I'm quite confident this is either React-related or something with your local dev environment that's doing funky stuff. Very difficult to troubleshoot blind, though. Can you create a CodeSandbox or something like that? 

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...