Using GSAP with Next.js: A Guide to Animations

Shyam Jith

--

When it comes to powerful, smooth animations in web development, GSAP (GreenSock Animation Platform) is one of the best tools. Integrating GSAP into Next.js allows you to create stunning animations with ease, and the @gsap/react library offers a React-specific integration that simplifies the process even more.

In this blog, we’ll explore how to use GSAP in Next.js, focusing on the useGSAP hook provided by @gsap/react. We'll cover:

  1. Setting up GSAP in Next.js
  2. Using the useGSAP hook
  3. Creating basic animations
  4. Animating on scroll
  5. Optimizing performance with GSAP

1. Setting Up GSAP in Next.js

Before you can start using GSAP, you need to install the GSAP library along with its React integration, @gsap/react.

Step 1: Install GSAP and @gsap/react:

npm install gsap @gsap/react

Step 2: Import GSAP and the useGSAP hook into your Next.js components:

import { useEffect, useRef } from 'react';
import { useGSAP } from '@gsap/react';
import gsap from 'gsap';

2. Using the useGSAP Hook

The useGSAP hook provided by @gsap/react simplifies the process of integrating GSAP animations within React components. You can use this hook to trigger animations during component mount or based on user interactions, such as scrolling or clicking.

Basic useGSAP Example:

import { useRef } from 'react';
import { useGSAP } from '@gsap/react';
import gsap from 'gsap';

export default function AnimatedBox() {
const boxRef = useRef(null);

// Using useGSAP to run animation on mount
useGSAP(() => {
gsap.to(boxRef.current, { x: 200, duration: 1 });
}, []);

return <div ref={boxRef} className="box">I'm animated!</div>;
}

In this example, the useGSAP hook triggers an animation when the component is rendered, moving the box 200px to the right.

3. Creating Basic Animations

GSAP provides a flexible syntax to animate any property of a DOM element, from basic properties like opacity and scale to more advanced properties such as rotation and skew.

Basic Animation Example:

useGSAP(() => {
gsap.fromTo(
boxRef.current,
{ opacity: 0, scale: 0.5 },
{ opacity: 1, scale: 1, duration: 1.5 }
);
}, []);

Here, we animate the box’s opacity and scale properties from an initial state to the final state.

4. Animating on Scroll

GSAP also supports scroll-triggered animations using its ScrollTrigger plugin, which can be used to animate elements as they enter or exit the viewport.

Step 1: Import ScrollTrigger:

import { ScrollTrigger } from 'gsap/ScrollTrigger';

gsap.registerPlugin(ScrollTrigger);

Scroll-triggered Animation Example:

useGSAP(() => {
gsap.fromTo(
boxRef.current,
{ opacity: 0, y: 50 },
{
opacity: 1,
y: 0,
scrollTrigger: {
trigger: boxRef.current,
start: "top 80%",
end: "top 30%",
scrub: true
}
}
);
}, []);

In this example, the box will fade in and move up as you scroll down the page.

5. Optimizing Performance with GSAP

GSAP is well-known for its performance optimizations. Here are some tips to ensure your animations run smoothly:

  • Use will-change: Use the will-change CSS property on animated elements to hint at upcoming changes, improving GPU performance.
.box {
will-change: transform, opacity;
}
  • Batch Animations: Instead of animating individual elements, use batching to group animations together.
  • Reduce scrollTrigger: Avoid having too many scroll-triggered animations active at once. Use lazy: false in ScrollTrigger to optimize resource usage.

Conclusion

Integrating GSAP into your Next.js project can elevate your website’s interactivity and visual appeal. Using the useGSAP hook provided by @gsap/react, you can easily trigger animations on mount, user interactions, or scroll events. With the power of GSAP and careful performance optimizations, your animations will run smoothly, providing a dynamic user experience.

Happy animating!

--

--

Responses (1)